Skip to content

Commit a78f208

Browse files
author
Sam Pullara
committed
start pragma support
1 parent 8e49baf commit a78f208

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,46 @@
22

33
import com.github.mustachejava.codes.*;
44

5+
import java.io.Writer;
6+
import java.util.HashMap;
57
import java.util.LinkedList;
68
import java.util.List;
9+
import java.util.Map;
10+
import java.util.logging.Logger;
711

812
/**
913
* The default implementation that builds up Code lists
1014
*/
1115
public class DefaultMustacheVisitor implements MustacheVisitor {
16+
protected static Logger logger = Logger.getLogger(DefaultMustacheVisitor.class.getSimpleName());
17+
1218
private static final Code EOF = new DefaultCode();
1319

1420
protected final List<Code> list = new LinkedList<Code>();
21+
private final Map<String, PragmaHandler> handlers = new HashMap<String, PragmaHandler>() {{
22+
put("implicit-iterator", new PragmaHandler() {
23+
@Override
24+
public Code handle(String pragma, String args) {
25+
return new DefaultCode() {
26+
@Override
27+
public Writer execute(Writer writer, Object[] scopes) {
28+
return super.execute(writer, scopes);
29+
}
30+
};
31+
}
32+
});
33+
}};
34+
1535
protected DefaultMustacheFactory cf;
1636

1737
public DefaultMustacheVisitor(DefaultMustacheFactory cf) {
1838
this.cf = cf;
1939
}
2040

41+
public void addPragmaHandler(String pragma, PragmaHandler handler) {
42+
handlers.put(pragma.toLowerCase(), handler);
43+
}
44+
2145
@Override
2246
public Mustache mustache(TemplateContext templateContext) {
2347
return new DefaultMustache(templateContext, cf, list.toArray(new Code[list.size()]), templateContext.file());
@@ -62,6 +86,20 @@ public void write(TemplateContext templateContext, final String text) {
6286
}
6387
}
6488

89+
@Override
90+
public void pragma(TemplateContext templateContext, String pragma, String args) {
91+
PragmaHandler pragmaHandler = handlers.get(pragma.toLowerCase());
92+
if (pragmaHandler == null) {
93+
// By default, warn that no pragmas are understood
94+
logger.warning("Unimplemented pragma: " + pragma);
95+
} else {
96+
Code code = pragmaHandler.handle(pragma, args);
97+
if (code != null) {
98+
list.add(code);
99+
}
100+
}
101+
}
102+
65103
@Override
66104
public void eof(TemplateContext templateContext) {
67105
list.add(EOF);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,20 @@ protected Mustache compile(final Reader reader, String tag, final AtomicInteger
167167
}
168168
case '%':
169169
// Pragmas
170-
out = write(mv, out, file, currentLine.intValue());
170+
if (!onlywhitespace) {
171+
out = write(mv, out, file, currentLine.intValue());
172+
}
173+
int index = variable.indexOf(" ");
174+
String pragma;
175+
String args;
176+
if (index == -1) {
177+
pragma = variable;
178+
args = null;
179+
} else {
180+
pragma = variable.substring(0, index);
181+
args = variable.substring(index + 1);
182+
}
183+
mv.pragma(new TemplateContext(sm, em, file, currentLine.get()), pragma, args);
171184
break;
172185
case '!':
173186
// Comment

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public interface MustacheVisitor {
1313
void partial(TemplateContext templateContext, String variable);
1414
void value(TemplateContext templateContext, String variable, boolean encoded);
1515
void write(TemplateContext templateContext, String text);
16+
void pragma(TemplateContext templateContext, String pragma, String args);
1617

1718
// Internal
1819
void eof(TemplateContext templateContext);
1920

2021
// Extension
2122
void extend(TemplateContext templateContext, String variable, Mustache mustache);
2223
void name(TemplateContext templateContext, String variable, Mustache mustache);
24+
2325
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.mustachejava;
2+
3+
import com.google.common.base.Optional;
4+
5+
public interface PragmaHandler {
6+
Code handle(String pragma, String args);
7+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ int taxed_value() {
5454
assertEquals(getContents(root, "simple.txt"), sw.toString());
5555
}
5656

57+
public void testSimplePragma() throws MustacheException, IOException, ExecutionException, InterruptedException {
58+
MustacheFactory c = new DefaultMustacheFactory(root);
59+
Mustache m = c.compile("simplepragma.html");
60+
StringWriter sw = new StringWriter();
61+
m.execute(sw, new Object() {
62+
String name = "Chris";
63+
int value = 10000;
64+
65+
int taxed_value() {
66+
return (int) (this.value - (this.value * 0.4));
67+
}
68+
69+
boolean in_ca = true;
70+
});
71+
assertEquals(getContents(root, "simple.txt"), sw.toString());
72+
}
73+
5774
public void testMultipleWrappers() throws MustacheException, IOException, ExecutionException, InterruptedException {
5875
MustacheFactory c = new DefaultMustacheFactory(root);
5976
Mustache m = c.compile("simple.html");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{%IMPLICIT-ITERATOR}}Hello {{name}}
2+
You have just won ${{value}}!
3+
4+
{{#test}}
5+
{{/test}}
6+
{{#in_ca}}
7+
Well, ${{ taxed_value }}, after taxes.{{fred}}
8+
{{/in_ca}}
9+
{{%UNKNOWN-PRAGMA}}

0 commit comments

Comments
 (0)