Minor formatting tweaks made while reading diffs.
This commit is contained in:
@@ -413,7 +413,7 @@ public class Mustache {
|
|||||||
private Mustache () {} // no instantiateski
|
private Mustache () {} // no instantiateski
|
||||||
|
|
||||||
protected static Template.Segment[] trim (Template.Segment[] segs, boolean top) {
|
protected static Template.Segment[] trim (Template.Segment[] segs, boolean top) {
|
||||||
// Trim modifies segs! It's return is not a copy!
|
// trim() modifies segs! Its return is not a copy!
|
||||||
// now that we have all of our segments, we make a pass through them to trim whitespace
|
// now that we have all of our segments, we make a pass through them to trim whitespace
|
||||||
// from section tags which stand alone on their lines
|
// from section tags which stand alone on their lines
|
||||||
assert segs != null;
|
assert segs != null;
|
||||||
@@ -440,22 +440,18 @@ public class Mustache {
|
|||||||
block._standaloneEnd = true;
|
block._standaloneEnd = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// we have to indent partials if there is space before
|
// we have to indent partials if there is space before they are also standalone...
|
||||||
// they are also standalone...
|
|
||||||
else if (seg instanceof IncludedTemplateSegment) {
|
else if (seg instanceof IncludedTemplateSegment) {
|
||||||
IncludedTemplateSegment include = (IncludedTemplateSegment) seg;
|
IncludedTemplateSegment include = (IncludedTemplateSegment) seg;
|
||||||
if (prev != null && prevBlank && nextBlank) {
|
if (prev != null && prevBlank && nextBlank) {
|
||||||
String indent = prev.indent();
|
String indent = prev.indent();
|
||||||
include._standalone = true;
|
include._standalone = true;
|
||||||
if (! indent.equals("")) {
|
if (!indent.equals("")) {
|
||||||
include = include.indent(indent, pseg == null,nseg == null);
|
include = include.indent(indent, pseg == null,nseg == null);
|
||||||
segs[ii] = include;
|
segs[ii] = include;
|
||||||
}
|
}
|
||||||
//segs[ii-1] = prev.trimTrailBlank();
|
//segs[ii-1] = prev.trimTrailBlank();
|
||||||
/*
|
/* We trim the end because partials follow standalone just like blocks */
|
||||||
* We trim the end because partials
|
|
||||||
* follow standalone just like blocks
|
|
||||||
*/
|
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
segs[ii+1] = next.trimLeadBlank();
|
segs[ii+1] = next.trimLeadBlank();
|
||||||
}
|
}
|
||||||
@@ -480,11 +476,10 @@ public class Mustache {
|
|||||||
* @param _last whether to append an indent on the last segment last empty newline (no character after \n).
|
* @param _last whether to append an indent on the last segment last empty newline (no character after \n).
|
||||||
* @return cloned segments if changed
|
* @return cloned segments if changed
|
||||||
*/
|
*/
|
||||||
static Template.Segment[] indentSegs(Template.Segment[] _segs, String indent, boolean _first, boolean _last) {
|
static Template.Segment[] indentSegs (Template.Segment[] _segs, String indent, boolean _first, boolean _last) {
|
||||||
// unlike trim this method clones the segments if they have changed
|
// unlike trim this method clones the segments if they have changed so the return value must
|
||||||
// so the return value must be handled
|
// be handled; a simple identity check on the return can be used to determine if there is
|
||||||
// a simple identity check on the return can be used to determine
|
// change
|
||||||
// if there is change
|
|
||||||
if (indent.equals("")) {
|
if (indent.equals("")) {
|
||||||
return _segs;
|
return _segs;
|
||||||
}
|
}
|
||||||
@@ -492,11 +487,9 @@ public class Mustache {
|
|||||||
Template.Segment[] copySegs = new Template.Segment[length];
|
Template.Segment[] copySegs = new Template.Segment[length];
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
for (int i = 0; i < _segs.length; i++) {
|
for (int i = 0; i < _segs.length; i++) {
|
||||||
|
|
||||||
Template.Segment seg = _segs[i];
|
Template.Segment seg = _segs[i];
|
||||||
Template.Segment pseg = (i > 0) ? _segs[i-1] : null;
|
Template.Segment pseg = (i > 0) ? _segs[i-1] : null;
|
||||||
Template.Segment nseg = (i < length - 1) ? _segs[i+1] : null;
|
Template.Segment nseg = (i < length - 1) ? _segs[i+1] : null;
|
||||||
|
|
||||||
Template.Segment copy;
|
Template.Segment copy;
|
||||||
if (seg instanceof BlockSegment) {
|
if (seg instanceof BlockSegment) {
|
||||||
BlockSegment bs = (BlockSegment) seg;
|
BlockSegment bs = (BlockSegment) seg;
|
||||||
@@ -557,14 +550,12 @@ public class Mustache {
|
|||||||
}
|
}
|
||||||
else if (seg instanceof IncludedTemplateSegment) {
|
else if (seg instanceof IncludedTemplateSegment) {
|
||||||
/*
|
/*
|
||||||
* If we are standalone then we rely on the indentation
|
* If we are standalone then we rely on the indentation already present before the
|
||||||
* already present before the partial tag.
|
* partial tag.
|
||||||
* [ WS ]{{> partial }}[\n]
|
* [ WS ]{{> partial }}[\n]
|
||||||
*
|
*
|
||||||
* That is partial tags do not have the trailing blank
|
* That is partial tags do not have the trailing blank removed during the trim
|
||||||
* removed during the trim process.
|
* process. This avoids needlessley creating StringSegment tags.
|
||||||
*
|
|
||||||
* This avoids needlessley creating StringSegment tags.
|
|
||||||
*/
|
*/
|
||||||
if (seg.isStandalone()) {
|
if (seg.isStandalone()) {
|
||||||
boolean last;
|
boolean last;
|
||||||
@@ -965,14 +956,14 @@ public class Mustache {
|
|||||||
* Calculate indent for partial idententation
|
* Calculate indent for partial idententation
|
||||||
* @return indent space or empty string
|
* @return indent space or empty string
|
||||||
*/
|
*/
|
||||||
String indent() {
|
String indent () {
|
||||||
if (_trailBlank == -1 || _trailBlank >= _text.length()) {
|
if (_trailBlank == -1 || _trailBlank >= _text.length()) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return _text.substring(_trailBlank);
|
return _text.substring(_trailBlank);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringSegment indent(String indent, boolean first, boolean last) {
|
StringSegment indent (String indent, boolean first, boolean last) {
|
||||||
if (indent.equals("")) {
|
if (indent.equals("")) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -980,7 +971,7 @@ public class Mustache {
|
|||||||
return new StringSegment(reindent, _first);
|
return new StringSegment(reindent, _first);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override boolean isStandalone() {
|
@Override boolean isStandalone () {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -998,17 +989,17 @@ public class Mustache {
|
|||||||
_leadBlank + "/" + _trailBlank;
|
_leadBlank + "/" + _trailBlank;
|
||||||
}
|
}
|
||||||
|
|
||||||
//we indent after every new line for partial indententation
|
// we indent after every new line for partial indententation
|
||||||
private static String reindent(String input, String indent, boolean first, boolean last) {
|
private static String reindent (String input, String indent, boolean first, boolean last) {
|
||||||
int length = input.length();
|
int length = input.length();
|
||||||
StringBuilder sb = new StringBuilder(indent.length() + length);
|
StringBuilder sb = new StringBuilder(indent.length() + length);
|
||||||
if (first) {
|
if (first) {
|
||||||
sb.append(indent);
|
sb.append(indent);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < length; i++) {
|
for (int ii = 0; ii < length; ii++) {
|
||||||
char c = input.charAt(i);
|
char c = input.charAt(ii);
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
if (c == '\n' && ( last || i != length - 1) ) {
|
if (c == '\n' && (last || ii != length - 1)) {
|
||||||
sb.append(indent);
|
sb.append(indent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1074,7 +1065,7 @@ public class Mustache {
|
|||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
protected IncludedTemplateSegment indent(String indent, boolean first, boolean last) {
|
protected IncludedTemplateSegment indent (String indent, boolean first, boolean last) {
|
||||||
// Indent this partial based on the spacing provided.
|
// Indent this partial based on the spacing provided.
|
||||||
// per the spec however much the partial reference is indendented (leading whitespace)
|
// per the spec however much the partial reference is indendented (leading whitespace)
|
||||||
// is how much the partial content should be indented.
|
// is how much the partial content should be indented.
|
||||||
@@ -1085,10 +1076,10 @@ public class Mustache {
|
|||||||
is._standalone = _standalone;
|
is._standalone = _standalone;
|
||||||
return is;
|
return is;
|
||||||
}
|
}
|
||||||
@Override boolean isStandalone() { return _standalone; }
|
@Override boolean isStandalone () { return _standalone; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString () {
|
||||||
return "Include(name=" + _name + ", indent=" + _indent + ", standalone=" + _standalone
|
return "Include(name=" + _name + ", indent=" + _indent + ", standalone=" + _standalone
|
||||||
+ ")";
|
+ ")";
|
||||||
}
|
}
|
||||||
@@ -1135,11 +1126,11 @@ public class Mustache {
|
|||||||
visitor.visitVariable(_name);
|
visitor.visitVariable(_name);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
VariableSegment indent(String indent, boolean first, boolean last) {
|
VariableSegment indent (String indent, boolean first, boolean last) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
boolean isStandalone() {
|
boolean isStandalone () {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@Override public String toString () {
|
@Override public String toString () {
|
||||||
@@ -1188,10 +1179,10 @@ public class Mustache {
|
|||||||
protected abstract BlockSegment indent (String indent, boolean first, boolean last);
|
protected abstract BlockSegment indent (String indent, boolean first, boolean last);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
boolean isStandalone() {
|
boolean isStandalone () {
|
||||||
return _standaloneEnd;
|
return _standaloneEnd;
|
||||||
}
|
}
|
||||||
boolean isStandaloneStart() {
|
boolean isStandaloneStart () {
|
||||||
return _standaloneStart;
|
return _standaloneStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1206,7 +1197,7 @@ public class Mustache {
|
|||||||
super(name, segs, line);
|
super(name, segs, line);
|
||||||
_comp = compiler;
|
_comp = compiler;
|
||||||
}
|
}
|
||||||
protected SectionSegment(SectionSegment original, Template.Segment[] segs) {
|
protected SectionSegment (SectionSegment original, Template.Segment[] segs) {
|
||||||
super(original, segs);
|
super(original, segs);
|
||||||
_comp = original._comp;
|
_comp = original._comp;
|
||||||
}
|
}
|
||||||
@@ -1328,7 +1319,7 @@ public class Mustache {
|
|||||||
@Override public void decompile (Delims delims, StringBuilder into) {} // nada
|
@Override public void decompile (Delims delims, StringBuilder into) {} // nada
|
||||||
@Override public void visit (Visitor visit) {}
|
@Override public void visit (Visitor visit) {}
|
||||||
@Override FauxSegment indent (String indent, boolean first, boolean last) { return this; }
|
@Override FauxSegment indent (String indent, boolean first, boolean last) { return this; }
|
||||||
@Override boolean isStandalone() { return true; }
|
@Override boolean isStandalone () { return true; }
|
||||||
@Override public String toString () { return "Faux"; }
|
@Override public String toString () { return "Faux"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -166,13 +166,8 @@ public class Template {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected Template indent(String indent) {
|
protected Template indent(String indent) {
|
||||||
/*
|
// What we want to do here is rebuild this partial template but indented.
|
||||||
* What we want to do here is rebuild this partial template
|
// If identing does not change anything we return the original template.
|
||||||
* but indented.
|
|
||||||
*
|
|
||||||
* If identing does not change anything we return
|
|
||||||
* the original template.
|
|
||||||
*/
|
|
||||||
if (indent.equals("")) {
|
if (indent.equals("")) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -397,32 +392,26 @@ public class Template {
|
|||||||
/** A template is broken into segments. */
|
/** A template is broken into segments. */
|
||||||
protected static abstract class Segment {
|
protected static abstract class Segment {
|
||||||
abstract void execute (Template tmpl, Context ctx, Writer out);
|
abstract void execute (Template tmpl, Context ctx, Writer out);
|
||||||
|
|
||||||
abstract void decompile (Mustache.Delims delims, StringBuilder into);
|
abstract void decompile (Mustache.Delims delims, StringBuilder into);
|
||||||
|
|
||||||
abstract void visit (Mustache.Visitor visitor);
|
abstract void visit (Mustache.Visitor visitor);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively indent by the parameter indent.
|
* Recursively indent by the parameter indent.
|
||||||
* @param indent should be space characters that are not \n
|
* @param indent should be space characters that are not {@code \n}.
|
||||||
* @param first append indent to the first line (regardless if it has a \n or not)
|
* @param first append indent to the first line (regardless if it has a {@code \n} or not).
|
||||||
* @param last append indent on the last \n that has no text after it
|
* @param last append indent on the last {@code \n} that has no text after it.
|
||||||
* @return newly crated segment or the same segment if nothing changed.
|
* @return a newly created segment or the same segment if nothing changed.
|
||||||
*/
|
*/
|
||||||
abstract Segment indent(String indent, boolean first, boolean last);
|
abstract Segment indent (String indent, boolean first, boolean last);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether or not the segment is standalone.
|
* Whether or not the segment is standalone. The definition of standalone is defined by the
|
||||||
* For blocks this is based on the closing tag.
|
* mustache spec. String and variable tags are never standalone. For blocks this is based on
|
||||||
* Once Trim is called standalone tags are determined so
|
* the closing tag. Once {@code trim} is called, standalone tags are determined so that
|
||||||
* that proper (re)indentation will work without reparsing
|
* proper (re)indentation will work without reparsing the template.
|
||||||
* the template.
|
* @return true if the tag is standalone.
|
||||||
*
|
|
||||||
* String and variable tags are never standalone.
|
|
||||||
*
|
|
||||||
* The definition of standalone is defined by the mustache spec.
|
|
||||||
*
|
|
||||||
* @return true if the tag is standalone
|
|
||||||
*/
|
*/
|
||||||
abstract boolean isStandalone();
|
abstract boolean isStandalone ();
|
||||||
|
|
||||||
protected static void write (Writer out, CharSequence data) {
|
protected static void write (Writer out, CharSequence data) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
//
|
||||||
|
// JMustache - A Java implementation of the Mustache templating language
|
||||||
|
// http://github.com/samskivert/jmustache/blob/master/LICENSE
|
||||||
|
|
||||||
package com.samskivert.mustache;
|
package com.samskivert.mustache;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
@@ -25,7 +29,6 @@ public class PartialThreadSafeTest {
|
|||||||
long t = System.currentTimeMillis();
|
long t = System.currentTimeMillis();
|
||||||
AtomicInteger loadCount = new AtomicInteger();
|
AtomicInteger loadCount = new AtomicInteger();
|
||||||
TemplateLoader loader = new TemplateLoader() {
|
TemplateLoader loader = new TemplateLoader() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Reader getTemplate(String name) throws Exception {
|
public Reader getTemplate(String name) throws Exception {
|
||||||
if ("partial".equals(name)) {
|
if ("partial".equals(name)) {
|
||||||
@@ -63,7 +66,5 @@ public class PartialThreadSafeTest {
|
|||||||
assertEquals(1, loadCount.get());
|
assertEquals(1, loadCount.get());
|
||||||
System.out.println(loadCount);
|
System.out.println(loadCount);
|
||||||
System.out.println(System.currentTimeMillis() - t);
|
System.out.println(System.currentTimeMillis() - t);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -229,7 +229,6 @@ public abstract class SharedTests
|
|||||||
} catch (UnsupportedOperationException uoe) {} // expected
|
} catch (UnsupportedOperationException uoe) {} // expected
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Map<String,String> partials = new LinkedHashMap<>();
|
Map<String,String> partials = new LinkedHashMap<>();
|
||||||
|
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
|
|||||||
@@ -20,5 +20,4 @@ public class CustomSpecTest extends SpecTest {
|
|||||||
};
|
};
|
||||||
return SpecTest.data("/custom/specs/", groups);
|
return SpecTest.data("/custom/specs/", groups);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,5 +25,4 @@ public class OfficialSpecTest extends SpecTest {
|
|||||||
};
|
};
|
||||||
return SpecTest.data("/specs/specs/", groups);
|
return SpecTest.data("/specs/specs/", groups);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ public abstract class SpecTest {
|
|||||||
s = s.replace('\t', '\u21E5');
|
s = s.replace('\t', '\u21E5');
|
||||||
s = s.replace("\n", "\u21B5\n");
|
s = s.replace("\n", "\u21B5\n");
|
||||||
s = s.replace("\u240D", "\u240D\n");
|
s = s.replace("\u240D", "\u240D\n");
|
||||||
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +74,6 @@ public abstract class SpecTest {
|
|||||||
return (text == null) ? null : text.replace("\r", "\\r").replace("\n", "\\n");
|
return (text == null) ? null : text.replace("\r", "\\r").replace("\n", "\\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Collection<Object[]> data (String specPath, String[] groups) {
|
public static Collection<Object[]> data (String specPath, String[] groups) {
|
||||||
List<Object[]> tuples = new ArrayList<>();
|
List<Object[]> tuples = new ArrayList<>();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -112,5 +110,4 @@ public abstract class SpecTest {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user