comparison 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
comparison
equal deleted inserted replaced
1702:8ad468cc88d4 1703:3a61451f8130
1 package goodjava.bbcode; 1 package goodjava.bbcode;
2 2
3 import java.util.List; 3 import java.util.List;
4 import java.util.ArrayList; 4 import java.util.ArrayList;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
5 import goodjava.parser.Parser; 7 import goodjava.parser.Parser;
6 8
7 9
8 public final class BBCode { 10 public final class BBCode {
9 11
97 || parser.testIgnoreCase("[/img]") 99 || parser.testIgnoreCase("[/img]")
98 || parser.testIgnoreCase("[color=") 100 || parser.testIgnoreCase("[color=")
99 || parser.testIgnoreCase("[/color]") 101 || parser.testIgnoreCase("[/color]")
100 || parser.testIgnoreCase("[size=") 102 || parser.testIgnoreCase("[size=")
101 || parser.testIgnoreCase("[/size]") 103 || parser.testIgnoreCase("[/size]")
102 || parser.testIgnoreCase("[youtube]") 104 || parser.testIgnoreCase("[video]")
103 || parser.testIgnoreCase("[/youtube]") 105 || parser.testIgnoreCase("[/video]")
104 || parser.testIgnoreCase("[quote]") 106 || parser.testIgnoreCase("[quote]")
105 || parser.testIgnoreCase("[quote=") 107 || parser.testIgnoreCase("[quote=")
106 || parser.testIgnoreCase("[/quote]") 108 || parser.testIgnoreCase("[/quote]")
107 ; 109 ;
108 } 110 }
118 s = parseUrl2(); if(s!=null) return s; 120 s = parseUrl2(); if(s!=null) return s;
119 s = parseCode(); if(s!=null) return s; 121 s = parseCode(); if(s!=null) return s;
120 s = parseImg(); if(s!=null) return s; 122 s = parseImg(); if(s!=null) return s;
121 s = parseColor(); if(s!=null) return s; 123 s = parseColor(); if(s!=null) return s;
122 s = parseSize(); if(s!=null) return s; 124 s = parseSize(); if(s!=null) return s;
123 s = parseYouTube(); if(s!=null) return s; 125 s = parseVideo(); if(s!=null) return s;
124 s = parseQuote1(); if(s!=null) return s; 126 s = parseQuote1(); if(s!=null) return s;
125 s = parseQuote2(); if(s!=null) return s; 127 s = parseQuote2(); if(s!=null) return s;
126 return null; 128 return null;
127 } 129 }
128 130
164 if( !parser.matchIgnoreCase("[url]") ) 166 if( !parser.matchIgnoreCase("[url]") )
165 return parser.failure(null); 167 return parser.failure(null);
166 String url = parseRealUrl(); 168 String url = parseRealUrl();
167 if( !parser.matchIgnoreCase("[/url]") ) 169 if( !parser.matchIgnoreCase("[/url]") )
168 return parser.failure(null); 170 return parser.failure(null);
169 String rtn = target==Target.HTML ? "<a href='"+url+"'>"+url+"</a>" : url; 171 String rtn = target==Target.HTML ? "<a href='" + url + "'>" + url + "</a>" : url;
170 return parser.success(rtn); 172 return parser.success(rtn);
171 } 173 }
172 174
173 private String parseUrl2() { 175 private String parseUrl2() {
174 parser.begin(); 176 parser.begin();
178 if( !parser.match(']') ) 180 if( !parser.match(']') )
179 return parser.failure(null); 181 return parser.failure(null);
180 String content = parseWellFormed(); 182 String content = parseWellFormed();
181 if( !parser.matchIgnoreCase("[/url]") ) 183 if( !parser.matchIgnoreCase("[/url]") )
182 return parser.failure(null); 184 return parser.failure(null);
183 String rtn = target==Target.HTML ? "<a href='"+url+"'>"+content+"</a>" : content; 185 String rtn = target==Target.HTML ? "<a href='" + url + "'>" + content + "</a>" : content;
184 return parser.success(rtn); 186 return parser.success(rtn);
185 } 187 }
186 188
187 private String parseRealUrl() { 189 private String parseRealUrl() {
188 parser.begin(); 190 parser.begin();
219 if( !parser.matchIgnoreCase("[img]") ) 221 if( !parser.matchIgnoreCase("[img]") )
220 return parser.failure(null); 222 return parser.failure(null);
221 String url = parseRealUrl(); 223 String url = parseRealUrl();
222 if( !parser.matchIgnoreCase("[/img]") ) 224 if( !parser.matchIgnoreCase("[/img]") )
223 return parser.failure(null); 225 return parser.failure(null);
224 String rtn = target==Target.HTML ? "<img src='"+url+"'>" : ""; 226 String rtn = target==Target.HTML ? "<img src='" + url + "'>" : "";
225 return parser.success(rtn); 227 return parser.success(rtn);
226 } 228 }
227 229
228 private String parseColor() { 230 private String parseColor() {
229 parser.begin(); 231 parser.begin();
259 return parser.failure(null); 261 return parser.failure(null);
260 String rtn = target==Target.HTML ? "<span style='font-size: "+size+"em'>"+content+"</span>" : content; 262 String rtn = target==Target.HTML ? "<span style='font-size: "+size+"em'>"+content+"</span>" : content;
261 return parser.success(rtn); 263 return parser.success(rtn);
262 } 264 }
263 265
264 private String parseYouTube() { 266 private static final Pattern youtubePtn1 = Pattern.compile("https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?");
265 parser.begin(); 267 private static final Pattern youtubePtn2 = Pattern.compile("https://www.youtube.com/watch?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?");
266 if( !parser.matchIgnoreCase("[youtube]") ) 268 private static final Pattern bitchutePtn = Pattern.compile("https://www.bitchute.com/video/([a-zA-Z0-9]+)/");
267 return parser.failure(null); 269
268 int start = parser.currentIndex(); 270 private static Matcher find(Pattern ptn,String s) {
269 while( parser.inCharRange('0','9') 271 Matcher m = ptn.matcher(s);
270 || parser.inCharRange('a','z') 272 return m.find() ? m : null;
271 || parser.inCharRange('A','Z') 273 }
272 || parser.match('-') 274
273 || parser.match('_') 275 private String parseVideo() {
274 ); 276 parser.begin();
275 String id = parser.textFrom(start); 277 if( !parser.matchIgnoreCase("[video]") )
276 if( id.length()==0 || !parser.matchIgnoreCase("[/youtube]") ) 278 return parser.failure(null);
277 return parser.failure(null); 279 String url = parseRealUrl();
278 String rtn = target==Target.HTML ? "<iframe width='420' height='315' src='https://www.youtube.com/embed/"+id+"' frameborder='0' allowfullscreen></iframe>" : ""; 280 if( !parser.matchIgnoreCase("[/video]") )
279 return parser.success(rtn); 281 return parser.failure(null);
280 } 282 if( target==Target.TEXT )
283 return parser.success( "" );
284 Matcher m;
285 m = find(youtubePtn1,url);
286 if( m == null )
287 m = find(youtubePtn2,url);
288 if( m != null ) {
289 StringBuilder sb = new StringBuilder();
290 sb.append( "<iframe width='420' height='315' src='https://www.youtube.com/embed/" );
291 sb.append( m.group(1) );
292 String t = m.group(2);
293 if( t != null ) {
294 sb.append( "?start=" );
295 sb.append( t );
296 }
297 sb.append( "' frameborder='0' allowfullscreen></iframe>" );
298 return parser.success( sb.toString() );
299 }
300 m = find(bitchutePtn,url);
301 if( m != null ) {
302 StringBuilder sb = new StringBuilder();
303 sb.append( "<iframe width='420' height='315' scrolling='no' frameborder='0' style='border: none;' src='https://www.bitchute.com/embed/" );
304 sb.append( m.group(1) );
305 sb.append( "/'></iframe>" );
306 return parser.success( sb.toString() );
307 }
308 return parser.success( "<a href='" + url + "'>" + url + "</a>" );
309 }
310
281 311
282 private String parseQuote1() { 312 private String parseQuote1() {
283 parser.begin(); 313 parser.begin();
284 if( !parser.matchIgnoreCase("[quote]") ) 314 if( !parser.matchIgnoreCase("[quote]") )
285 return parser.failure(null); 315 return parser.failure(null);