view src/goodjava/bbcode/BBCode.java @ 1708:8d257d56dc42

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Jul 2022 14:06:59 -0600
parents a6e27c8e7ef4
children 05d14db623b6
line wrap: on
line source

package goodjava.bbcode;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import goodjava.parser.Parser;


public final class BBCode {

	public final class Element {
		public final String name;
		public final String param;
		public final Object contents;  // String, Element, or List
		public final Map<String,String> extra;

		private Element(String name,Object contents) {
			this(name,null,contents);
		}

		private Element(String name,String param,Object contents) {
			this(name,param,contents,null);
		}

		private Element(String name,String param,Object contents,Map<String,String> extra) {
			this.name = name.toLowerCase();
			this.param = param;
			this.contents = contents;
			this.extra = extra;
		}
	}

	public static Object parse(String text) {
		if( text.indexOf('[') == -1 )
			return text;
		return new BBCode(text).parse();
	}

	private final Parser parser;

	private BBCode(String text) {
		this.parser = new Parser(text);
	}

	private Object parse() {
		List list = new ArrayList();
		StringBuilder text = new StringBuilder();
		while( !parser.endOfInput() ) {
			Element block = parseBlock();
			if( block != null ) {
				add(list,text);
				list.add( block );
			} else {
				text.append( parser.currentChar() );
				parser.anyChar();
			}
		}
		add(list,text);
		return list.size()==1 ? list.get(0) : list;
	}

	private Object parseWellFormed() {
		List list = new ArrayList();
		StringBuilder text = new StringBuilder();
		while( !parser.endOfInput() ) {
			Element block = parseBlock();
			if( block != null ) {
				add(list,text);
				list.add( block );
				continue;
			}
			if( couldBeTag() )
				break;
			text.append( parser.currentChar() );
			parser.anyChar();
		}
		add(list,text);
		return list.size()==1 ? list.get(0) : list;
	}

	private void add(List list,StringBuilder text) {
		if( text.length() > 0 ) {
			list.add( text.toString() );
			text.setLength(0);
		}
	}

	private boolean couldBeTag() {
		if( parser.currentChar() != '[' )
			return false;
		return parser.testIgnoreCase("[b]")
			|| parser.testIgnoreCase("[/b]")
			|| parser.testIgnoreCase("[i]")
			|| parser.testIgnoreCase("[/i]")
			|| parser.testIgnoreCase("[u]")
			|| parser.testIgnoreCase("[/u]")
			|| parser.testIgnoreCase("[url]")
			|| parser.testIgnoreCase("[url=")
			|| parser.testIgnoreCase("[/url]")
			|| parser.testIgnoreCase("[code]")
			|| parser.testIgnoreCase("[/code]")
			|| parser.testIgnoreCase("[img]")
			|| parser.testIgnoreCase("[/img]")
			|| parser.testIgnoreCase("[color=")
			|| parser.testIgnoreCase("[/color]")
			|| parser.testIgnoreCase("[size=")
			|| parser.testIgnoreCase("[/size]")
			|| parser.testIgnoreCase("[video]")
			|| parser.testIgnoreCase("[/video]")
			|| parser.testIgnoreCase("[quote]")
			|| parser.testIgnoreCase("[quote=")
			|| parser.testIgnoreCase("[/quote]")
		;
	}

	private Element parseBlock() {
		if( parser.currentChar() != '[' )
			return null;
		Element s;
		s = parseB();  if(s!=null) return s;
		s = parseI();  if(s!=null) return s;
		s = parseU();  if(s!=null) return s;
		s = parseUrl1();  if(s!=null) return s;
		s = parseUrl2();  if(s!=null) return s;
		s = parseCode();  if(s!=null) return s;
		s = parseImg();  if(s!=null) return s;
		s = parseColor();  if(s!=null) return s;
		s = parseSize();  if(s!=null) return s;
		s = parseVideo();  if(s!=null) return s;
		s = parseQuote1();  if(s!=null) return s;
		s = parseQuote2();  if(s!=null) return s;
		return null;
	}

	private Element parseB() {
		parser.begin();
		if( !parser.matchIgnoreCase("[b]") )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/b]") )
			return parser.failure(null);
		Element rtn = new Element("b",content);
		return parser.success(rtn);
	}

	private Element parseI() {
		parser.begin();
		if( !parser.matchIgnoreCase("[i]") )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/i]") )
			return parser.failure(null);
		Element rtn = new Element("i",content);
		return parser.success(rtn);
	}

	private Element parseU() {
		parser.begin();
		if( !parser.matchIgnoreCase("[u]") )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/u]") )
			return parser.failure(null);
		Element rtn = new Element("u",content);
		return parser.success(rtn);
	}

	private Element parseUrl1() {
		parser.begin();
		if( !parser.matchIgnoreCase("[url]") )
			return parser.failure(null);
		String url = parseRealUrl();
		if( !parser.matchIgnoreCase("[/url]") )
			return parser.failure(null);
		Element rtn = new Element("url",url);
		return parser.success(rtn);
	}

	private Element parseUrl2() {
		parser.begin();
		if( !parser.matchIgnoreCase("[url=") )
			return parser.failure(null);
		String url = parseRealUrl();
		if( !parser.match(']') )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/url]") )
			return parser.failure(null);
		Element rtn = new Element("url",url,content);
		return parser.success(rtn);
	}

	private String parseRealUrl() {
		parser.begin();
		while( parser.match(' ') );
		int start = parser.currentIndex();
		if( !parser.matchIgnoreCase("http") )
			return parser.failure(null);
		parser.matchIgnoreCase("s");
		if( !parser.matchIgnoreCase("://") )
			return parser.failure(null);
		while( parser.noneOf(" []'") );
		String url = parser.textFrom(start);
		while( parser.match(' ') );
		return parser.success(url);
	}

	private Element parseCode() {
		parser.begin();
		if( !parser.matchIgnoreCase("[code]") )
			return parser.failure(null);
		int start = parser.currentIndex();
		while( !parser.testIgnoreCase("[/code]") ) {
			if( !parser.anyChar() )
				return parser.failure(null);
		}
		String content = parser.textFrom(start);
		if( !parser.matchIgnoreCase("[/code]") ) throw new RuntimeException();
		Element rtn = new Element("code",content);
		return parser.success(rtn);
	}

	private Element parseImg() {
		parser.begin();
		if( !parser.matchIgnoreCase("[img]") )
			return parser.failure(null);
		String url = parseRealUrl();
		if( !parser.matchIgnoreCase("[/img]") )
			return parser.failure(null);
		Element rtn = new Element("img",url);
		return parser.success(rtn);
	}

	private Element parseColor() {
		parser.begin();
		if( !parser.matchIgnoreCase("[color=") )
			return parser.failure(null);
		int start = parser.currentIndex();
		parser.match('#');
		while( parser.inCharRange('0','9')
			|| parser.inCharRange('a','z')
			|| parser.inCharRange('A','Z')
		);
		String color = parser.textFrom(start);
		if( !parser.match(']') )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/color]") )
			return parser.failure(null);
		Element rtn = new Element("color",color,content);
		return parser.success(rtn);
	}

	private Element parseSize() {
		parser.begin();
		if( !parser.matchIgnoreCase("[size=") )
			return parser.failure(null);
		int start = parser.currentIndex();
		while( parser.match('.') || parser.inCharRange('0','9') );
		String size = parser.textFrom(start);
		if( !parser.match(']') )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/size]") )
			return parser.failure(null);
		Element rtn = new Element("size",size,content);
		return parser.success(rtn);
	}

	private static final Pattern youtubePtn1 = Pattern.compile("https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?");
	private static final Pattern youtubePtn2 = Pattern.compile("https://www.youtube.com/watch?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?");
	private static final Pattern bitchutePtn = Pattern.compile("https://www.bitchute.com/video/([a-zA-Z0-9]+)/");

	private static Matcher find(Pattern ptn,String s) {
		Matcher m = ptn.matcher(s);
		return m.find() ? m : null;
	}

	private Element parseVideo() {
		parser.begin();
		if( !parser.matchIgnoreCase("[video]") )
			return parser.failure(null);
		String url = parseRealUrl();
		if( !parser.matchIgnoreCase("[/video]") )
			return parser.failure(null);
		Map<String,String> extra = new LinkedHashMap<String,String>();
		Element rtn = new Element("video",null,url,extra);
		Matcher m;
		m = find(youtubePtn1,url);
		if( m == null )
			m = find(youtubePtn2,url);
		if( m != null ) {
			extra.put( "site", "youtube" );
			extra.put( "id", m.group(1) );
			String t = m.group(2);
			if( t != null )
				extra.put( "start", t );
			return parser.success(rtn);
		}
		m = find(bitchutePtn,url);
		if( m != null ) {
			extra.put( "site", "bitchute" );
			extra.put( "id", m.group(1) );
			return parser.success(rtn);
		}
		return parser.success(rtn);
	}


	private Element parseQuote1() {
		parser.begin();
		if( !parser.matchIgnoreCase("[quote]") )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/quote]") )
			return parser.failure(null);
		Element rtn = new Element("quote",content);
		return parser.success(rtn);
	}

	private Element parseQuote2() {
		parser.begin();
		if( !parser.matchIgnoreCase("[quote=") )
			return parser.failure(null);
		List args = new ArrayList();
		int start = parser.currentIndex();
		while( parser.noneOf("[]") );
		String name = parser.textFrom(start).trim();
		if( !parser.match(']') )
			return parser.failure(null);
		Object content = parseWellFormed();
		if( !parser.matchIgnoreCase("[/quote]") )
			return parser.failure(null);
		Element rtn = new Element("quote",name,content);
		return parser.success(rtn);
	}

}