comparison src/goodjava/bbcode/BBCode.java @ 1714:31a82b0d0a87

bbcode and html work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 18 Jul 2022 23:49:47 -0600
parents 36c28be6d432
children 6c9aea554691
comparison
equal deleted inserted replaced
1713:4d70e9543ef2 1714:31a82b0d0a87
2 2
3 import java.util.List; 3 import java.util.List;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 import java.util.Map; 5 import java.util.Map;
6 import java.util.LinkedHashMap; 6 import java.util.LinkedHashMap;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern; 7 import java.util.regex.Pattern;
9 import goodjava.parser.Parser; 8 import goodjava.parser.Parser;
10 9
11 10
12 public final class BBCode { 11 public final class BBCode {
274 private Element parseColor() { 273 private Element parseColor() {
275 parser.begin(); 274 parser.begin();
276 if( !parser.matchIgnoreCase("[color=") ) 275 if( !parser.matchIgnoreCase("[color=") )
277 return parser.failure(null); 276 return parser.failure(null);
278 int start = parser.currentIndex(); 277 int start = parser.currentIndex();
279 parser.match('#');
280 while( parser.inCharRange('0','9') 278 while( parser.inCharRange('0','9')
281 || parser.inCharRange('a','z') 279 || parser.inCharRange('a','z')
282 || parser.inCharRange('A','Z') 280 || parser.inCharRange('A','Z')
281 || parser.anyOf("#(, )")
283 ); 282 );
284 String color = parser.textFrom(start); 283 String color = parser.textFrom(start);
285 if( !parser.match(']') ) 284 if( !parser.match(']') )
286 return parser.failure(null); 285 return parser.failure(null);
287 Object content = parseUntil("[/color]"); 286 Object content = parseUntil("[/color]");
295 parser.begin(); 294 parser.begin();
296 if( !parser.matchIgnoreCase("[size=") ) 295 if( !parser.matchIgnoreCase("[size=") )
297 return parser.failure(null); 296 return parser.failure(null);
298 int start = parser.currentIndex(); 297 int start = parser.currentIndex();
299 while( parser.match('.') || parser.inCharRange('0','9') ); 298 while( parser.match('.') || parser.inCharRange('0','9') );
299 if( parser.matchIgnoreCase("pt") || parser.match('%') )
300 ; // ok
300 String size = parser.textFrom(start); 301 String size = parser.textFrom(start);
301 if( !parser.match(']') ) 302 if( !parser.match(']') )
302 return parser.failure(null); 303 return parser.failure(null);
303 Object content = parseUntil("[/size]"); 304 Object content = parseUntil("[/size]");
304 if( content==null ) 305 if( content==null )
305 return parser.failure(null); 306 return parser.failure(null);
306 Element rtn = new Element("size",size,content); 307 Element rtn = new Element("size",size,content);
307 return parser.success(rtn); 308 return parser.success(rtn);
308 } 309 }
309 310
310 private static final Pattern youtubePtn1 = Pattern.compile("https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?");
311 private static final Pattern youtubePtn2 = Pattern.compile("https://www.youtube.com/watch?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?");
312 private static final Pattern bitchutePtn = Pattern.compile("https://www.bitchute.com/video/([a-zA-Z0-9]+)/");
313
314 private static Matcher find(Pattern ptn,String s) {
315 Matcher m = ptn.matcher(s);
316 return m.find() ? m : null;
317 }
318
319 private Element parseVideo() { 311 private Element parseVideo() {
320 parser.begin(); 312 parser.begin();
321 if( !parser.matchIgnoreCase("[video]") ) 313 if( !parser.matchIgnoreCase("[video]") )
322 return parser.failure(null); 314 return parser.failure(null);
323 String url = parseRealUrl(); 315 String url = parseRealUrl();
324 if( !parser.matchIgnoreCase("[/video]") ) 316 if( !parser.matchIgnoreCase("[/video]") )
325 return parser.failure(null); 317 return parser.failure(null);
326 Map<String,String> extra = new LinkedHashMap<String,String>(); 318 Element rtn = new Element("video",null,url);
327 Element rtn = new Element("video",null,url,extra);
328 Matcher m;
329 m = find(youtubePtn1,url);
330 if( m == null )
331 m = find(youtubePtn2,url);
332 if( m != null ) {
333 extra.put( "site", "youtube" );
334 extra.put( "id", m.group(1) );
335 String t = m.group(2);
336 if( t != null )
337 extra.put( "start", t );
338 return parser.success(rtn);
339 }
340 m = find(bitchutePtn,url);
341 if( m != null ) {
342 extra.put( "site", "bitchute" );
343 extra.put( "id", m.group(1) );
344 return parser.success(rtn);
345 }
346 return parser.success(rtn); 319 return parser.success(rtn);
347 } 320 }
348 321
349 322
350 private Element parseQuote() { 323 private Element parseQuote() {