From d0aea9df910f620119e0f591b654ab9e17e0c822 Mon Sep 17 00:00:00 2001 From: Synapticloop Date: Sun, 30 Aug 2015 23:00:55 +0100 Subject: [PATCH] updated tests, addition of gh-pages documentation --- README.md | 118 +----------------- .../templar/function/FunctionLength.java | 2 +- .../templar/token/DumpContextToken.java | 8 ++ .../templar/token/DumpFunctionsToken.java | 8 ++ .../templar/utils/TemplarConfiguration.java | 8 ++ .../render-hashmap-lookup-test.templar | 2 +- 6 files changed, 30 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index 4f4704c..3eabd77 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ templar ======= + +(Documentation now lives here: [http://synapticloop.github.io/templar/](http://synapticloop.github.io/templar/)) + A lightweight java templating engine - and by lightweight we mean a small number of lines of code... (that weights in around 90k): + ``` @@ -43,120 +47,6 @@ We use it as the basis of our code generation tool ```h2zero``` which has now be This will build and run all of the tests, leaving you with a distributable located: here -> ```dist/templar.jar``` -## To Use: - -This example was taken from [sab-spot-comment](http://synapticloop.github.io/sab-spot-comment/): - -In a very small nutshell - - 1. Create a new templarContext - 1. Add stuff to it - 1. Create a parser (and pass in the templar file) - 1. parser.render(templarContext) - -``` -TemplarContext templarContext = new TemplarContext(); - -templarContext.add("setupManager", SetupManager.INSTANCE); - -templarContext.add("dummySuccessComment", new Download("Dummy Success Download", "http://www.example.com/", "SAB_NZB_ID", "", System.currentTimeMillis(), "GUID", false).getComment()); -templarContext.add("dummyFailedComment", new Download("Dummy Failed Download", "http://www.example.com/", "SAB_NZB_ID", "Unpacking failed, archive requires a password", System.currentTimeMillis(), "GUID", false).getComment()); -templarContext.add("dummyIgnoredDownload", new Download("Dummy Ignored Download", "http://www.example.com/", "SAB_NZB_ID", "Unpacking failed, archive requires a password", System.currentTimeMillis(), "GUID", true)); - -try { - Parser parser = new Parser(this.getClass().getResourceAsStream("/synapticloop/ssc/template/admin.templar")); - return(HttpUtils.okResponse(NanoHTTPD.MIME_HTML, parser.render(templarContext))); -} catch (ParseException pex) { - pex.printStackTrace(); - return(HttpUtils.internalServerErrorResponse()); -} catch (RenderException rex) { - rex.printStackTrace(); - return(HttpUtils.internalServerErrorResponse()); -} -``` - -## What are the templating commands? - -This is a very brief overview: - -All commands start with a ```{``` and end with ```}```. - -If you want an actual ```{``` character then you will need to do ```{{```. - -here goes: - - 1. ```{-- some comment here }``` a comment - can be multilined, but the comment cannot contain a ```}``` character - this designates the end of the comment - 1. ```{if ...}``` do something ```{else}``` do something else ```{endif}``` the else token is not necessary - 1. ```{import some/file/here}``` a nice little inclusion method - can also be ```{import classpath:/some/classpath/file/here}``` note that it is from the current working directory and is not relative... (will maybe add this) - 1. ```{set __something__ as __somethingElse}``` ```something``` is the thing to be evaluated and ```somethingElse``` is the variable name to be placed in the context. If you want to set a string as a variable then you need to quote it with either a single (') or double (") quotation mark. - 1. ```{loop __something__ as __somethingElse__}``` loop over stuff placing a variable named ```somethingElse``` in each iteration - don't forget the ```{endloop}``` token - 1. ```{dumpcontext}``` dump all of the key-value pairs in the context - useful for debugging. - 1. ```{dumpfunctions}``` dump all of the functions and their number of arguments - useful for debugging. - 1. ```{nl}``` or ```{\n}``` a new line character - 1. ```{tab}``` or ```{\t}``` a tab character - -and that should be about it. - -for the ```{if}``` and ```{set}``` commands can also take functions - or you can just print out the value of the function itself a la ```{fn:length[array]}``` - -The functions are as follows: - -``` - -// null operators -functionMap.put("null", new FunctionIsNull()); // test whether the passed in parameter is null -functionMap.put("notNull", new FunctionIsNotNull()); // test whether the passed in parameter is not null -functionMap.put("!Null", new FunctionIsNotNull()); // test whether the passed in parameter is not null -functionMap.put("!null", new FunctionIsNotNull());// test whether the passed in parameter is not null - -// boolean function operators -functionMap.put("=", new FunctionEqual()); // test whether the passed in parameters are equal -functionMap.put("equal", new FunctionEqual()); // test whether the passed in parameters are equal -functionMap.put("<>", new FunctionNotEqual()); // test whether the passed in parameters are not equal -functionMap.put("not=", new FunctionNotEqual()); // test whether the passed in parameters are not equal -functionMap.put("!=", new FunctionNotEqual()); // test whether the passed in parameters are not equal -functionMap.put("notEqual", new FunctionNotEqual()); // test whether the passed in parameters are not equal -functionMap.put(">", new FunctionGreaterThan()); // test whether the the first parameter is greater than the second -functionMap.put("gt", new FunctionGreaterThan()); // test whether the the first parameter is greater than the second -functionMap.put(">=", new FunctionGreaterThanEqual()); // test whether the the first parameter is greater than or equal to the second -functionMap.put("gte", new FunctionGreaterThanEqual()); // test whether the the first parameter is greater than or equal to the second -functionMap.put("<", new FunctionLessThan()); // test whether the the first parameter is less than the second -functionMap.put("lt", new FunctionLessThan()); // test whether the the first parameter is less than the second -functionMap.put("<=", new FunctionLessThanEqual()); // test whether the the first parameter is less than or equal to the second -functionMap.put("lte", new FunctionLessThanEqual()); // test whether the the first parameter is less than or equal to than the second - -// size operators -functionMap.put("length", new FunctionLength()); // return the length/size of the passed in parameter -functionMap.put("size", new FunctionSize()); // return the length/size of the passed in parameter - -// date operators -functionMap.put("fmtDate", new FunctionFormatDate()); // format the date with the two parameters date, and format as a string - -// boolean test operators -functionMap.put("false", new FunctionFalse()); // test whether the parameter is false -functionMap.put("true", new FunctionTrue()); // test whether the parameter is true - -// logical operators -functionMap.put("and", new FunctionAnd()); // logical AND function for the two parameters -functionMap.put("&", new FunctionAnd()); // logical AND function for the two parameters -functionMap.put("or", new FunctionOr()); // logical OR function for the two parameters -functionMap.put("|", new FunctionOr()); // logical OR function for the two parameters - -// mathematical operators -functionMap.put("+", new FunctionAdd()); // Mathematical ADDITION of two numbers -functionMap.put("-", new FunctionSubtract()); // Mathematical SUBTRACTION of two numbers -functionMap.put("*", new FunctionMultiply()); // Mathematical MULTIPLICATION of two numbers -functionMap.put("/", new FunctionDivide()); // Mathematical DIVISION of two numbers -functionMap.put("^", new FunctionPower()); // Mathematical EXPONENT of two numbers -functionMap.put("%", new FunctionModulus()); // Mathematical MODULUS of two numbers - -// even and odd -functionMap.put("even", new FunctionEven()); // Test whether the passed in number is even -functionMap.put("odd", new FunctionOdd()); // Test whether the passed in number is odd -``` - -See [src/main/java/synapticloop/templar/function](https://github.com/synapticloop/templar/tree/master/src/main/java/synapticloop/templar/function) for all of the in-built functions and [TemplarContext.java](https://github.com/synapticloop/templar/blob/master/src/main/java/synapticloop/templar/utils/TemplarContext.java) for all of the registrations and aliases. - ## What's with the whitespace (tabs and newlines)? diff --git a/src/main/java/synapticloop/templar/function/FunctionLength.java b/src/main/java/synapticloop/templar/function/FunctionLength.java index 2ed5565..a171d51 100644 --- a/src/main/java/synapticloop/templar/function/FunctionLength.java +++ b/src/main/java/synapticloop/templar/function/FunctionLength.java @@ -27,7 +27,7 @@ public class FunctionLength extends Function { private static final String[] FIELDS = {"size", "length"}; - private static final String[] METHODS = {"getSize", "getLength"}; + private static final String[] METHODS = {"getSize", "getLength", "length"}; public FunctionLength() { super(1); diff --git a/src/main/java/synapticloop/templar/token/DumpContextToken.java b/src/main/java/synapticloop/templar/token/DumpContextToken.java index c36c8d2..7a8b6c8 100644 --- a/src/main/java/synapticloop/templar/token/DumpContextToken.java +++ b/src/main/java/synapticloop/templar/token/DumpContextToken.java @@ -21,6 +21,7 @@ import synapticloop.templar.exception.ParseException; import synapticloop.templar.exception.RenderException; +import synapticloop.templar.utils.ParserHelper; import synapticloop.templar.utils.TemplarContext; import synapticloop.templar.utils.Tokeniser; @@ -29,6 +30,13 @@ public class DumpContextToken extends CommandToken { public DumpContextToken(String value, StringTokenizer stringTokenizer, Tokeniser tokeniser) throws ParseException { super(value, stringTokenizer, tokeniser); + StringBuilder stringBuilder = new StringBuilder(); + + boolean foundEndToken = ParserHelper.didFindEndToken(stringTokenizer, stringBuilder); + + if(!foundEndToken) { + throw new ParseException("Unable to find the closing dump context token '}'", this); + } } @Override diff --git a/src/main/java/synapticloop/templar/token/DumpFunctionsToken.java b/src/main/java/synapticloop/templar/token/DumpFunctionsToken.java index c0429b3..58762fa 100644 --- a/src/main/java/synapticloop/templar/token/DumpFunctionsToken.java +++ b/src/main/java/synapticloop/templar/token/DumpFunctionsToken.java @@ -24,6 +24,7 @@ import synapticloop.templar.exception.ParseException; import synapticloop.templar.exception.RenderException; import synapticloop.templar.function.Function; +import synapticloop.templar.utils.ParserHelper; import synapticloop.templar.utils.TemplarContext; import synapticloop.templar.utils.Tokeniser; @@ -32,6 +33,13 @@ public class DumpFunctionsToken extends CommandToken { public DumpFunctionsToken(String value, StringTokenizer stringTokenizer, Tokeniser tokeniser) throws ParseException { super(value, stringTokenizer, tokeniser); + StringBuilder stringBuilder = new StringBuilder(); + + boolean foundEndToken = ParserHelper.didFindEndToken(stringTokenizer, stringBuilder); + + if(!foundEndToken) { + throw new ParseException("Unable to find the closing dump function token '}'", this); + } } @Override diff --git a/src/main/java/synapticloop/templar/utils/TemplarConfiguration.java b/src/main/java/synapticloop/templar/utils/TemplarConfiguration.java index ac32eda..44d04b6 100644 --- a/src/main/java/synapticloop/templar/utils/TemplarConfiguration.java +++ b/src/main/java/synapticloop/templar/utils/TemplarConfiguration.java @@ -22,6 +22,14 @@ public class TemplarConfiguration { private boolean explicitTabs = true; private boolean ignoreWhitespace = true; + public TemplarConfiguration() { } + + public TemplarConfiguration(boolean explicitNewLines, boolean explicitTabs, boolean ignoreWhitespace) { + this.explicitNewLines = explicitNewLines; + this.explicitTabs = explicitTabs; + this.ignoreWhitespace = ignoreWhitespace; + } + public void setExplicitNewLines(boolean explicitNewLines) { this.explicitNewLines = explicitNewLines; } diff --git a/src/test/template/render-hashmap-lookup-test.templar b/src/test/template/render-hashmap-lookup-test.templar index 5d9ebae..7ea1d32 100644 --- a/src/test/template/render-hashmap-lookup-test.templar +++ b/src/test/template/render-hashmap-lookup-test.templar @@ -9,4 +9,4 @@ {loop session.sessionObjects.keySet as key} {\t}KEY: {key}, VALUE: {session.sessionObjects.$key} {if fn:notNull[session.sessionObjects.$key]}CORRECT{else} FATAL ERROR{endif}{\n} -{endloop} \ No newline at end of file +{endloop}