Skip to content

Commit b38a02a

Browse files
committed
Merge branch 'master' of github.com:spullara/mustache.java
2 parents ced640c + 1c0e88f commit b38a02a

8 files changed

Lines changed: 59 additions & 20 deletions

File tree

codegen/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>mustache.java</artifactId>
55
<groupId>com.github.spullara.mustache.java</groupId>
6-
<version>0.8.9-SNAPSHOT</version>
6+
<version>0.8.10-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

@@ -16,7 +16,7 @@
1616
<dependency>
1717
<groupId>com.github.spullara.mustache.java</groupId>
1818
<artifactId>compiler</artifactId>
19-
<version>0.8.9-SNAPSHOT</version>
19+
<version>0.8.10-SNAPSHOT</version>
2020
</dependency>
2121

2222
<!-- ASM -->
@@ -35,7 +35,7 @@
3535
<dependency>
3636
<groupId>com.github.spullara.mustache.java</groupId>
3737
<artifactId>compiler</artifactId>
38-
<version>0.8.9-SNAPSHOT</version>
38+
<version>0.8.10-SNAPSHOT</version>
3939
<classifier>tests</classifier>
4040
<scope>test</scope>
4141
</dependency>

compiler/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>mustache.java</artifactId>
55
<groupId>com.github.spullara.mustache.java</groupId>
6-
<version>0.8.9-SNAPSHOT</version>
6+
<version>0.8.10-SNAPSHOT</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>

compiler/src/main/java/com/github/mustachejava/codes/IterableCode.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,22 @@ public void run() {
8787
protected Writer handleFunction(Writer writer, Function function, Object[] scopes) {
8888
StringWriter sw = new StringWriter();
8989
runIdentity(sw);
90-
Object newtemplate = function.apply(sw.toString());
91-
if (newtemplate != null) {
92-
if (function instanceof TemplateFunction) {
90+
if (function instanceof TemplateFunction) {
91+
Object newtemplate = function.apply(sw.toString());
92+
if (newtemplate != null) {
9393
String templateText = newtemplate.toString();
9494
writer = writeTemplate(writer, templateText, scopes);
95-
} else {
96-
try {
97-
writer.write(newtemplate.toString());
98-
} catch (IOException e) {
99-
throw new MustacheException("Failed to write function result", e);
95+
}
96+
} else {
97+
try {
98+
StringWriter capture = new StringWriter();
99+
writeTemplate(capture, sw.toString(), scopes).close();
100+
Object apply = function.apply(capture.toString());
101+
if (apply != null) {
102+
writer.write(apply.toString());
100103
}
104+
} catch (IOException e) {
105+
throw new MustacheException("Failed to write function result", e);
101106
}
102107
}
103108
return writer;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.github.mustachejava.util.CapturingMustacheVisitor;
55
import com.github.mustachejavabenchmarks.JsonCapturer;
66
import com.github.mustachejavabenchmarks.JsonInterpreterTest;
7+
import com.google.common.base.Function;
78
import junit.framework.TestCase;
89
import org.codehaus.jackson.JsonGenerator;
910
import org.codehaus.jackson.JsonNode;
@@ -351,6 +352,39 @@ public String apply(@Nullable String s) {
351352
assertEquals("This is not interesting.", sw.toString());
352353
}
353354

355+
public void testFunctions() throws IOException {
356+
MustacheFactory c = init();
357+
Mustache m = c.compile(new StringReader("{{#f}}{{foo}}{{/f}}"), "test");
358+
{
359+
StringWriter sw = new StringWriter();
360+
m.execute(sw, new Object() {
361+
Function f = new Function<String, String>() {
362+
@Override
363+
public String apply(String s) {
364+
return s.toUpperCase();
365+
}
366+
};
367+
String foo = "bar";
368+
}).flush();
369+
assertEquals("BAR", sw.toString());
370+
}
371+
{
372+
StringWriter sw = new StringWriter();
373+
m.execute(sw, new Object() {
374+
Function f = new TemplateFunction() {
375+
@Override
376+
public String apply(String s) {
377+
return s.toUpperCase();
378+
}
379+
};
380+
String foo = "bar";
381+
String FOO = "baz";
382+
}).flush();
383+
assertEquals("baz", sw.toString());
384+
}
385+
}
386+
387+
354388
public void testComplex() throws MustacheException, IOException {
355389
StringWriter json = new StringWriter();
356390
MappingJsonFactory jf = new MappingJsonFactory();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public String apply(String input) {
108108
});
109109
put("Section", new Object() {
110110
Function lambda() {
111-
return new Function<String, String>() {
111+
return new TemplateFunction() {
112112
@Override
113113
public String apply(String input) {
114114
return input.equals("{{x}}") ? "yes" : "no";

handlebar/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<parent>
33
<groupId>com.github.spullara.mustache.java</groupId>
44
<artifactId>mustache.java</artifactId>
5-
<version>0.8.9-SNAPSHOT</version>
5+
<version>0.8.10-SNAPSHOT</version>
66
<relativePath>../pom.xml</relativePath>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>com.github.spullara.mustache.java</groupId>
3030
<artifactId>compiler</artifactId>
31-
<version>0.8.9-SNAPSHOT</version>
31+
<version>0.8.10-SNAPSHOT</version>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.codehaus.jackson</groupId>

indy/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<parent>
44
<artifactId>mustache.java</artifactId>
55
<groupId>com.github.spullara.mustache.java</groupId>
6-
<version>0.8.9-SNAPSHOT</version>
6+
<version>0.8.10-SNAPSHOT</version>
77
<relativePath>../pom.xml</relativePath>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<groupId>com.github.spullara.mustache.java</groupId>
1111
<artifactId>indy</artifactId>
12-
<version>0.8.9-SNAPSHOT</version>
12+
<version>0.8.10-SNAPSHOT</version>
1313
<packaging>bundle</packaging>
1414

1515
<name>indy</name>
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>com.github.spullara.mustache.java</groupId>
2222
<artifactId>codegen</artifactId>
23-
<version>0.8.9-SNAPSHOT</version>
23+
<version>0.8.10-SNAPSHOT</version>
2424
</dependency>
2525

2626
<!-- JRuby -->
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>com.github.spullara.mustache.java</groupId>
4949
<artifactId>compiler</artifactId>
50-
<version>0.8.9-SNAPSHOT</version>
50+
<version>0.8.10-SNAPSHOT</version>
5151
<classifier>tests</classifier>
5252
<scope>test</scope>
5353
</dependency>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>com.github.spullara.mustache.java</groupId>
55
<artifactId>mustache.java</artifactId>
6-
<version>0.8.9-SNAPSHOT</version>
6+
<version>0.8.10-SNAPSHOT</version>
77
<description>Implementation of the Mustache language in Java.</description>
88

99
<modules>

0 commit comments

Comments
 (0)