Mercurial Hosting > luan
comparison src/luan/modules/parsers/Html.java @ 1562:b89212fd04b5
remove table.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Nov 2020 16:50:59 -0700 |
parents | 27efb1fcbcb5 |
children |
comparison
equal
deleted
inserted
replaced
1561:e1a13e707bf3 | 1562:b89212fd04b5 |
---|---|
10 import goodjava.parser.Parser; | 10 import goodjava.parser.Parser; |
11 | 11 |
12 | 12 |
13 public final class Html { | 13 public final class Html { |
14 | 14 |
15 public static LuanTable toList(Luan luan,String text,LuanTable containerTagsTbl) { | 15 public static LuanTable toList(String text,LuanTable containerTagsTbl) { |
16 try { | 16 try { |
17 return new Html(luan,text,containerTagsTbl).parse(); | 17 return new Html(text,containerTagsTbl).parse(); |
18 } catch(LuanException e) { | 18 } catch(LuanException e) { |
19 throw new RuntimeException(e); | 19 throw new RuntimeException(e); |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 private final Luan luan; | |
24 private final Parser parser; | 23 private final Parser parser; |
25 private final Set<String> containerTags = new HashSet<String>(); | 24 private final Set<String> containerTags = new HashSet<String>(); |
26 | 25 |
27 private Html(Luan luan,String text,LuanTable containerTagsTbl) { | 26 private Html(String text,LuanTable containerTagsTbl) { |
28 this.luan = luan; | |
29 this.parser = new Parser(text); | 27 this.parser = new Parser(text); |
30 for( Object v : containerTagsTbl.asList() ) { | 28 for( Object v : containerTagsTbl.asList() ) { |
31 containerTags.add((String)v); | 29 containerTags.add((String)v); |
32 } | 30 } |
33 } | 31 } |
61 sb.append( parser.currentChar() ); | 59 sb.append( parser.currentChar() ); |
62 parser.anyChar(); | 60 parser.anyChar(); |
63 } | 61 } |
64 if( sb.length() > 0 ) | 62 if( sb.length() > 0 ) |
65 list.add(sb.toString()); | 63 list.add(sb.toString()); |
66 return new LuanTable(luan,list); | 64 return new LuanTable(list); |
67 } | 65 } |
68 | 66 |
69 private LuanTable parseComment() throws LuanException { | 67 private LuanTable parseComment() throws LuanException { |
70 parser.begin(); | 68 parser.begin(); |
71 if( !parser.match("<!--") ) | 69 if( !parser.match("<!--") ) |
74 while( !parser.test("-->") ) { | 72 while( !parser.test("-->") ) { |
75 if( !parser.anyChar() ) | 73 if( !parser.anyChar() ) |
76 return parser.failure(null); | 74 return parser.failure(null); |
77 } | 75 } |
78 String text = parser.textFrom(start); | 76 String text = parser.textFrom(start); |
79 LuanTable tbl = new LuanTable(luan); | 77 LuanTable tbl = new LuanTable(); |
80 tbl.put("type","comment"); | 78 tbl.rawPut("type","comment"); |
81 tbl.put("text",text); | 79 tbl.rawPut("text",text); |
82 return parser.success(tbl); | 80 return parser.success(tbl); |
83 } | 81 } |
84 | 82 |
85 private LuanTable parseCdata() throws LuanException { | 83 private LuanTable parseCdata() throws LuanException { |
86 parser.begin(); | 84 parser.begin(); |
90 while( !parser.test("]]>") ) { | 88 while( !parser.test("]]>") ) { |
91 if( !parser.anyChar() ) | 89 if( !parser.anyChar() ) |
92 return parser.failure(null); | 90 return parser.failure(null); |
93 } | 91 } |
94 String text = parser.textFrom(start); | 92 String text = parser.textFrom(start); |
95 LuanTable tbl = new LuanTable(luan); | 93 LuanTable tbl = new LuanTable(); |
96 tbl.put("type","cdata"); | 94 tbl.rawPut("type","cdata"); |
97 tbl.put("text",text); | 95 tbl.rawPut("text",text); |
98 return parser.success(tbl); | 96 return parser.success(tbl); |
99 } | 97 } |
100 | 98 |
101 private LuanTable parseContainer(LuanTable tag) throws LuanException { | 99 private LuanTable parseContainer(LuanTable tag) throws LuanException { |
102 String endTagName = '/' + (String)tag.rawGet("name"); | 100 String endTagName = '/' + (String)tag.rawGet("name"); |
112 } | 110 } |
113 if( !parser.anyChar() ) | 111 if( !parser.anyChar() ) |
114 return parser.failure(null); | 112 return parser.failure(null); |
115 } | 113 } |
116 String text = parser.text.substring(start,end); | 114 String text = parser.text.substring(start,end); |
117 LuanTable tbl = new LuanTable(luan); | 115 LuanTable tbl = new LuanTable(); |
118 tbl.put("type","container"); | 116 tbl.rawPut("type","container"); |
119 tbl.put("tag",tag); | 117 tbl.rawPut("tag",tag); |
120 tbl.put("text",text); | 118 tbl.rawPut("text",text); |
121 return parser.success(tbl); | 119 return parser.success(tbl); |
122 } | 120 } |
123 | 121 |
124 private LuanTable parseTag() throws LuanException { | 122 private LuanTable parseTag() throws LuanException { |
125 LuanTable tbl = new LuanTable(luan); | 123 LuanTable tbl = new LuanTable(); |
126 tbl.put("type","tag"); | 124 tbl.rawPut("type","tag"); |
127 int tagStart = parser.begin(); | 125 int tagStart = parser.begin(); |
128 if( !parser.match('<') ) | 126 if( !parser.match('<') ) |
129 return parser.failure(null); | 127 return parser.failure(null); |
130 int start = parser.currentIndex(); | 128 int start = parser.currentIndex(); |
131 parser.match('/'); | 129 parser.match('/'); |
132 if( !matchNameChar() ) | 130 if( !matchNameChar() ) |
133 return parser.failure(null); | 131 return parser.failure(null); |
134 while( matchNameChar() ); | 132 while( matchNameChar() ); |
135 String name = parser.textFrom(start).toLowerCase(); | 133 String name = parser.textFrom(start).toLowerCase(); |
136 tbl.put("name",name); | 134 tbl.rawPut("name",name); |
137 LuanTable attributes = new LuanTable(luan); | 135 LuanTable attributes = new LuanTable(); |
138 tbl.put("attributes",attributes); | 136 tbl.rawPut("attributes",attributes); |
139 String attrName; | 137 String attrName; |
140 while( (attrName = parseAttrName()) != null ) { | 138 while( (attrName = parseAttrName()) != null ) { |
141 String attrValue = parseAttrValue(); | 139 String attrValue = parseAttrValue(); |
142 attributes.put( attrName, attrValue!=null ? attrValue : true ); | 140 attributes.rawPut( attrName, attrValue!=null ? attrValue : true ); |
143 if( attrName.equals("style") && attrValue!=null ) { | 141 if( attrName.equals("style") && attrValue!=null ) { |
144 LuanTable style = Css.style(luan,attrValue); | 142 LuanTable style = Css.style(attrValue); |
145 if( style!=null ) | 143 if( style!=null ) |
146 tbl.put("style",style); | 144 tbl.rawPut("style",style); |
147 } | 145 } |
148 } | 146 } |
149 while( matchSpace() ); | 147 while( matchSpace() ); |
150 boolean isEmpty = parser.match('/'); | 148 boolean isEmpty = parser.match('/'); |
151 tbl.put("is_empty",isEmpty); | 149 tbl.rawPut("is_empty",isEmpty); |
152 if( !parser.match('>') ) | 150 if( !parser.match('>') ) |
153 return parser.failure(null); | 151 return parser.failure(null); |
154 String raw = parser.textFrom(tagStart); | 152 String raw = parser.textFrom(tagStart); |
155 tbl.put("raw",raw); | 153 tbl.rawPut("raw",raw); |
156 return parser.success(tbl); | 154 return parser.success(tbl); |
157 } | 155 } |
158 | 156 |
159 private String parseAttrName() { | 157 private String parseAttrName() { |
160 parser.begin(); | 158 parser.begin(); |