Mercurial Hosting > luan
changeset 637:6ea90dc10375
bbcode parser now takes a quoter function
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Mar 2016 12:38:24 -0700 |
parents | 13b390bfed32 |
children | 3ab4f282f0bd |
files | core/src/luan/modules/Utils.java core/src/luan/modules/parsers/BBCode.java |
diffstat | 2 files changed, 74 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
diff -r 13b390bfed32 -r 6ea90dc10375 core/src/luan/modules/Utils.java --- a/core/src/luan/modules/Utils.java Tue Mar 01 07:29:01 2016 -0700 +++ b/core/src/luan/modules/Utils.java Thu Mar 03 12:38:24 2016 -0700 @@ -19,29 +19,49 @@ static final int bufSize = 8192; - private static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException { + private static void checkNotNull(LuanState luan,Object v,String expected,int pos) throws LuanException { if( v == null ) - throw new LuanException(luan,"bad argument #1 ("+expected+" expected, got nil)"); + throw new LuanException(luan,"bad argument #"+pos+" ("+expected+" expected, got nil)"); + } + + public static void checkNotNull(LuanState luan,String s,int pos) throws LuanException { + checkNotNull(luan,s,"string",pos); } public static void checkNotNull(LuanState luan,String s) throws LuanException { - checkNotNull(luan,s,"string"); + checkNotNull(luan,s,1); + } + + public static void checkNotNull(LuanState luan,byte[] b,int pos) throws LuanException { + checkNotNull(luan,b,"binary",pos); } public static void checkNotNull(LuanState luan,byte[] b) throws LuanException { - checkNotNull(luan,b,"binary"); + checkNotNull(luan,b,1); + } + + public static void checkNotNull(LuanState luan,LuanTable t,int pos) throws LuanException { + checkNotNull(luan,t,"table",pos); } public static void checkNotNull(LuanState luan,LuanTable t) throws LuanException { - checkNotNull(luan,t,"table"); + checkNotNull(luan,t,1); + } + + public static void checkNotNull(LuanState luan,Number n,int pos) throws LuanException { + checkNotNull(luan,n,"number",pos); } public static void checkNotNull(LuanState luan,Number n) throws LuanException { - checkNotNull(luan,n,"number"); + checkNotNull(luan,n,1); + } + + public static void checkNotNull(LuanState luan,LuanFunction fn,int pos) throws LuanException { + checkNotNull(luan,fn,"function",pos); } public static void checkNotNull(LuanState luan,LuanFunction fn) throws LuanException { - checkNotNull(luan,fn,"function"); + checkNotNull(luan,fn,1); } public static String readAll(Reader in)
diff -r 13b390bfed32 -r 6ea90dc10375 core/src/luan/modules/parsers/BBCode.java --- a/core/src/luan/modules/parsers/BBCode.java Tue Mar 01 07:29:01 2016 -0700 +++ b/core/src/luan/modules/parsers/BBCode.java Thu Mar 03 12:38:24 2016 -0700 @@ -1,25 +1,39 @@ package luan.modules.parsers; +import java.util.List; +import java.util.ArrayList; +import luan.Luan; +import luan.LuanState; +import luan.LuanFunction; +import luan.LuanException; +import luan.modules.Utils; + public final class BBCode { - public static String toHtml(String bbcode) { - return new BBCode(bbcode,true).parse(); + public static String toHtml(LuanState luan,String bbcode,LuanFunction quoter) throws LuanException { + return new BBCode(luan,bbcode,true,quoter).parse(); } - public static String toText(String bbcode) { - return new BBCode(bbcode,false).parse(); + public static String toText(LuanState luan,String bbcode,LuanFunction quoter) throws LuanException { + return new BBCode(luan,bbcode,false,quoter).parse(); } + private final LuanState luan; private final Parser parser; private final boolean toHtml; + private final LuanFunction quoter; - private BBCode(String text,boolean toHtml) { + private BBCode(LuanState luan,String text,boolean toHtml,LuanFunction quoter) throws LuanException { + Utils.checkNotNull(luan,text,1); + Utils.checkNotNull(luan,quoter,3); + this.luan = luan; this.parser = new Parser(text); this.toHtml = toHtml; + this.quoter = quoter; } - private String parse() { + private String parse() throws LuanException { StringBuilder sb = new StringBuilder(); while( !parser.endOfInput() ) { String block = parseBlock(); @@ -33,7 +47,7 @@ return sb.toString(); } - private String parseWellFormed() { + private String parseWellFormed() throws LuanException { StringBuilder sb = new StringBuilder(); while( !parser.endOfInput() ) { String block = parseBlock(); @@ -77,7 +91,7 @@ ; } - private String parseBlock() { + private String parseBlock() throws LuanException { if( parser.currentChar() != '[' ) return null; String s; @@ -96,7 +110,7 @@ return null; } - private String parseB() { + private String parseB() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[b]") ) return parser.failure(null); @@ -107,7 +121,7 @@ return parser.success(rtn); } - private String parseI() { + private String parseI() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[i]") ) return parser.failure(null); @@ -118,7 +132,7 @@ return parser.success(rtn); } - private String parseU() { + private String parseU() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[u]") ) return parser.failure(null); @@ -140,7 +154,7 @@ return parser.success(rtn); } - private String parseUrl2() { + private String parseUrl2() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[url=") ) return parser.failure(null); @@ -195,7 +209,7 @@ return parser.success(rtn); } - private String parseColor() { + private String parseColor() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[color=") ) return parser.failure(null); @@ -215,7 +229,7 @@ return parser.success(rtn); } - private String parseSize() { + private String parseSize() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[size=") ) return parser.failure(null); @@ -249,50 +263,49 @@ return parser.success(rtn); } - private String parseQuote1() { + private String quote(Object... args) throws LuanException { + Object obj = quoter.call(luan,args); + if( !(obj instanceof String) ) + throw new LuanException(luan,"BBCode quoter function returned "+Luan.type(obj)+" but string required"); + return (String)obj; + } + + private String parseQuote1() throws LuanException { parser.begin(); if( !parser.matchIgnoreCase("[quote]") ) return parser.failure(null); String content = parseWellFormed(); if( !parser.matchIgnoreCase("[/quote]") ) return parser.failure(null); - String rtn = toHtml ? "<blockquote>"+content+"</blockquote>" : ""; + String rtn = quote(content); return parser.success(rtn); } - private String parseQuote2() { + private String parseQuote2() throws LuanException { 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( name.length() == 0 ) return parser.failure(null); - String src = null; - if( parser.match(';') ) { + args.add(name); + while( parser.match(';') ) { start = parser.currentIndex(); - while( parser.noneOf("[]'") ); - src = parser.textFrom(start).trim(); - if( src.length() == 0 ) - return parser.failure(null); + while( parser.noneOf("[];'") ); + String src = parser.textFrom(start).trim(); + args.add(src); } if( !parser.match(']') ) return parser.failure(null); String content = parseWellFormed(); + args.add(0,content); if( !parser.matchIgnoreCase("[/quote]") ) return parser.failure(null); - if( !toHtml ) - return parser.success(""); - StringBuilder sb = new StringBuilder(); - sb.append( "<blockquote><div quoted>" ); - if( src != null ) - sb.append( "<a href='/thread?id=" ).append( src ).append( "'>" ); - sb.append( "<span quoted>" ).append( name ).append( "</span>" ); - if( src != null ) - sb.append( "</a>" ); - sb.append( "</div>" ).append( content ).append( "</blockquote>" ); - return parser.success(sb.toString()); + String rtn = quote(args.toArray()); + return parser.success(rtn); } }