annotate src/goodjava/bbcode/BBCode.java @ 1802:ca98dee04e08

add Parsers.json_null
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 21 Apr 2024 21:25:15 -0600
parents 6c9aea554691
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
1 package goodjava.bbcode;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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;
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
5 import java.util.Map;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
6 import java.util.LinkedHashMap;
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
7 import java.util.regex.Pattern;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1335
diff changeset
8 import goodjava.parser.Parser;
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
9
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public final class BBCode {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
13 private static final Pattern tagPtn = Pattern.compile(
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
14 "\\[(/?[a-zA-Z_]+(=[^ \\n\\t\\[\\]]*)?)\\]"
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
15 );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
16
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
17 public static String encode(String s) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
18 return tagPtn.matcher(s).replaceAll("[brackets]$1[/brackets]");
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
19 }
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
20
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
21
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
22 public final class Element {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
23 public final String name;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
24 public final String param;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
25 public final Object contents; // String, Element, or List
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
27 private Element(String name,String param,Object contents) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
28 this.name = name.toLowerCase();
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
29 this.param = param;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
30 this.contents = contents;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
31 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
34 public static Object parse(String text) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
35 if( text.indexOf('[') == -1 )
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
36 return text;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
37 return new BBCode(text).parse();
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
38 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
40 private final Parser parser;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
41
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
42 private BBCode(String text) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 this.parser = new Parser(text);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
46 private Object parse() {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
47 List list = new ArrayList();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
48 StringBuilder text = new StringBuilder();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 while( !parser.endOfInput() ) {
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
50 Element block = parseBlock();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
51 if( block != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
52 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
53 list.add( block );
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
54 } else {
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
55 text.append( parser.currentChar() );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 parser.anyChar();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
59 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
60 return list.size()==1 ? list.get(0) : list;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
63 private Object parseUntil(String end) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
64 List list = new ArrayList();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
65 StringBuilder text = new StringBuilder();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
66 while( !parser.matchIgnoreCase(end) ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
67 if( parser.endOfInput() )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
68 return null;
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
69 Element block = parseBlock();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 if( block != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
71 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
72 list.add( block );
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
73 } else {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
74 text.append( parser.currentChar() );
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
75 parser.anyChar();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
78 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
79 return list.size()==1 ? list.get(0) : list;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
82 private void add(List list,StringBuilder text) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
83 if( text.length() > 0 ) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
84 list.add( text.toString() );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
85 text.setLength(0);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
86 }
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
87 }
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
88
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
89 private Element parseBlock() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 if( parser.currentChar() != '[' )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 return null;
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
92 parser.begin();
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
93 parser.anyChar(); // [
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
94 int start = parser.currentIndex();
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
95 if( !matchTagChar() )
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
96 return parser.failure(null);
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
97 while(matchTagChar());
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
98 String name = parser.textFrom(start).toLowerCase();
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
99 String param = null;
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
100 if( parser.match('=') ) {
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
101 start = parser.currentIndex();
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
102 while( parser.noneOf("[]\n") );
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
103 param = parser.textFrom(start);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
104 }
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
105 if( !parser.match(']') )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return parser.failure(null);
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
107 Object content = parseUntil("[/"+name+"]");
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
108 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 return parser.failure(null);
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
110 Element rtn = new Element(name,param,content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
114 private boolean matchTagChar() {
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
115 return parser.inCharRange('a','z')
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 || parser.inCharRange('A','Z')
1737
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
117 || parser.inCharRange('0','9')
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
118 || parser.match('_')
6c9aea554691 generalize bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1714
diff changeset
119 ;
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
120 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
121
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 }