changeset 1714:31a82b0d0a87

bbcode and html work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Jul 2022 23:49:47 -0600
parents 4d70e9543ef2
children ad44e849c60c
files src/goodjava/bbcode/BBCode.java src/goodjava/html/Css.java src/goodjava/html/Html.java src/luan/modules/HtmlLuan.java
diffstat 4 files changed, 73 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/bbcode/BBCode.java	Fri Jul 15 11:55:27 2022 -0600
+++ b/src/goodjava/bbcode/BBCode.java	Mon Jul 18 23:49:47 2022 -0600
@@ -4,7 +4,6 @@
 import java.util.ArrayList;
 import java.util.Map;
 import java.util.LinkedHashMap;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import goodjava.parser.Parser;
 
@@ -276,10 +275,10 @@
 		if( !parser.matchIgnoreCase("[color=") )
 			return parser.failure(null);
 		int start = parser.currentIndex();
-		parser.match('#');
 		while( parser.inCharRange('0','9')
 			|| parser.inCharRange('a','z')
 			|| parser.inCharRange('A','Z')
+			|| parser.anyOf("#(, )")
 		);
 		String color = parser.textFrom(start);
 		if( !parser.match(']') )
@@ -297,6 +296,8 @@
 			return parser.failure(null);
 		int start = parser.currentIndex();
 		while( parser.match('.') || parser.inCharRange('0','9') );
+		if( parser.matchIgnoreCase("pt") || parser.match('%') )
+			; // ok
 		String size = parser.textFrom(start);
 		if( !parser.match(']') )
 			return parser.failure(null);
@@ -307,15 +308,6 @@
 		return parser.success(rtn);
 	}
 
-	private static final Pattern youtubePtn1 = Pattern.compile("https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?");
-	private static final Pattern youtubePtn2 = Pattern.compile("https://www.youtube.com/watch?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?");
-	private static final Pattern bitchutePtn = Pattern.compile("https://www.bitchute.com/video/([a-zA-Z0-9]+)/");
-
-	private static Matcher find(Pattern ptn,String s) {
-		Matcher m = ptn.matcher(s);
-		return m.find() ? m : null;
-	}
-
 	private Element parseVideo() {
 		parser.begin();
 		if( !parser.matchIgnoreCase("[video]") )
@@ -323,26 +315,7 @@
 		String url = parseRealUrl();
 		if( !parser.matchIgnoreCase("[/video]") )
 			return parser.failure(null);
-		Map<String,String> extra = new LinkedHashMap<String,String>();
-		Element rtn = new Element("video",null,url,extra);
-		Matcher m;
-		m = find(youtubePtn1,url);
-		if( m == null )
-			m = find(youtubePtn2,url);
-		if( m != null ) {
-			extra.put( "site", "youtube" );
-			extra.put( "id", m.group(1) );
-			String t = m.group(2);
-			if( t != null )
-				extra.put( "start", t );
-			return parser.success(rtn);
-		}
-		m = find(bitchutePtn,url);
-		if( m != null ) {
-			extra.put( "site", "bitchute" );
-			extra.put( "id", m.group(1) );
-			return parser.success(rtn);
-		}
+		Element rtn = new Element("video",null,url);
 		return parser.success(rtn);
 	}
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/html/Css.java	Mon Jul 18 23:49:47 2022 -0600
@@ -0,0 +1,57 @@
+package goodjava.html;
+
+import java.util.Map;
+import java.util.LinkedHashMap;
+import goodjava.parser.Parser;
+
+
+public final class Css {
+
+	public static Map<String,String> style(String text) {
+		return new Css(text).parseStyle();
+	}
+
+	private final Parser parser;
+
+	private Css(String text) {
+		this.parser = new Parser(text);
+	}
+
+	private Map<String,String> parseStyle() {
+		Map<String,String> map = new LinkedHashMap<String,String>();
+		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();
+
+			map.put(prop,val);
+			parser.match(';');
+			while( matchSpace() );
+		}
+		return map;
+	}
+
+	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");
+	}
+
+}
--- a/src/goodjava/html/Html.java	Fri Jul 15 11:55:27 2022 -0600
+++ b/src/goodjava/html/Html.java	Mon Jul 18 23:49:47 2022 -0600
@@ -72,12 +72,18 @@
 		public final Map<String,Object> attributes;
 		public final boolean isEmpty;
 		public final String raw;
+		public final Map<String,String> style;
 
 		private Tag(String name,Map<String,Object> attributes,boolean isEmpty,String raw) {
+			this(name,attributes,isEmpty,raw,null);
+		}
+
+		private Tag(String name,Map<String,Object> attributes,boolean isEmpty,String raw,Map<String,String> style) {
 			this.name = name;
 			this.attributes = attributes;
 			this.isEmpty = isEmpty;
 			this.raw = raw;
+			this.style = style;
 		}
 	}
 
@@ -209,23 +215,20 @@
 		String name = parser.textFrom(start).toLowerCase();
 		Map<String,Object> attributes = new HashMap<String,Object>();
 		String attrName;
+		Map<String,String> style = null;
 		while( (attrName = parseAttrName()) != null ) {
 			String attrValue = parseAttrValue();
 			attributes.put( attrName, attrValue!=null ? attrValue : true );
-/*
-			if( attrName.equals("style") && attrValue!=null ) {
-				LuanTable style = Css.style(attrValue);
-				if( style!=null )
-					tbl.rawPut("style",style);
+			if( attrName.equals("style") && attrValue!=null && style==null ) {
+				style = Css.style(attrValue);
 			}
-*/
 		}
 		while( matchSpace() );
 		boolean isEmpty = parser.match('/');
 		if( !parser.match('>') )
 			return parser.failure(null);
 		String raw = parser.textFrom(tagStart);
-		Tag tag = new Tag(name,attributes,isEmpty,raw);
+		Tag tag = new Tag(name,attributes,isEmpty,raw,style);
 		return parser.success(tag);
 	}
 
--- a/src/luan/modules/HtmlLuan.java	Fri Jul 15 11:55:27 2022 -0600
+++ b/src/luan/modules/HtmlLuan.java	Mon Jul 18 23:49:47 2022 -0600
@@ -27,6 +27,8 @@
 		tbl.rawPut("attributes",new LuanTable(tag.attributes));
 		tbl.rawPut("is_empty",tag.isEmpty);
 		tbl.rawPut("raw",tag.raw);
+		if( tag.style != null )
+			tbl.rawPut("style",new LuanTable(tag.style));
 		return tbl;
 	}
 	public static LuanTable parse(String text,LuanTable containerTagsTbl) {