-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathSpecMustacheVisitor.java
More file actions
63 lines (51 loc) · 1.9 KB
/
SpecMustacheVisitor.java
File metadata and controls
63 lines (51 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.github.mustachejava;
import com.github.mustachejava.codes.PartialCode;
import com.github.mustachejava.codes.ValueCode;
import com.github.mustachejava.util.IndentWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class SpecMustacheVisitor extends DefaultMustacheVisitor {
public SpecMustacheVisitor(DefaultMustacheFactory df) {
super(df);
}
@Override
public void partial(TemplateContext tc, final String variable, String indent) {
TemplateContext partialTC = new TemplateContext("{{", "}}", tc.file(), tc.line(), tc.startOfLine());
list.add(new SpecPartialCode(partialTC, df, variable, indent));
}
@Override
public void value(TemplateContext tc, final String variable, boolean encoded) {
list.add(new SpecValueCode(tc, df, variable, encoded));
}
static class SpecPartialCode extends PartialCode {
private final String indent;
public SpecPartialCode(TemplateContext tc, DefaultMustacheFactory cf, String variable, String indent) {
super(tc, cf, variable);
this.indent = indent;
}
@Override
protected Writer executePartial(Writer writer, final List<Object> scopes) {
partial.execute(new IndentWriter(writer, indent), scopes);
return writer;
}
}
static class SpecValueCode extends ValueCode {
public SpecValueCode(TemplateContext tc, DefaultMustacheFactory df, String variable, boolean encoded) {
super(tc, df, variable, encoded);
}
@Override
protected void execute(Writer writer, final String value) throws IOException {
if (writer instanceof IndentWriter) {
IndentWriter iw = (IndentWriter) writer;
iw.flushIndent();
writer = iw.inner;
while (writer instanceof IndentWriter) {
((IndentWriter) writer).flushIndent();
writer = ((IndentWriter) writer).inner;
}
}
super.execute(writer, value);
}
}
}