Mercurial Hosting > luan
comparison core/src/luan/modules/HtmlLuan.java @ 426:23a93c118042
fix LuanTable.get() to use metatables
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2015 18:44:20 -0600 |
parents | 23b99a5039b5 |
children | dae264ad6a7b |
comparison
equal
deleted
inserted
replaced
425:0a2fb80907f9 | 426:23a93c118042 |
---|---|
97 break; | 97 break; |
98 } | 98 } |
99 String tagText = text.substring(i2+1,i); | 99 String tagText = text.substring(i2+1,i); |
100 try { | 100 try { |
101 LuanTable tag = parseTag(tagText); | 101 LuanTable tag = parseTag(tagText); |
102 String tagName = (String)tag.get("name"); | 102 String tagName = (String)tag.rawGet("name"); |
103 if( containerTags.contains(tagName) ) { | 103 if( containerTags.contains(tagName) ) { |
104 i2 = i; | 104 i2 = i; |
105 String endTagName = '/' + tagName; | 105 String endTagName = '/' + tagName; |
106 while(true) { | 106 while(true) { |
107 i2 = text.indexOf('<',i2+1); | 107 i2 = text.indexOf('<',i2+1); |
198 i = i2; | 198 i = i2; |
199 while( i<len && Character.isWhitespace(text.charAt(i)) ) i++; | 199 while( i<len && Character.isWhitespace(text.charAt(i)) ) i++; |
200 while( i<len ) { | 200 while( i<len ) { |
201 i2 = toEndName(text,i,len); | 201 i2 = toEndName(text,i,len); |
202 String attrName = unquote(text.substring(i,i2).toLowerCase()); | 202 String attrName = unquote(text.substring(i,i2).toLowerCase()); |
203 if( attributes.get(attrName) != null ) | 203 if( attributes.rawGet(attrName) != null ) |
204 throw new BadTag("duplicate attribute: "+attrName); | 204 throw new BadTag("duplicate attribute: "+attrName); |
205 i = i2; | 205 i = i2; |
206 while( i<len && Character.isWhitespace(text.charAt(i)) ) i++; | 206 while( i<len && Character.isWhitespace(text.charAt(i)) ) i++; |
207 if( i<len && text.charAt(i) == '=' ) { | 207 if( i<len && text.charAt(i) == '=' ) { |
208 i++; | 208 i++; |
280 for( Object o : html ) { | 280 for( Object o : html ) { |
281 if( o instanceof String ) { | 281 if( o instanceof String ) { |
282 buf.append( o ); | 282 buf.append( o ); |
283 } else if( o instanceof LuanTable ) { | 283 } else if( o instanceof LuanTable ) { |
284 LuanTable t = (LuanTable)o; | 284 LuanTable t = (LuanTable)o; |
285 String type = (String)t.get("type"); | 285 String type = (String)t.get(luan,"type"); |
286 if( type==null ) | 286 if( type==null ) |
287 throw luan.exception( "no type in element of table for 'Html.to_string'" ); | 287 throw luan.exception( "no type in element of table for 'Html.to_string'" ); |
288 if( type.equals("comment") ) { | 288 if( type.equals("comment") ) { |
289 buf.append( "<!--" ).append( t.get("text") ).append( "-->" ); | 289 buf.append( "<!--" ).append( t.get(luan,"text") ).append( "-->" ); |
290 } else if( type.equals("cdata") ) { | 290 } else if( type.equals("cdata") ) { |
291 buf.append( "<![CDATA[" ).append( t.get("text") ).append( "]]" ); | 291 buf.append( "<![CDATA[" ).append( t.get(luan,"text") ).append( "]]" ); |
292 } else if( type.equals("tag") ) { | 292 } else if( type.equals("tag") ) { |
293 buf.append( tagToString(t) ); | 293 buf.append( tagToString(luan,t) ); |
294 } else if( type.equals("container") ) { | 294 } else if( type.equals("container") ) { |
295 LuanTable tag = (LuanTable)t.get("tag"); | 295 LuanTable tag = (LuanTable)t.get(luan,"tag"); |
296 buf.append( tagToString(tag) ); | 296 buf.append( tagToString(luan,tag) ); |
297 buf.append( t.get("text") ); | 297 buf.append( t.get(luan,"text") ); |
298 buf.append( "</" ).append( tag.get("name") ).append( ">" ); | 298 buf.append( "</" ).append( tag.get(luan,"name") ).append( ">" ); |
299 } else { | 299 } else { |
300 throw luan.exception( "invalid element type for 'Html.to_string'" ); | 300 throw luan.exception( "invalid element type for 'Html.to_string'" ); |
301 } | 301 } |
302 } else | 302 } else |
303 throw luan.exception( "invalid value ("+Luan.type(o)+") in table for 'Html.to_string'" ); | 303 throw luan.exception( "invalid value ("+Luan.type(o)+") in table for 'Html.to_string'" ); |
304 } | 304 } |
305 return buf.toString(); | 305 return buf.toString(); |
306 } | 306 } |
307 | 307 |
308 private static String tagToString(LuanTable tbl) { | 308 private static String tagToString(LuanState luan,LuanTable tbl) throws LuanException { |
309 StringBuilder buf = new StringBuilder(); | 309 StringBuilder buf = new StringBuilder(); |
310 buf.append('<'); | 310 buf.append('<'); |
311 buf.append(tbl.get("name")); | 311 buf.append(tbl.get(luan,"name")); |
312 LuanTable attributes = (LuanTable)tbl.get("attributes"); | 312 LuanTable attributes = (LuanTable)tbl.get(luan,"attributes"); |
313 for( Map.Entry<Object,Object> attr : attributes ) { | 313 for( Map.Entry<Object,Object> attr : attributes ) { |
314 buf.append( ' ' ); | 314 buf.append( ' ' ); |
315 buf.append( attr.getKey() ); | 315 buf.append( attr.getKey() ); |
316 Object val = attr.getValue(); | 316 Object val = attr.getValue(); |
317 if( !val.equals(Boolean.TRUE) ) { | 317 if( !val.equals(Boolean.TRUE) ) { |
318 buf.append( '=' ); | 318 buf.append( '=' ); |
319 buf.append( quote((String)val) ); | 319 buf.append( quote((String)val) ); |
320 } | 320 } |
321 } | 321 } |
322 if( tbl.get("is_empty").equals(Boolean.TRUE) ) | 322 if( tbl.get(luan,"is_empty").equals(Boolean.TRUE) ) |
323 buf.append('/'); | 323 buf.append('/'); |
324 buf.append('>'); | 324 buf.append('>'); |
325 return buf.toString(); | 325 return buf.toString(); |
326 } | 326 } |
327 | 327 |