Mercurial Hosting > luan
annotate 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 |
rev | line source |
---|---|
585 | 1 package luan.modules.parsers; |
2 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
3 import java.util.List; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
4 import java.util.ArrayList; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
5 import luan.Luan; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
6 import luan.LuanFunction; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
7 import luan.LuanException; |
1702 | 8 import luan.LuanRuntimeException; |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
9 import luan.modules.Utils; |
1247 | 10 import luan.modules.HtmlLuan; |
1702 | 11 import goodjava.bbcode.BBCode; |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
12 |
585 | 13 |
1702 | 14 public final class BBCodeLuan { |
1247 | 15 |
1702 | 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 }; | |
585 | 29 } |
30 | |
1702 | 31 public static String toHtml(Luan luan,String text,LuanFunction quoterFn) throws LuanException { |
32 return parse(luan,text,quoterFn,BBCode.Target.HTML); | |
585 | 33 } |
34 | |
1702 | 35 public static String toText(Luan luan,String text,LuanFunction quoterFn) throws LuanException { |
36 return parse(luan,text,quoterFn,BBCode.Target.TEXT); | |
585 | 37 } |
38 | |
1702 | 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(); | |
1248
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
51 } |
585 | 52 } |
53 | |
54 } |