comparison 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
comparison
equal deleted inserted replaced
1701:077366d117bb 1702:8ad468cc88d4
1 package luan.modules.parsers;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import luan.Luan;
6 import luan.LuanFunction;
7 import luan.LuanException;
8 import luan.LuanRuntimeException;
9 import luan.modules.Utils;
10 import luan.modules.HtmlLuan;
11 import goodjava.bbcode.BBCode;
12
13
14 public final class BBCodeLuan {
15
16 private static BBCode.Quoter quoter(final Luan luan,final LuanFunction quoterFn) {
17 return new BBCode.Quoter() {
18 public String quote(BBCode.Target target,String text,String param) {
19 try {
20 Object obj = quoterFn.call(luan,text,param);
21 if( !(obj instanceof String) )
22 throw new LuanException("BBCode quoter function returned "+Luan.type(obj)+" but string required");
23 return (String)obj;
24 } catch(LuanException e) {
25 throw new LuanRuntimeException(e);
26 }
27 }
28 };
29 }
30
31 public static String toHtml(Luan luan,String text,LuanFunction quoterFn) throws LuanException {
32 return parse(luan,text,quoterFn,BBCode.Target.HTML);
33 }
34
35 public static String toText(Luan luan,String text,LuanFunction quoterFn) throws LuanException {
36 return parse(luan,text,quoterFn,BBCode.Target.TEXT);
37 }
38
39 private static String parse(Luan luan,String text,LuanFunction quoterFn,BBCode.Target target)
40 throws LuanException
41 {
42 Utils.checkNotNull(text,1);
43 BBCode bbcode = new BBCode(text);
44 bbcode.target = target;
45 if( quoterFn != null )
46 bbcode.quoter = quoter(luan,quoterFn);
47 try {
48 return bbcode.parse();
49 } catch(LuanRuntimeException e) {
50 throw (LuanException)e.getCause();
51 }
52 }
53
54 }