comparison src/goodjava/bbcode/BBCode.java @ 1712:36c28be6d432

improve html and bbcode
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Jul 2022 22:14:21 -0600
parents 05d14db623b6
children 31a82b0d0a87
comparison
equal deleted inserted replaced
1711:05d14db623b6 1712:36c28be6d432
8 import java.util.regex.Pattern; 8 import java.util.regex.Pattern;
9 import goodjava.parser.Parser; 9 import goodjava.parser.Parser;
10 10
11 11
12 public final class BBCode { 12 public final class BBCode {
13
14 private static final Pattern tagPtn = Pattern.compile(
15 "\\[(/?[a-zA-Z]+(=[^ \\n\\t\\[\\]]*)?)\\]"
16 );
17
18 public static String encode(String s) {
19 return tagPtn.matcher(s).replaceAll("[brackets]$1[/brackets]");
20 }
21
13 22
14 public final class Element { 23 public final class Element {
15 public final String name; 24 public final String name;
16 public final String param; 25 public final String param;
17 public final Object contents; // String, Element, or List 26 public final Object contents; // String, Element, or List
214 if( !parser.matchIgnoreCase("http") ) 223 if( !parser.matchIgnoreCase("http") )
215 return parser.failure(null); 224 return parser.failure(null);
216 parser.matchIgnoreCase("s"); 225 parser.matchIgnoreCase("s");
217 if( !parser.matchIgnoreCase("://") ) 226 if( !parser.matchIgnoreCase("://") )
218 return parser.failure(null); 227 return parser.failure(null);
219 while( parser.noneOf(" []'") ); 228 while( parser.noneOf(" \n\t[]") );
220 String url = parser.textFrom(start); 229 String url = parser.textFrom(start);
221 while( parser.match(' ') ); 230 while( parser.match(' ') );
222 return parser.success(url); 231 return parser.success(url);
223 } 232 }
224 233
231 if( parser.match(']') ) { 240 if( parser.match(']') ) {
232 param = null; 241 param = null;
233 end = "[/code]"; 242 end = "[/code]";
234 } else if( parser.match('=') ) { 243 } else if( parser.match('=') ) {
235 int start = parser.currentIndex(); 244 int start = parser.currentIndex();
236 while( parser.noneOf("[]") ); 245 while( parser.noneOf("[]\n") );
237 param = parser.textFrom(start); 246 param = parser.textFrom(start);
238 if( !parser.match(']') ) 247 if( !parser.match(']') )
239 return parser.failure(null); 248 return parser.failure(null);
240 end = "[/code=" + param + "]"; 249 end = "[/code=" + param + "]";
241 } else 250 } else
345 String name; 354 String name;
346 if( parser.match(']') ) { 355 if( parser.match(']') ) {
347 name = null; 356 name = null;
348 } else if( parser.match('=') ) { 357 } else if( parser.match('=') ) {
349 int start = parser.currentIndex(); 358 int start = parser.currentIndex();
350 while( parser.noneOf("[]") ); 359 while( parser.noneOf("[]\n") );
351 name = parser.textFrom(start).trim(); 360 name = parser.textFrom(start);
352 if( !parser.match(']') ) 361 if( !parser.match(']') )
353 return parser.failure(null); 362 return parser.failure(null);
354 } else 363 } else
355 return parser.failure(null); 364 return parser.failure(null);
356 Object content = parseUntil("[/quote]"); 365 Object content = parseUntil("[/quote]");