Mercurial Hosting > luan
comparison src/luan/modules/HtmlLuan.java @ 1712:36c28be6d432
improve html and bbcode
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Jul 2022 22:14:21 -0600 |
parents | 21f5edab1fbf |
children | 31a82b0d0a87 |
comparison
equal
deleted
inserted
replaced
1711:05d14db623b6 | 1712:36c28be6d432 |
---|---|
1 package luan.modules; | 1 package luan.modules; |
2 | 2 |
3 import java.util.List; | 3 import java.util.List; |
4 import java.util.ArrayList; | 4 import java.util.ArrayList; |
5 import java.util.Arrays; | |
6 import java.util.Set; | 5 import java.util.Set; |
7 import java.util.HashSet; | 6 import java.util.HashSet; |
8 import java.util.Map; | 7 import goodjava.html.Html; |
9 import java.util.regex.Pattern; | |
10 import java.util.regex.Matcher; | |
11 import luan.Luan; | |
12 import luan.LuanTable; | 8 import luan.LuanTable; |
13 import luan.LuanException; | 9 import luan.LuanException; |
14 | 10 |
15 | 11 |
16 public final class HtmlLuan { | 12 public final class HtmlLuan { |
17 | 13 |
18 public static String encode(String s) throws LuanException { | 14 public static String encode(String s) throws LuanException { |
19 Utils.checkNotNull(s); | 15 Utils.checkNotNull(s); |
20 final char[] a = s.toCharArray(); | 16 return Html.encode(s); |
21 StringBuilder buf = new StringBuilder(); | |
22 for( char c : a ) { | |
23 switch(c) { | |
24 case '&': | |
25 buf.append("&"); | |
26 break; | |
27 case '<': | |
28 buf.append("<"); | |
29 break; | |
30 case '>': | |
31 buf.append(">"); | |
32 break; | |
33 case '"': | |
34 buf.append("""); | |
35 break; | |
36 default: | |
37 buf.append(c); | |
38 } | |
39 } | |
40 return buf.toString(); | |
41 } | 17 } |
42 | 18 |
43 private static final Pattern entityPtn = Pattern.compile( | |
44 "&#(\\d+);" | |
45 ); | |
46 | |
47 public static String decode(String s) { | 19 public static String decode(String s) { |
48 StringBuffer buf = new StringBuffer(); | 20 return Html.decode(s); |
49 Matcher m = entityPtn.matcher(s); | |
50 while( m.find() ) { | |
51 String entity = new String(new char[]{(char)Integer.parseInt(m.group(1))}); | |
52 m.appendReplacement(buf,entity); | |
53 } | |
54 m.appendTail(buf); | |
55 s = buf.toString(); | |
56 s = s.replace(" "," "); | |
57 s = s.replace(""","\""); | |
58 s = s.replace(">",">"); | |
59 s = s.replace("<","<"); | |
60 s = s.replace("&","&"); | |
61 return s; | |
62 } | 21 } |
63 | 22 |
64 public static String quote(String s) { | 23 private static LuanTable tag(Html.Tag tag) throws LuanException { |
65 StringBuilder buf = new StringBuilder(); | 24 LuanTable tbl = new LuanTable(); |
66 buf.append('"'); | 25 tbl.rawPut("type","tag"); |
67 int i = 0; | 26 tbl.rawPut("name",tag.name); |
68 while(true) { | 27 tbl.rawPut("attributes",new LuanTable(tag.attributes)); |
69 int i2 = s.indexOf('"',i); | 28 tbl.rawPut("is_empty",tag.isEmpty); |
70 if( i2 == -1 ) { | 29 tbl.rawPut("raw",tag.raw); |
71 buf.append(s.substring(i)); | 30 return tbl; |
72 break; | 31 } |
73 } else { | 32 public static LuanTable parse(String text,LuanTable containerTagsTbl) { |
74 buf.append(s.substring(i,i2)); | 33 try { |
75 buf.append("""); | 34 Set<String> containerTags = new HashSet(); |
76 i = i2 + 1; | 35 for( Object s : containerTagsTbl.asList() ) { |
36 containerTags.add((String)s); | |
77 } | 37 } |
38 List list = Html.parse(text,containerTags); | |
39 List rtn = new ArrayList(); | |
40 for( Object el : list ) { | |
41 if( el instanceof String ) { | |
42 rtn.add(el); | |
43 } else if( el instanceof Html.Tag ) { | |
44 Html.Tag tag = (Html.Tag)el; | |
45 rtn.add(tag(tag)); | |
46 } else if( el instanceof Html.Comment ) { | |
47 Html.Comment comment = (Html.Comment)el; | |
48 LuanTable tbl = new LuanTable(); | |
49 tbl.rawPut("type","comment"); | |
50 tbl.rawPut("text",comment.text); | |
51 rtn.add(tbl); | |
52 } else if( el instanceof Html.CData ) { | |
53 Html.CData cdata = (Html.CData)el; | |
54 LuanTable tbl = new LuanTable(); | |
55 tbl.rawPut("type","cdata"); | |
56 tbl.rawPut("text",cdata.text); | |
57 rtn.add(tbl); | |
58 } else if( el instanceof Html.Container ) { | |
59 Html.Container container = (Html.Container)el; | |
60 LuanTable tbl = new LuanTable(); | |
61 tbl.rawPut("type","container"); | |
62 tbl.rawPut("tag",tag(container.tag)); | |
63 tbl.rawPut("text",container.text); | |
64 rtn.add(tbl); | |
65 } else { | |
66 throw new RuntimeException("invalid el "+el); | |
67 } | |
68 } | |
69 return new LuanTable(rtn); | |
70 } catch(LuanException e) { | |
71 throw new RuntimeException(e); | |
78 } | 72 } |
79 buf.append('"'); | |
80 return buf.toString(); | |
81 } | 73 } |
82 | 74 |
83 } | 75 } |