Skip to content

Commit d8936b4

Browse files
committed
add support for more esoteric changes of mustache delimiters
1 parent 082d493 commit d8936b4

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

compiler/src/main/java/com/github/mustachejava/MustacheParser.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,28 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
9090
if (c == sm.charAt(0)) {
9191
br.mark(1);
9292
if (sm.length() == 1 || br.read() == sm.charAt(1)) {
93+
// If it is a delimiter change we need to specially handle it
9394
// Two mustaches, now capture command
9495
StringBuilder sb = new StringBuilder();
96+
br.mark(1);
97+
c = br.read();
98+
boolean delimiter = c == '=';
99+
if (delimiter) {
100+
sb.append((char) c);
101+
} else {
102+
br.reset();
103+
}
95104
while ((c = br.read()) != -1) {
96105
br.mark(1);
106+
if (delimiter) {
107+
if (c == '=') {
108+
// Reached the end of the definition
109+
delimiter = false;
110+
} else {
111+
sb.append((char) c);
112+
}
113+
continue;
114+
}
97115
if (c == em.charAt(0)) {
98116
if (em.length() > 1) {
99117
if (br.read() == em.charAt(1)) {
@@ -209,14 +227,14 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
209227
case '=':
210228
// Change delimiters
211229
out = write(mv, out, file, currentLine.intValue(), startOfLine);
212-
String delimiters = command.replaceAll("\\s+", "");
213-
int length = delimiters.length();
214-
if (length > 6 || length / 2 * 2 != length) {
230+
String trimmed = command.substring(1).trim();
231+
String[] split = trimmed.split("\\s+");
232+
if (split.length != 2) {
215233
TemplateContext tc = new TemplateContext(sm, em, file, currentLine.get(), startOfLine);
216-
throw new MustacheException("Invalid delimiter string: " + delimiters, tc);
234+
throw new MustacheException("Invalid delimiter string: " + trimmed, tc);
217235
}
218-
sm = delimiters.substring(1, length / 2);
219-
em = delimiters.substring(length / 2, length - 1);
236+
sm = split[0];
237+
em = split[1];
220238
break;
221239
default: {
222240
if (c == -1) {

compiler/src/test/java/com/github/mustachejava/DelimiterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ public void testWithTemplateFunction2() throws IOException {
5656
assertEquals("Hello, ${name}.", sw.toString());
5757
}
5858

59+
@Test
60+
public void testStrSubstitutor() throws IOException {
61+
DefaultMustacheFactory mf = new DefaultMustacheFactory();
62+
Mustache maven = mf.compile(new StringReader("Hello, $<foo>."), "maven", "$<", ">");
63+
StringWriter sw = new StringWriter();
64+
maven.execute(sw, new Object() {
65+
String foo = "Jason";
66+
}).close();
67+
assertEquals("Hello, Jason.", sw.toString());
68+
}
69+
70+
@Test
71+
public void testStrSubstitutor2() throws IOException {
72+
DefaultMustacheFactory mf = new DefaultMustacheFactory();
73+
Mustache maven = mf.compile(new StringReader("{{=$< >=}}Hello, $<foo>."), "maven");
74+
StringWriter sw = new StringWriter();
75+
maven.execute(sw, new Object() {
76+
String foo = "Jason";
77+
}).close();
78+
assertEquals("Hello, Jason.", sw.toString());
79+
}
80+
5981
private static class NoEncodingMustacheFactory extends DefaultMustacheFactory {
6082
@Override
6183
public void encode(String value, Writer writer) {

0 commit comments

Comments
 (0)