annotate src/goodjava/bbcode/BBCode.java @ 1713:4d70e9543ef2

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 15 Jul 2022 11:55:27 -0600
parents 36c28be6d432
children 31a82b0d0a87
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.Matcher;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
8 import java.util.regex.Pattern;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1335
diff changeset
9 import goodjava.parser.Parser;
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
10
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public final class BBCode {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
14 private static final Pattern tagPtn = Pattern.compile(
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
15 "\\[(/?[a-zA-Z]+(=[^ \\n\\t\\[\\]]*)?)\\]"
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
18 public static String encode(String s) {
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
19 return tagPtn.matcher(s).replaceAll("[brackets]$1[/brackets]");
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
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
22
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
23 public final class Element {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
24 public final String name;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
25 public final String param;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
26 public final Object contents; // String, Element, or List
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
27 public final Map<String,String> extra;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
28
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
29 private Element(String name,Object contents) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
30 this(name,null,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
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
33 private Element(String name,String param,Object contents) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
34 this(name,param,contents,null);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
35 }
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
36
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
37 private Element(String name,String param,Object contents,Map<String,String> extra) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
38 this.name = name.toLowerCase();
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
39 this.param = param;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
40 this.contents = contents;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
41 this.extra = extra;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
42 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
45 public static Object parse(String text) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
46 if( text.indexOf('[') == -1 )
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
47 return text;
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
48 return new BBCode(text).parse();
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
49 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
51 private final Parser parser;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
52
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
53 private BBCode(String text) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 this.parser = new Parser(text);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
57 private Object parse() {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
58 List list = new ArrayList();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
59 StringBuilder text = new StringBuilder();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 while( !parser.endOfInput() ) {
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
61 Element block = parseBlock(false);
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
62 if( block != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
63 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
64 list.add( block );
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
65 } else {
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
66 text.append( parser.currentChar() );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 parser.anyChar();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
70 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
71 return list.size()==1 ? list.get(0) : list;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
74 private Object parseUntil(String end) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
75 return parseUntil(end,false);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
76 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
77
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
78 private Object parseUntil(String end,boolean inList) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
79 List list = new ArrayList();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
80 StringBuilder text = new StringBuilder();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
81 while( !parser.matchIgnoreCase(end) ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
82 if( parser.endOfInput() )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
83 return null;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
84 Element block = parseBlock(inList);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if( block != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
86 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
87 list.add( block );
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
88 } else {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
89 text.append( parser.currentChar() );
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
90 parser.anyChar();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 }
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
93 add(list,text);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
94 return list.size()==1 ? list.get(0) : list;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
97 private void add(List list,StringBuilder text) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
98 if( text.length() > 0 ) {
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
99 list.add( text.toString() );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
100 text.setLength(0);
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
101 }
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
102 }
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
103
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
104 private Element parseBlock(boolean inList) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 if( parser.currentChar() != '[' )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return null;
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
107 Element s;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 s = parseB(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 s = parseI(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 s = parseU(); if(s!=null) return s;
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
111 s = parseS(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
112 s = parseSup(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
113 s = parseBrackets(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
114 s = parseUrl(); if(s!=null) return s;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 s = parseCode(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 s = parseImg(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 s = parseColor(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 s = parseSize(); if(s!=null) return s;
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
119 s = parseVideo(); if(s!=null) return s;
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
120 s = parseQuote(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
121 s = parseUl(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
122 s = parseOl(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
123 if( inList ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
124 s = parseLi(); if(s!=null) return s;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
125 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 return null;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
129 private Element parseB() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 if( !parser.matchIgnoreCase("[b]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
133 Object content = parseUntil("[/b]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
134 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
136 Element rtn = new Element("b",content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
140 private Element parseI() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 if( !parser.matchIgnoreCase("[i]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
144 Object content = parseUntil("[/i]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
145 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
147 Element rtn = new Element("i",content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
151 private Element parseU() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 if( !parser.matchIgnoreCase("[u]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
155 Object content = parseUntil("[/u]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
156 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
158 Element rtn = new Element("u",content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
162 private Element parseS() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 parser.begin();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
164 if( !parser.matchIgnoreCase("[s]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
165 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
166 Object content = parseUntil("[/s]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
167 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
169 Element rtn = new Element("s",content);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
170 return parser.success(rtn);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
171 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
172
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
173 private Element parseSup() {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
174 parser.begin();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
175 if( !parser.matchIgnoreCase("[sup]") )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
177 Object content = parseUntil("[/sup]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
178 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
179 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
180 Element rtn = new Element("sup",content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
184 private Element parseBrackets() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 parser.begin();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
186 if( !parser.matchIgnoreCase("[brackets]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
187 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
188 Object content = parseUntil("[/brackets]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
189 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
190 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
191 Element rtn = new Element("brackets",content);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
192 return parser.success(rtn);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
193 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
194
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
195 private Element parseUrl() {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
196 parser.begin();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
197 if( !parser.matchIgnoreCase("[url") )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
199 String url;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
200 Object content;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
201 if( parser.match(']') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
202 url = parseRealUrl();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
203 content = null;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
204 if( !parser.matchIgnoreCase("[/url]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
205 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
206 } else if( parser.match('=') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
207 url = parseRealUrl();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
208 if( !parser.match(']') )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
209 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
210 content = parseUntil("[/url]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
211 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
212 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
213 } else
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
215 Element rtn = new Element("url",url,content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219 private String parseRealUrl() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221 while( parser.match(' ') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 if( !parser.matchIgnoreCase("http") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 parser.matchIgnoreCase("s");
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
226 if( !parser.matchIgnoreCase("://") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 return parser.failure(null);
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
228 while( parser.noneOf(" \n\t[]") );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229 String url = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230 while( parser.match(' ') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
231 return parser.success(url);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
234 private Element parseCode() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235 parser.begin();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
236 String param;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
237 String end;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
238 if( !parser.matchIgnoreCase("[code") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
239 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
240 if( parser.match(']') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
241 param = null;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
242 end = "[/code]";
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
243 } else if( parser.match('=') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
244 int start = parser.currentIndex();
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
245 while( parser.noneOf("[]\n") );
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
246 param = parser.textFrom(start);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
247 if( !parser.match(']') )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
248 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
249 end = "[/code=" + param + "]";
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
250 } else
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
251 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252 int start = parser.currentIndex();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
253 while( !parser.testIgnoreCase(end) ) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 if( !parser.anyChar() )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 String content = parser.textFrom(start);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
258 if( !parser.matchIgnoreCase(end) ) throw new RuntimeException();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
259 Element rtn = new Element("code",param,content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
263 private Element parseImg() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 if( !parser.matchIgnoreCase("[img]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 String url = parseRealUrl();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 if( !parser.matchIgnoreCase("[/img]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
270 Element rtn = new Element("img",url);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
274 private Element parseColor() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276 if( !parser.matchIgnoreCase("[color=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 parser.match('#');
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280 while( parser.inCharRange('0','9')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 || parser.inCharRange('a','z')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
282 || parser.inCharRange('A','Z')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
283 );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284 String color = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
286 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
287 Object content = parseUntil("[/color]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
288 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
290 Element rtn = new Element("color",color,content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
291 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
292 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
293
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
294 private Element parseSize() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
295 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
296 if( !parser.matchIgnoreCase("[size=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
297 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
298 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
299 while( parser.match('.') || parser.inCharRange('0','9') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300 String size = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
301 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
302 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
303 Object content = parseUntil("[/size]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
304 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
305 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
306 Element rtn = new Element("size",size,content);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
307 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
308 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
309
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
310 private static final Pattern youtubePtn1 = Pattern.compile("https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?");
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
311 private static final Pattern youtubePtn2 = Pattern.compile("https://www.youtube.com/watch?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?");
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
312 private static final Pattern bitchutePtn = Pattern.compile("https://www.bitchute.com/video/([a-zA-Z0-9]+)/");
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
313
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
314 private static Matcher find(Pattern ptn,String s) {
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
315 Matcher m = ptn.matcher(s);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
316 return m.find() ? m : null;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
317 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
318
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
319 private Element parseVideo() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320 parser.begin();
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
321 if( !parser.matchIgnoreCase("[video]") )
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
322 return parser.failure(null);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
323 String url = parseRealUrl();
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
324 if( !parser.matchIgnoreCase("[/video]") )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
325 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
326 Map<String,String> extra = new LinkedHashMap<String,String>();
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
327 Element rtn = new Element("video",null,url,extra);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
328 Matcher m;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
329 m = find(youtubePtn1,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
330 if( m == null )
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
331 m = find(youtubePtn2,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
332 if( m != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
333 extra.put( "site", "youtube" );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
334 extra.put( "id", m.group(1) );
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
335 String t = m.group(2);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
336 if( t != null )
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
337 extra.put( "start", t );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
338 return parser.success(rtn);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
339 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
340 m = find(bitchutePtn,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
341 if( m != null ) {
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
342 extra.put( "site", "bitchute" );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
343 extra.put( "id", m.group(1) );
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
344 return parser.success(rtn);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
345 }
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
346 return parser.success(rtn);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
347 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
348
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
349
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
350 private Element parseQuote() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
351 parser.begin();
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
352 if( !parser.matchIgnoreCase("[quote") )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
353 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
354 String name;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
355 if( parser.match(']') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
356 name = null;
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
357 } else if( parser.match('=') ) {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
358 int start = parser.currentIndex();
1712
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
359 while( parser.noneOf("[]\n") );
36c28be6d432 improve html and bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1711
diff changeset
360 name = parser.textFrom(start);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
361 if( !parser.match(']') )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
362 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
363 } else
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
364 return parser.failure(null);
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
365 Object content = parseUntil("[/quote]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
366 if( content==null )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
367 return parser.failure(null);
1705
a6e27c8e7ef4 bbcode now returns data
Franklin Schmidt <fschmidt@gmail.com>
parents: 1703
diff changeset
368 Element rtn = new Element("quote",name,content);
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
369 return parser.success(rtn);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
370 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
371
1711
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
372
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
373 private Element parseUl() {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
374 parser.begin();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
375 if( !parser.matchIgnoreCase("[ul]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
376 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
377 Object content = parseUntil("[/ul]",true);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
378 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
379 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
380 Element rtn = new Element("ul",content);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
381 return parser.success(rtn);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
382 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
383
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
384 private Element parseOl() {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
385 parser.begin();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
386 if( !parser.matchIgnoreCase("[ol]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
387 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
388 Object content = parseUntil("[/ol]",true);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
389 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
390 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
391 Element rtn = new Element("ol",content);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
392 return parser.success(rtn);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
393 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
394
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
395 private Element parseLi() {
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
396 parser.begin();
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
397 if( !parser.matchIgnoreCase("[li]") )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
398 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
399 Object content = parseUntil("[/li]");
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
400 if( content==null )
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
401 return parser.failure(null);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
402 Element rtn = new Element("li",content);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
403 return parser.success(rtn);
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
404 }
05d14db623b6 add bbcodes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1705
diff changeset
405
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
406 }