From 751f671fd62eb314cc7cf2e4659f2d4402bb6482 Mon Sep 17 00:00:00 2001 From: David Loehr Date: Fri, 3 May 2013 11:57:53 -0400 Subject: [PATCH] Reset compiler.delims after parsing a template --- .../com/samskivert/mustache/Mustache.java | 32 +++++++++++++++++-- .../com/samskivert/mustache/MustacheTest.java | 12 +++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index e7a231e..0fa57c8 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -218,8 +218,16 @@ public class Mustache * Compiles the supplied template into a repeatedly executable intermediate form. */ protected static Template compile (Reader source, Compiler compiler) { - Accumulator accum = new Parser(compiler).parse(source); - return new Template(accum.finish(), compiler); + String originalDelims = compiler.delims == null ? null : compiler.delims.toString(); + //compiler.delims.start1 + try { + Accumulator accum = new Parser(compiler).parse(source); + return new Template(accum.finish(), compiler); + } finally { + if (originalDelims != null) { + compiler.delims.updateDelims(originalDelims); + } + } } private Mustache () {} // no instantiateski @@ -421,6 +429,26 @@ public class Mustache public boolean isStaches () { return start1 == '{' && start2 == '{' && end1 == '}' && end2 == '}'; } + + /** + * Returns a String representation that is compatible with + * {@link #updateDelims(java.lang.String)}. + * @return + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append(start1); + if (start2 != NO_CHAR) { + builder.append(start2); + } + builder.append(" "); + builder.append(end1); + if (end2 != NO_CHAR) { + builder.append(end2); + } + return builder.toString(); + } public Delims updateDelims (String dtext) { String[] delims = dtext.split(" "); diff --git a/src/test/java/com/samskivert/mustache/MustacheTest.java b/src/test/java/com/samskivert/mustache/MustacheTest.java index 931b548..13b2bb2 100644 --- a/src/test/java/com/samskivert/mustache/MustacheTest.java +++ b/src/test/java/com/samskivert/mustache/MustacheTest.java @@ -488,6 +488,18 @@ public class MustacheTest String nullvar = null; }); } + + @Test public void testCompilingDoesntChangeCompilersDelimiters() { + Mustache.Compiler compiler = Mustache.compiler(); + test(compiler, + "value", "{{=<% %>=}}<% variable %>", new Object() { + String variable = "value"; + }); + test(compiler, + "value", "{{=<% %>=}}<% variable %>", new Object() { + String variable = "value"; + }); + } @Test public void testLambda1 () { test("Willy is awesome.", "{{#bold}}{{name}} is awesome.{{/bold}}",