annotate src/goodjava/bbcode/BBCode.java @ 1703:3a61451f8130

add bbcode video
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 Jul 2022 00:12:05 -0600
parents 8ad468cc88d4
children a6e27c8e7ef4
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;
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
5 import java.util.regex.Matcher;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
6 import java.util.regex.Pattern;
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1335
diff changeset
7 import goodjava.parser.Parser;
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
8
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public final class BBCode {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
12 public enum Target { HTML, TEXT }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
14 public interface Quoter {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
15 public String quote(Target target,String text,String param);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
18 public static final Quoter defaultQuoter = new Quoter() {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
19 public String quote(Target target,String text,String param) {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
20 StringBuilder sb = new StringBuilder();
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
21 sb.append( "<blockquote>" );
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
22 if( param != null ) {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
23 sb.append( htmlEncode(param) );
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
24 sb.append( " wrote:\n" );
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
25 }
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
26 sb.append( text );
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
27 sb.append( "</blockquote>" );
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
28 return sb.toString();
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
29 }
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
30 };
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
32 public Target target = Target.HTML;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
33 public Quoter quoter = defaultQuoter;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
34 private final Parser parser;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
35
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
36 public BBCode(String text) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 this.parser = new Parser(text);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 }
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 public String parse() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 StringBuilder sb = new StringBuilder();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
42 StringBuilder text = new StringBuilder();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 while( !parser.endOfInput() ) {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 String block = parseBlock();
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
45 if( block != null ) {
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
46 sb.append( textToString(text) );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 sb.append(block);
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
48 } else {
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
49 text.append( parser.currentChar() );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 parser.anyChar();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 }
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
53 sb.append( textToString(text) );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 return sb.toString();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
57 private String parseWellFormed() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 StringBuilder sb = new StringBuilder();
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() ) {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 String block = parseBlock();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 if( block != null ) {
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
63 sb.append( textToString(text) );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 sb.append(block);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 continue;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 if( couldBeTag() )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 break;
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
69 text.append( parser.currentChar() );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 parser.anyChar();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
72 sb.append( textToString(text) );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 return sb.toString();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
76 private String textToString(StringBuilder text) {
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
77 String s = text.toString();
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
78 text.setLength(0);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
79 if( target == Target.HTML )
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
80 s = htmlEncode(s);
1247
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
81 return s;
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
82 }
728d8e75f476 html encode bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
83
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 private boolean couldBeTag() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if( parser.currentChar() != '[' )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 return false;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 return parser.testIgnoreCase("[b]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 || parser.testIgnoreCase("[/b]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 || parser.testIgnoreCase("[i]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 || parser.testIgnoreCase("[/i]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 || parser.testIgnoreCase("[u]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 || parser.testIgnoreCase("[/u]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 || parser.testIgnoreCase("[url]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 || parser.testIgnoreCase("[url=")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 || parser.testIgnoreCase("[/url]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 || parser.testIgnoreCase("[code]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 || parser.testIgnoreCase("[/code]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 || parser.testIgnoreCase("[img]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 || parser.testIgnoreCase("[/img]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 || parser.testIgnoreCase("[color=")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 || parser.testIgnoreCase("[/color]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 || parser.testIgnoreCase("[size=")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 || parser.testIgnoreCase("[/size]")
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
104 || parser.testIgnoreCase("[video]")
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
105 || parser.testIgnoreCase("[/video]")
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 || parser.testIgnoreCase("[quote]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 || parser.testIgnoreCase("[quote=")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 || parser.testIgnoreCase("[/quote]")
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 ;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
112 private String parseBlock() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 if( parser.currentChar() != '[' )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 return null;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 String s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 s = parseB(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 s = parseI(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 s = parseU(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 s = parseUrl1(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 s = parseUrl2(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 s = parseCode(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 s = parseImg(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 s = parseColor(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 s = parseSize(); if(s!=null) return s;
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
125 s = parseVideo(); if(s!=null) return s;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 s = parseQuote1(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 s = parseQuote2(); if(s!=null) return s;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 return null;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
131 private String parseB() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 if( !parser.matchIgnoreCase("[b]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 if( !parser.matchIgnoreCase("[/b]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
138 String rtn = target==Target.HTML ? "<b>"+content+"</b>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
142 private String parseI() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 if( !parser.matchIgnoreCase("[i]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 if( !parser.matchIgnoreCase("[/i]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
149 String rtn = target==Target.HTML ? "<i>"+content+"</i>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
153 private String parseU() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 if( !parser.matchIgnoreCase("[u]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 if( !parser.matchIgnoreCase("[/u]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
160 String rtn = target==Target.HTML ? "<u>"+content+"</u>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 private String parseUrl1() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 if( !parser.matchIgnoreCase("[url]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 String url = parseRealUrl();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 if( !parser.matchIgnoreCase("[/url]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 return parser.failure(null);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
171 String rtn = target==Target.HTML ? "<a href='" + url + "'>" + url + "</a>" : url;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
175 private String parseUrl2() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 if( !parser.matchIgnoreCase("[url=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 String url = parseRealUrl();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 if( !parser.matchIgnoreCase("[/url]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 return parser.failure(null);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
185 String rtn = target==Target.HTML ? "<a href='" + url + "'>" + content + "</a>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 private String parseRealUrl() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 while( parser.match(' ') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 if( !parser.matchIgnoreCase("http") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 parser.matchIgnoreCase("s");
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 if( !parser.matchIgnoreCase("://") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 while( parser.noneOf(" []'") );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 String url = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 while( parser.match(' ') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 return parser.success(url);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 private String parseCode() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 if( !parser.matchIgnoreCase("[code]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 while( !parser.testIgnoreCase("[/code]") ) {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 if( !parser.anyChar() )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 String content = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 if( !parser.matchIgnoreCase("[/code]") ) throw new RuntimeException();
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
215 String rtn = target==Target.HTML ? "<code>"+content+"</code>" : 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 parseImg() {
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 if( !parser.matchIgnoreCase("[img]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 String url = parseRealUrl();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 if( !parser.matchIgnoreCase("[/img]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 return parser.failure(null);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
226 String rtn = target==Target.HTML ? "<img src='" + url + "'>" : "";
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
228 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
230 private String parseColor() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
231 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 if( !parser.matchIgnoreCase("[color=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235 parser.match('#');
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
236 while( parser.inCharRange('0','9')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
237 || parser.inCharRange('a','z')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238 || parser.inCharRange('A','Z')
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
239 );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240 String color = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244 if( !parser.matchIgnoreCase("[/color]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
245 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
246 String rtn = target==Target.HTML ? "<span style='color: "+color+"'>"+content+"</span>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
248 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
250 private String parseSize() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
251 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252 if( !parser.matchIgnoreCase("[size=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 int start = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 while( parser.match('.') || parser.inCharRange('0','9') );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256 String size = parser.textFrom(start);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
259 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 if( !parser.matchIgnoreCase("[/size]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
262 String rtn = target==Target.HTML ? "<span style='font-size: "+size+"em'>"+content+"</span>" : content;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
266 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
267 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
268 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
269
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
270 private static Matcher find(Pattern ptn,String s) {
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
271 Matcher m = ptn.matcher(s);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
272 return m.find() ? m : null;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
273 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
274
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
275 private String parseVideo() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276 parser.begin();
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
277 if( !parser.matchIgnoreCase("[video]") )
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
278 return parser.failure(null);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
279 String url = parseRealUrl();
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
280 if( !parser.matchIgnoreCase("[/video]") )
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 return parser.failure(null);
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
282 if( target==Target.TEXT )
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
283 return parser.success( "" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
284 Matcher m;
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
285 m = find(youtubePtn1,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
286 if( m == null )
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
287 m = find(youtubePtn2,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
288 if( m != null ) {
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
289 StringBuilder sb = new StringBuilder();
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
290 sb.append( "<iframe width='420' height='315' src='https://www.youtube.com/embed/" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
291 sb.append( m.group(1) );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
292 String t = m.group(2);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
293 if( t != null ) {
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
294 sb.append( "?start=" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
295 sb.append( t );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
296 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
297 sb.append( "' frameborder='0' allowfullscreen></iframe>" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
298 return parser.success( sb.toString() );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
299 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
300 m = find(bitchutePtn,url);
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
301 if( m != null ) {
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
302 StringBuilder sb = new StringBuilder();
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
303 sb.append( "<iframe width='420' height='315' scrolling='no' frameborder='0' style='border: none;' src='https://www.bitchute.com/embed/" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
304 sb.append( m.group(1) );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
305 sb.append( "/'></iframe>" );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
306 return parser.success( sb.toString() );
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
307 }
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
308 return parser.success( "<a href='" + url + "'>" + url + "</a>" );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
309 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
310
1703
3a61451f8130 add bbcode video
Franklin Schmidt <fschmidt@gmail.com>
parents: 1702
diff changeset
311
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
312 private String parseQuote1() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
313 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
314 if( !parser.matchIgnoreCase("[quote]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
315 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
316 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
317 if( !parser.matchIgnoreCase("[/quote]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
318 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
319 String rtn = quoter.quote(target,content,null);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320 return parser.success(rtn);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
321 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
322
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
323 private String parseQuote2() {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
324 parser.begin();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
325 if( !parser.matchIgnoreCase("[quote=") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
326 return parser.failure(null);
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
327 List args = new ArrayList();
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
328 int start = parser.currentIndex();
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
329 while( parser.noneOf("[]") );
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
330 String name = parser.textFrom(start).trim();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
331 if( !parser.match(']') )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
332 return parser.failure(null);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
333 String content = parseWellFormed();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
334 if( !parser.matchIgnoreCase("[/quote]") )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
335 return parser.failure(null);
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
336 String rtn = quoter.quote(target,content,name);
637
6ea90dc10375 bbcode parser now takes a quoter function
Franklin Schmidt <fschmidt@gmail.com>
parents: 588
diff changeset
337 return parser.success(rtn);
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
339
1702
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
340 public static String htmlEncode(String s) {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
341 final char[] a = s.toCharArray();
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
342 StringBuilder buf = new StringBuilder();
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
343 for( char c : a ) {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
344 switch(c) {
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
345 case '&':
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
346 buf.append("&amp;");
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
347 break;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
348 case '<':
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
349 buf.append("&lt;");
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
350 break;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
351 case '>':
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
352 buf.append("&gt;");
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
353 break;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
354 case '"':
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
355 buf.append("&quot;");
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
356 break;
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
357 default:
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
358 buf.append(c);
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
359 }
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
360 }
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
361 return buf.toString();
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
362 }
8ad468cc88d4 add goodjava/bbcode
Franklin Schmidt <fschmidt@gmail.com>
parents: 1563
diff changeset
363
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
364 }