changeset 1289:8d54bcc0b6d3

add css style parser
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 31 Dec 2018 20:59:08 -0700
parents 8432d20a2729
children fb802d8aba0b
files src/luan/modules/parsers/Css.java src/luan/modules/parsers/Html.java
diffstat 2 files changed, 72 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
diff -r 8432d20a2729 -r 8d54bcc0b6d3 src/luan/modules/parsers/Css.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/parsers/Css.java	Mon Dec 31 20:59:08 2018 -0700
@@ -0,0 +1,59 @@
+package luan.modules.parsers;
+
+import luan.LuanState;
+import luan.LuanTable;
+import luan.lib.parser.Parser;
+
+
+public final class Css {
+
+	public static LuanTable style(LuanState luan,String text) {
+		return new Css(luan,text).parseStyle();
+	}
+
+	private final LuanState luan;
+	private final Parser parser;
+
+	private Css(LuanState luan,String text) {
+		this.luan = luan;
+		this.parser = new Parser(text);
+	}
+
+	private LuanTable parseStyle() {
+		LuanTable tbl = new LuanTable(luan);
+		while( matchSpace() );
+		while( !parser.endOfInput() ) {
+			int start = parser.currentIndex();
+			if( !matchPropertyChar() )
+				return null;
+			while( matchPropertyChar() );
+			String prop = parser.textFrom(start).toLowerCase();
+
+			while( matchSpace() );
+			if( !parser.match(':') )
+				return null;
+
+			start = parser.currentIndex();
+			while( !parser.endOfInput() && parser.noneOf(";") );
+			String val = parser.textFrom(start).trim();
+
+			tbl.rawPut(prop,val);
+			parser.match(';');
+			while( matchSpace() );
+		}
+		return tbl;
+	}
+
+	private boolean matchPropertyChar() {
+		return parser.inCharRange('a','z')
+			|| parser.inCharRange('A','Z')
+			|| parser.inCharRange('0','9')
+			|| parser.anyOf("_-")
+		;
+	}
+
+	private boolean matchSpace() {
+		return parser.anyOf(" \t\r\n");
+	}
+
+}
diff -r 8432d20a2729 -r 8d54bcc0b6d3 src/luan/modules/parsers/Html.java
--- a/src/luan/modules/parsers/Html.java	Mon Dec 31 18:39:04 2018 -0700
+++ b/src/luan/modules/parsers/Html.java	Mon Dec 31 20:59:08 2018 -0700
@@ -7,14 +7,11 @@
 import luan.LuanState;
 import luan.LuanTable;
 import luan.lib.parser.Parser;
-import luan.lib.parser.ParseException;
 
 
 public final class Html {
 
-	public static LuanTable toList(LuanState luan,String text,LuanTable containerTagsTbl)
-		throws ParseException
-	{
+	public static LuanTable toList(LuanState luan,String text,LuanTable containerTagsTbl) {
 		return new Html(luan,text,containerTagsTbl).parse();
 	}
 
@@ -30,7 +27,7 @@
 		}
 	}
 
-	private LuanTable parse() throws ParseException {
+	private LuanTable parse() {
 		List list = new ArrayList();
 		StringBuilder sb = new StringBuilder();
 		while( !parser.endOfInput() ) {
@@ -120,6 +117,8 @@
 	}
 
 	private LuanTable parseTag() {
+		LuanTable tbl = new LuanTable(luan);
+		tbl.rawPut("type","tag");
 		int tagStart = parser.begin();
 		if( !parser.match('<') )
 			return parser.failure(null);
@@ -129,22 +128,25 @@
 			return parser.failure(null);
 		while( matchNameChar() );
 		String name = parser.textFrom(start).toLowerCase();
+		tbl.rawPut("name",name);
 		LuanTable attributes = new LuanTable(luan);
+		tbl.rawPut("attributes",attributes);
 		String attrName;
 		while( (attrName = parseAttrName()) != null ) {
 			String attrValue = parseAttrValue();
 			attributes.rawPut( attrName, attrValue!=null ? attrValue : true );
+			if( attrName.equals("style") && attrValue!=null ) {
+				LuanTable style = Css.style(luan,attrValue);
+				if( style!=null )
+					tbl.rawPut("style",style);
+			}
 		}
 		while( matchSpace() );
 		boolean isEmpty = parser.match('/');
+		tbl.rawPut("is_empty",isEmpty);
 		if( !parser.match('>') )
 			return parser.failure(null);
 		String raw = parser.textFrom(tagStart);
-		LuanTable tbl = new LuanTable(luan);
-		tbl.rawPut("type","tag");
-		tbl.rawPut("name",name);
-		tbl.rawPut("attributes",attributes);
-		tbl.rawPut("is_empty",isEmpty);
 		tbl.rawPut("raw",raw);
 		return parser.success(tbl);
 	}
@@ -158,7 +160,7 @@
 		if( !matchNameChar() )
 			return parser.failure(null);
 		while( matchNameChar() );
-		String name = parser.textFrom(start);
+		String name = parser.textFrom(start).toLowerCase();
 		return parser.success(name);
 	}