diff src/luan/modules/parsers/BBCodeLuan.java @ 1702:8ad468cc88d4

add goodjava/bbcode
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 30 Jun 2022 20:04:34 -0600
parents src/luan/modules/parsers/BBCode.java@8fbcc4747091
children a6e27c8e7ef4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/parsers/BBCodeLuan.java	Thu Jun 30 20:04:34 2022 -0600
@@ -0,0 +1,54 @@
+package luan.modules.parsers;
+
+import java.util.List;
+import java.util.ArrayList;
+import luan.Luan;
+import luan.LuanFunction;
+import luan.LuanException;
+import luan.LuanRuntimeException;
+import luan.modules.Utils;
+import luan.modules.HtmlLuan;
+import goodjava.bbcode.BBCode;
+
+
+public final class BBCodeLuan {
+
+	private static BBCode.Quoter quoter(final Luan luan,final LuanFunction quoterFn) {
+		return new BBCode.Quoter() {
+			public String quote(BBCode.Target target,String text,String param) {
+				try {
+					Object obj = quoterFn.call(luan,text,param);
+					if( !(obj instanceof String) )
+						throw new LuanException("BBCode quoter function returned "+Luan.type(obj)+" but string required");
+					return (String)obj;
+				} catch(LuanException e) {
+					throw new LuanRuntimeException(e);
+				}
+			}
+		};
+	}
+
+	public static String toHtml(Luan luan,String text,LuanFunction quoterFn) throws LuanException {
+		return parse(luan,text,quoterFn,BBCode.Target.HTML);
+	}
+
+	public static String toText(Luan luan,String text,LuanFunction quoterFn) throws LuanException {
+		return parse(luan,text,quoterFn,BBCode.Target.TEXT);
+	}
+
+	private static String parse(Luan luan,String text,LuanFunction quoterFn,BBCode.Target target)
+		throws LuanException
+	{
+		Utils.checkNotNull(text,1);
+		BBCode bbcode = new BBCode(text);
+		bbcode.target = target;
+		if( quoterFn != null )
+			bbcode.quoter = quoter(luan,quoterFn);
+		try {
+			return bbcode.parse();
+		} catch(LuanRuntimeException e) {
+			throw (LuanException)e.getCause();
+		}
+	}
+
+}