Mercurial Hosting > luan
annotate src/luan/modules/parsers/BBCode.java @ 1335:e0cf0d108a77
major cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 14 Feb 2019 03:10:45 -0700 |
parents | 25746915a241 |
children | 27efb1fcbcb5 |
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; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
8 import luan.modules.Utils; |
1247 | 9 import luan.modules.HtmlLuan; |
1111
88b5b81cad4a
move Parser to luan.lib.parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
777
diff
changeset
|
10 import luan.lib.parser.Parser; |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
11 |
585 | 12 |
13 public final class BBCode { | |
14 | |
1335 | 15 public static String toHtml(String bbcode,LuanFunction quoter) throws LuanException { |
16 return new BBCode(bbcode,quoter,true).parse(); | |
585 | 17 } |
18 | |
1335 | 19 public static String toText(String bbcode,LuanFunction quoter) throws LuanException { |
20 return new BBCode(bbcode,quoter,false).parse(); | |
585 | 21 } |
22 | |
23 private final Parser parser; | |
638 | 24 private final LuanFunction quoter; |
585 | 25 private final boolean toHtml; |
26 | |
1335 | 27 private BBCode(String text,LuanFunction quoter,boolean toHtml) throws LuanException { |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
638
diff
changeset
|
28 Utils.checkNotNull(text,1); |
777
1460d297e960
add bbcode to blog example
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
29 // Utils.checkNotNull(quoter,2); |
585 | 30 this.parser = new Parser(text); |
638 | 31 this.quoter = quoter; |
585 | 32 this.toHtml = toHtml; |
33 } | |
34 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
35 private String parse() throws LuanException { |
585 | 36 StringBuilder sb = new StringBuilder(); |
1247 | 37 StringBuilder text = new StringBuilder(); |
585 | 38 while( !parser.endOfInput() ) { |
39 String block = parseBlock(); | |
1247 | 40 if( block != null ) { |
41 sb.append( textToString(text) ); | |
585 | 42 sb.append(block); |
1247 | 43 } else { |
44 text.append( parser.currentChar() ); | |
585 | 45 parser.anyChar(); |
46 } | |
47 } | |
1247 | 48 sb.append( textToString(text) ); |
585 | 49 return sb.toString(); |
50 } | |
51 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
52 private String parseWellFormed() throws LuanException { |
585 | 53 StringBuilder sb = new StringBuilder(); |
1247 | 54 StringBuilder text = new StringBuilder(); |
585 | 55 while( !parser.endOfInput() ) { |
56 String block = parseBlock(); | |
57 if( block != null ) { | |
1247 | 58 sb.append( textToString(text) ); |
585 | 59 sb.append(block); |
60 continue; | |
61 } | |
62 if( couldBeTag() ) | |
63 break; | |
1247 | 64 text.append( parser.currentChar() ); |
585 | 65 parser.anyChar(); |
66 } | |
1247 | 67 sb.append( textToString(text) ); |
585 | 68 return sb.toString(); |
69 } | |
70 | |
1247 | 71 private String textToString(StringBuilder text) throws LuanException { |
72 String s = text.toString(); | |
73 text.setLength(0); | |
74 if( toHtml ) | |
75 s = HtmlLuan.encode(s); | |
76 return s; | |
77 } | |
78 | |
585 | 79 private boolean couldBeTag() { |
80 if( parser.currentChar() != '[' ) | |
81 return false; | |
82 return parser.testIgnoreCase("[b]") | |
83 || parser.testIgnoreCase("[/b]") | |
84 || parser.testIgnoreCase("[i]") | |
85 || parser.testIgnoreCase("[/i]") | |
86 || parser.testIgnoreCase("[u]") | |
87 || parser.testIgnoreCase("[/u]") | |
88 || parser.testIgnoreCase("[url]") | |
89 || parser.testIgnoreCase("[url=") | |
90 || parser.testIgnoreCase("[/url]") | |
91 || parser.testIgnoreCase("[code]") | |
92 || parser.testIgnoreCase("[/code]") | |
93 || parser.testIgnoreCase("[img]") | |
94 || parser.testIgnoreCase("[/img]") | |
95 || parser.testIgnoreCase("[color=") | |
96 || parser.testIgnoreCase("[/color]") | |
97 || parser.testIgnoreCase("[size=") | |
98 || parser.testIgnoreCase("[/size]") | |
99 || parser.testIgnoreCase("[youtube]") | |
100 || parser.testIgnoreCase("[/youtube]") | |
101 || parser.testIgnoreCase("[quote]") | |
102 || parser.testIgnoreCase("[quote=") | |
103 || parser.testIgnoreCase("[/quote]") | |
104 ; | |
105 } | |
106 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
107 private String parseBlock() throws LuanException { |
585 | 108 if( parser.currentChar() != '[' ) |
109 return null; | |
110 String s; | |
111 s = parseB(); if(s!=null) return s; | |
112 s = parseI(); if(s!=null) return s; | |
113 s = parseU(); if(s!=null) return s; | |
114 s = parseUrl1(); if(s!=null) return s; | |
115 s = parseUrl2(); if(s!=null) return s; | |
116 s = parseCode(); if(s!=null) return s; | |
117 s = parseImg(); if(s!=null) return s; | |
118 s = parseColor(); if(s!=null) return s; | |
119 s = parseSize(); if(s!=null) return s; | |
120 s = parseYouTube(); if(s!=null) return s; | |
121 s = parseQuote1(); if(s!=null) return s; | |
122 s = parseQuote2(); if(s!=null) return s; | |
123 return null; | |
124 } | |
125 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
126 private String parseB() throws LuanException { |
585 | 127 parser.begin(); |
128 if( !parser.matchIgnoreCase("[b]") ) | |
129 return parser.failure(null); | |
130 String content = parseWellFormed(); | |
131 if( !parser.matchIgnoreCase("[/b]") ) | |
132 return parser.failure(null); | |
133 String rtn = toHtml ? "<b>"+content+"</b>" : content; | |
134 return parser.success(rtn); | |
135 } | |
136 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
137 private String parseI() throws LuanException { |
585 | 138 parser.begin(); |
139 if( !parser.matchIgnoreCase("[i]") ) | |
140 return parser.failure(null); | |
141 String content = parseWellFormed(); | |
142 if( !parser.matchIgnoreCase("[/i]") ) | |
143 return parser.failure(null); | |
144 String rtn = toHtml ? "<i>"+content+"</i>" : content; | |
145 return parser.success(rtn); | |
146 } | |
147 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
148 private String parseU() throws LuanException { |
585 | 149 parser.begin(); |
150 if( !parser.matchIgnoreCase("[u]") ) | |
151 return parser.failure(null); | |
152 String content = parseWellFormed(); | |
153 if( !parser.matchIgnoreCase("[/u]") ) | |
154 return parser.failure(null); | |
155 String rtn = toHtml ? "<u>"+content+"</u>" : content; | |
156 return parser.success(rtn); | |
157 } | |
158 | |
159 private String parseUrl1() { | |
160 parser.begin(); | |
161 if( !parser.matchIgnoreCase("[url]") ) | |
162 return parser.failure(null); | |
163 String url = parseRealUrl(); | |
164 if( !parser.matchIgnoreCase("[/url]") ) | |
165 return parser.failure(null); | |
166 String rtn = toHtml ? "<a href='"+url+"'>"+url+"</u>" : url; | |
167 return parser.success(rtn); | |
168 } | |
169 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
170 private String parseUrl2() throws LuanException { |
585 | 171 parser.begin(); |
172 if( !parser.matchIgnoreCase("[url=") ) | |
173 return parser.failure(null); | |
174 String url = parseRealUrl(); | |
175 if( !parser.match(']') ) | |
176 return parser.failure(null); | |
177 String content = parseWellFormed(); | |
178 if( !parser.matchIgnoreCase("[/url]") ) | |
179 return parser.failure(null); | |
180 String rtn = toHtml ? "<a href='"+url+"'>"+content+"</u>" : content; | |
181 return parser.success(rtn); | |
182 } | |
183 | |
184 private String parseRealUrl() { | |
185 parser.begin(); | |
186 while( parser.match(' ') ); | |
187 int start = parser.currentIndex(); | |
188 if( !parser.matchIgnoreCase("http") ) | |
189 return parser.failure(null); | |
190 parser.matchIgnoreCase("s"); | |
191 if( !parser.matchIgnoreCase("://") ) | |
192 return parser.failure(null); | |
193 while( parser.noneOf(" []'") ); | |
194 String url = parser.textFrom(start); | |
195 while( parser.match(' ') ); | |
196 return parser.success(url); | |
197 } | |
198 | |
199 private String parseCode() { | |
200 parser.begin(); | |
201 if( !parser.matchIgnoreCase("[code]") ) | |
202 return parser.failure(null); | |
203 int start = parser.currentIndex(); | |
204 while( !parser.testIgnoreCase("[/code]") ) { | |
205 if( !parser.anyChar() ) | |
206 return parser.failure(null); | |
207 } | |
208 String content = parser.textFrom(start); | |
209 if( !parser.matchIgnoreCase("[/code]") ) throw new RuntimeException(); | |
210 String rtn = toHtml ? "<code>"+content+"</code>" : content; | |
211 return parser.success(rtn); | |
212 } | |
213 | |
214 private String parseImg() { | |
215 parser.begin(); | |
216 if( !parser.matchIgnoreCase("[img]") ) | |
217 return parser.failure(null); | |
218 String url = parseRealUrl(); | |
219 if( !parser.matchIgnoreCase("[/img]") ) | |
220 return parser.failure(null); | |
221 String rtn = toHtml ? "<img src='"+url+"'>" : ""; | |
222 return parser.success(rtn); | |
223 } | |
224 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
225 private String parseColor() throws LuanException { |
585 | 226 parser.begin(); |
227 if( !parser.matchIgnoreCase("[color=") ) | |
228 return parser.failure(null); | |
229 int start = parser.currentIndex(); | |
230 parser.match('#'); | |
231 while( parser.inCharRange('0','9') | |
232 || parser.inCharRange('a','z') | |
233 || parser.inCharRange('A','Z') | |
234 ); | |
235 String color = parser.textFrom(start); | |
236 if( !parser.match(']') ) | |
237 return parser.failure(null); | |
238 String content = parseWellFormed(); | |
239 if( !parser.matchIgnoreCase("[/color]") ) | |
240 return parser.failure(null); | |
241 String rtn = toHtml ? "<span style='color: "+color+"'>"+content+"</span>" : content; | |
242 return parser.success(rtn); | |
243 } | |
244 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
245 private String parseSize() throws LuanException { |
585 | 246 parser.begin(); |
247 if( !parser.matchIgnoreCase("[size=") ) | |
248 return parser.failure(null); | |
249 int start = parser.currentIndex(); | |
250 while( parser.match('.') || parser.inCharRange('0','9') ); | |
251 String size = parser.textFrom(start); | |
252 if( !parser.match(']') ) | |
253 return parser.failure(null); | |
254 String content = parseWellFormed(); | |
255 if( !parser.matchIgnoreCase("[/size]") ) | |
256 return parser.failure(null); | |
257 String rtn = toHtml ? "<span style='font-size: "+size+"em'>"+content+"</span>" : content; | |
258 return parser.success(rtn); | |
259 } | |
260 | |
261 private String parseYouTube() { | |
262 parser.begin(); | |
263 if( !parser.matchIgnoreCase("[youtube]") ) | |
264 return parser.failure(null); | |
265 int start = parser.currentIndex(); | |
266 while( parser.inCharRange('0','9') | |
267 || parser.inCharRange('a','z') | |
268 || parser.inCharRange('A','Z') | |
269 || parser.match('-') | |
270 || parser.match('_') | |
271 ); | |
272 String id = parser.textFrom(start); | |
273 if( id.length()==0 || !parser.matchIgnoreCase("[/youtube]") ) | |
274 return parser.failure(null); | |
275 String rtn = toHtml ? "<iframe width='420' height='315' src='https://www.youtube.com/embed/"+id+"' frameborder='0' allowfullscreen></iframe>" : ""; | |
276 return parser.success(rtn); | |
277 } | |
278 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
279 private String quote(Object... args) throws LuanException { |
1248
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
280 if( quoter==null ) { |
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
281 if( toHtml ) |
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
282 throw new LuanException("BBCode quoter function not defined"); |
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
283 else |
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
284 return ""; |
475905984870
improve lucene highlighter and allow bbcode_to_text quoter to be nil
Franklin Schmidt <fschmidt@gmail.com>
parents:
1247
diff
changeset
|
285 } |
1335 | 286 Object obj = quoter.call(args); |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
287 if( !(obj instanceof String) ) |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
638
diff
changeset
|
288 throw new LuanException("BBCode quoter function returned "+Luan.type(obj)+" but string required"); |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
289 return (String)obj; |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
290 } |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
291 |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
292 private String parseQuote1() throws LuanException { |
585 | 293 parser.begin(); |
294 if( !parser.matchIgnoreCase("[quote]") ) | |
295 return parser.failure(null); | |
296 String content = parseWellFormed(); | |
297 if( !parser.matchIgnoreCase("[/quote]") ) | |
298 return parser.failure(null); | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
299 String rtn = quote(content); |
585 | 300 return parser.success(rtn); |
301 } | |
302 | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
303 private String parseQuote2() throws LuanException { |
585 | 304 parser.begin(); |
305 if( !parser.matchIgnoreCase("[quote=") ) | |
306 return parser.failure(null); | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
307 List args = new ArrayList(); |
585 | 308 int start = parser.currentIndex(); |
309 while( parser.noneOf("[];") ); | |
310 String name = parser.textFrom(start).trim(); | |
311 if( name.length() == 0 ) | |
312 return parser.failure(null); | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
313 args.add(name); |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
314 while( parser.match(';') ) { |
585 | 315 start = parser.currentIndex(); |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
316 while( parser.noneOf("[];'") ); |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
317 String src = parser.textFrom(start).trim(); |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
318 args.add(src); |
585 | 319 } |
320 if( !parser.match(']') ) | |
321 return parser.failure(null); | |
322 String content = parseWellFormed(); | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
323 args.add(0,content); |
585 | 324 if( !parser.matchIgnoreCase("[/quote]") ) |
325 return parser.failure(null); | |
637
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
326 String rtn = quote(args.toArray()); |
6ea90dc10375
bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents:
588
diff
changeset
|
327 return parser.success(rtn); |
585 | 328 } |
329 | |
330 } |