diff --git a/src/main/java/de/neuland/pug4j/Pug4J.java b/src/main/java/de/neuland/pug4j/Pug4J.java index 1528469..ffa892a 100644 --- a/src/main/java/de/neuland/pug4j/Pug4J.java +++ b/src/main/java/de/neuland/pug4j/Pug4J.java @@ -3,8 +3,6 @@ import java.io.*; import java.net.URL; import java.nio.charset.Charset; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Map; import de.neuland.pug4j.exceptions.PugCompilerException; @@ -42,8 +40,7 @@ public static void render(String filename, Map model, Writer wri public static void render(String filename, Map model, Writer writer, boolean pretty) throws IOException, PugCompilerException { PugTemplate template = getTemplate(filename); - template.setPrettyPrint(pretty); - template.process(new PugModel(model), writer); + render(template,model,writer,pretty); } public static String render(PugTemplate template, Map model) throws PugCompilerException { @@ -112,10 +109,9 @@ private static PugTemplate getTemplate(Reader reader, String name, String extens private static PugTemplate createTemplate(String filename, TemplateLoader loader, ExpressionHandler expressionHandler) throws IOException { Parser parser = new Parser(filename, loader, expressionHandler); Node root = parser.parse(); - PugTemplate template = new PugTemplate(); + PugTemplate template = new PugTemplate(root); template.setExpressionHandler(expressionHandler); template.setTemplateLoader(loader); - template.setRootNode(root); return template; } diff --git a/src/main/java/de/neuland/pug4j/PugConfiguration.java b/src/main/java/de/neuland/pug4j/PugConfiguration.java index 9a4ae13..d115a1b 100644 --- a/src/main/java/de/neuland/pug4j/PugConfiguration.java +++ b/src/main/java/de/neuland/pug4j/PugConfiguration.java @@ -5,7 +5,6 @@ import de.neuland.pug4j.exceptions.PugCompilerException; import de.neuland.pug4j.exceptions.PugException; import de.neuland.pug4j.expression.ExpressionHandler; -import de.neuland.pug4j.expression.GraalJsExpressionHandler; import de.neuland.pug4j.expression.JexlExpressionHandler; import de.neuland.pug4j.filter.CDATAFilter; import de.neuland.pug4j.filter.CssFilter; @@ -38,7 +37,6 @@ public class PugConfiguration { private Map sharedVariables = new HashMap(); private TemplateLoader templateLoader = new FileTemplateLoader(); private ExpressionHandler expressionHandler = new JexlExpressionHandler(); - //private ExpressionHandler expressionHandler = new GraalJsExpressionHandler(); protected static final int MAX_ENTRIES = 1000; public PugConfiguration() { @@ -103,14 +101,12 @@ public String renderTemplate(PugTemplate template, Map model) th } private PugTemplate createTemplate(String name) throws PugException, IOException { - PugTemplate template = new PugTemplate(); Parser parser = new Parser(name, templateLoader, expressionHandler); Node root = parser.parse(); + PugTemplate template = new PugTemplate(root,getMode()); template.setTemplateLoader(templateLoader); template.setExpressionHandler(expressionHandler); - template.setRootNode(root); template.setPrettyPrint(prettyPrint); - template.setMode(getMode()); return template; } diff --git a/src/main/java/de/neuland/pug4j/PugConfigurationCaffeine.java b/src/main/java/de/neuland/pug4j/PugConfigurationCaffeine.java index 586e11b..985560d 100644 --- a/src/main/java/de/neuland/pug4j/PugConfigurationCaffeine.java +++ b/src/main/java/de/neuland/pug4j/PugConfigurationCaffeine.java @@ -89,15 +89,13 @@ public String renderTemplate(PugTemplate template, Map model) th } private PugTemplate createTemplate(String name) throws PugException, IOException { - PugTemplate template = new PugTemplate(); Parser parser = new Parser(name, templateLoader, expressionHandler); Node root = parser.parse(); + PugTemplate template = new PugTemplate(root,getMode()); template.setTemplateLoader(templateLoader); template.setExpressionHandler(expressionHandler); - template.setRootNode(root); template.setPrettyPrint(prettyPrint); - template.setMode(getMode()); return template; } diff --git a/src/main/java/de/neuland/pug4j/compiler/Compiler.java b/src/main/java/de/neuland/pug4j/compiler/Compiler.java index a374e98..26c1275 100644 --- a/src/main/java/de/neuland/pug4j/compiler/Compiler.java +++ b/src/main/java/de/neuland/pug4j/compiler/Compiler.java @@ -4,19 +4,20 @@ import java.io.Writer; import de.neuland.pug4j.exceptions.PugCompilerException; -import de.neuland.pug4j.expression.ExpressionHandler; import de.neuland.pug4j.model.PugModel; import de.neuland.pug4j.parser.node.Node; import de.neuland.pug4j.template.PugTemplate; public class Compiler { - private final Node rootNode; - private boolean prettyPrint; - private PugTemplate template = new PugTemplate(); + private PugTemplate template; + public Compiler(PugTemplate pugTemplate) { + this.template = pugTemplate; + } + //@TODO: Deprecated: Remove in 3.0.0 public Compiler(Node rootNode) { - this.rootNode = rootNode; + template = new PugTemplate(rootNode); } public String compileToString(PugModel model) throws PugCompilerException { @@ -27,14 +28,16 @@ public String compileToString(PugModel model) throws PugCompilerException { public void compile(PugModel model, Writer w) throws PugCompilerException { IndentWriter writer = new IndentWriter(w); - writer.setUseIndent(prettyPrint); - rootNode.execute(writer, model, template); + writer.setUseIndent(template.isPrettyPrint()); + template.getRootNode().execute(writer, model, template); } + //@TODO: Deprecated: Remove in 3.0.0 public void setPrettyPrint(boolean prettyPrint) { - this.prettyPrint = prettyPrint; + this.template.setPrettyPrint(prettyPrint); } + //@TODO: Deprecated: Remove in 3.0.0 public void setTemplate(PugTemplate pugTemplate) { this.template = pugTemplate; } diff --git a/src/main/java/de/neuland/pug4j/lexer/token/Doctypes.java b/src/main/java/de/neuland/pug4j/lexer/token/Doctypes.java index 47b7e64..087ef8c 100644 --- a/src/main/java/de/neuland/pug4j/lexer/token/Doctypes.java +++ b/src/main/java/de/neuland/pug4j/lexer/token/Doctypes.java @@ -7,7 +7,8 @@ public class Doctypes { private static Map doctypes = new HashMap(); static { - doctypes.put("default", ""); + doctypes.put("default", ""); // Fallback + doctypes.put("html", ""); doctypes.put("xml", ""); doctypes.put("transitional", ""); doctypes.put("strict", ""); @@ -15,6 +16,7 @@ public class Doctypes { doctypes.put("1.1", ""); doctypes.put("basic", ""); doctypes.put("mobile", ""); + doctypes.put("plist", ""); } public static String get(String pugDoctype) { diff --git a/src/main/java/de/neuland/pug4j/parser/node/DoctypeNode.java b/src/main/java/de/neuland/pug4j/parser/node/DoctypeNode.java index 85f9cc1..22ff002 100644 --- a/src/main/java/de/neuland/pug4j/parser/node/DoctypeNode.java +++ b/src/main/java/de/neuland/pug4j/parser/node/DoctypeNode.java @@ -2,16 +2,23 @@ import de.neuland.pug4j.compiler.IndentWriter; import de.neuland.pug4j.exceptions.PugCompilerException; +import de.neuland.pug4j.lexer.token.Doctypes; import de.neuland.pug4j.model.PugModel; import de.neuland.pug4j.template.PugTemplate; +import org.apache.commons.lang3.StringUtils; public class DoctypeNode extends Node { @Override public void execute(IndentWriter writer, PugModel model, PugTemplate template) throws PugCompilerException { String name = getValue(); - template.setDoctype(name); - writer.append(template.getDoctypeLine()); + if (name == null || StringUtils.isBlank(name)) { + name = "html"; + } + String doctypeLine = Doctypes.get(name); + if (doctypeLine == null) { + doctypeLine = ""; + } + writer.append(doctypeLine); writer.setCompiledDoctype(true); } - } diff --git a/src/main/java/de/neuland/pug4j/template/PugTemplate.java b/src/main/java/de/neuland/pug4j/template/PugTemplate.java index fa8569e..a269c67 100644 --- a/src/main/java/de/neuland/pug4j/template/PugTemplate.java +++ b/src/main/java/de/neuland/pug4j/template/PugTemplate.java @@ -6,10 +6,9 @@ import de.neuland.pug4j.compiler.Compiler; import de.neuland.pug4j.exceptions.PugCompilerException; import de.neuland.pug4j.expression.ExpressionHandler; -import de.neuland.pug4j.lexer.token.Doctypes; import de.neuland.pug4j.model.PugModel; +import de.neuland.pug4j.parser.node.DoctypeNode; import de.neuland.pug4j.parser.node.Node; -import org.apache.commons.lang3.StringUtils; public class PugTemplate { @@ -19,12 +18,21 @@ public class PugTemplate { private boolean xml = false; private TemplateLoader templateLoader; private ExpressionHandler expressionHandler; - private String doctypeLine; + + public PugTemplate() { + } + + public PugTemplate(final Node rootNode) { + setRootNode(rootNode); + } + + public PugTemplate(final Node rootNode, final Mode mode) { + setMode(mode); + setRootNode(rootNode); + } public void process(PugModel model, Writer writer) throws PugCompilerException { - Compiler compiler = new Compiler(rootNode); - compiler.setPrettyPrint(prettyPrint); - compiler.setTemplate(this); + Compiler compiler = new Compiler(this); compiler.compile(model, writer); } @@ -40,7 +48,13 @@ public Node getRootNode() { return rootNode; } + //@TODO: Deprecated: Remove in 3.0.0 public void setRootNode(Node rootNode) { + final Node peek = rootNode.getNodes().peek(); + if(peek instanceof DoctypeNode){ + DoctypeNode doctypeNode = (DoctypeNode) peek; + setDoctype(doctypeNode.getValue()); + } this.rootNode = rootNode; } @@ -61,33 +75,13 @@ public TemplateLoader getTemplateLoader() { } public void setDoctype(String name){ - if (name == null || StringUtils.isBlank(name)) { - name = "default"; - } - doctypeLine = Doctypes.get(name); - if (doctypeLine == null) { - doctypeLine = ""; - } - - this.terse = "".equals(this.doctypeLine.toLowerCase()); - this.xml = doctypeLine.startsWith("emptyMap()); @@ -47,15 +44,12 @@ public void shouldCloseBodylessTagsWithSlashAndIgnoreBlockWhenCompilingToXhtml() @Test public void shouldCloseBodylessTagsWithoutSlashAndIgnoreBlockWhenCompilingToHtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.HTML); - - TagNode tagNode = new TagNode(); for (String tagName : bodylessTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); withTextBlock(tagNode); - template.setRootNode(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.HTML); String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -65,15 +59,11 @@ public void shouldCloseBodylessTagsWithoutSlashAndIgnoreBlockWhenCompilingToHtml @Test public void shouldCloseBodylessTagsWithEndTagWhenCompilingToXml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XML); - - TagNode tagNode = new TagNode(); - for (String tagName : bodylessTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); withTextBlock(tagNode); - template.setRootNode(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XML); String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -83,16 +73,13 @@ public void shouldCloseBodylessTagsWithEndTagWhenCompilingToXml() { @Test public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompilingToXhtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XHTML); - - TagNode tagNode = new TagNode(); for (String tagName : bodylessTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XHTML); String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -102,16 +89,12 @@ public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompili @Test public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompilingToHtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.HTML); - - TagNode tagNode = new TagNode(); - for (String tagName : bodylessTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.HTML); String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -121,16 +104,12 @@ public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompili @Test public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompilingToXml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XML); - - TagNode tagNode = new TagNode(); - for (String tagName : bodylessTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XML); String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -140,15 +119,12 @@ public void shouldCloseSelfClosingBodylessTagsWithSlashAndIgnoreBlockWhenCompili @Test public void shouldCloseNormalTagsWithEndTagWhenCompilingToXhtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XHTML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); withTextBlock(tagNode); - template.setRootNode(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XHTML); + String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -159,15 +135,12 @@ public void shouldCloseNormalTagsWithEndTagWhenCompilingToXhtml() { @Test public void shouldCloseNormalTagsWithEndTagWhenCompilingToHtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.HTML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); withTextBlock(tagNode); - template.setRootNode(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.HTML); + String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -178,15 +151,12 @@ public void shouldCloseNormalTagsWithEndTagWhenCompilingToHtml() { @Test public void shouldCloseNormalTagsWithEndTagWhenCompilingToXml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); withTextBlock(tagNode); - template.setRootNode(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XML); + String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -198,16 +168,13 @@ public void shouldCloseNormalTagsWithEndTagWhenCompilingToXml() { @Test public void shouldCloseSelfClosingNormalTagsWithSlashAndIgnoreBlockWhenCompilingToXhtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XHTML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XHTML); + String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -217,16 +184,13 @@ public void shouldCloseSelfClosingNormalTagsWithSlashAndIgnoreBlockWhenCompiling @Test public void shouldCloseSelfClosingNormalTagsWithSlashAndIgnoreBlockWhenCompilingToHtml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.HTML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.HTML); + String result = jade.renderTemplate(template, Collections.emptyMap()); @@ -236,16 +200,13 @@ public void shouldCloseSelfClosingNormalTagsWithSlashAndIgnoreBlockWhenCompiling @Test public void shouldCloseSelfClosingNormalTagsWithSlashAndIgnoreBlockWhenCompilingToXml() { - PugTemplate template = new PugTemplate(); - template.setMode(Pug4J.Mode.XML); - - TagNode tagNode = new TagNode(); - for (String tagName : normalTags) { + TagNode tagNode = new TagNode(); tagNode.setName(tagName); - withTextBlock(tagNode); tagNode.setSelfClosing(true); - template.setRootNode(tagNode); + withTextBlock(tagNode); + PugTemplate template = new PugTemplate(tagNode,Pug4J.Mode.XML); + String result = jade.renderTemplate(template, Collections.emptyMap()); diff --git a/src/test/resources/issues/00a.jade b/src/test/resources/issues/00a.jade index b3f6474..1e4a2fa 100644 --- a/src/test/resources/issues/00a.jade +++ b/src/test/resources/issues/00a.jade @@ -1,4 +1,4 @@ -doctype default +doctype html - var foo=true - var bar=false a(data-test=true) diff --git a/src/test/resources/issues/00d.html b/src/test/resources/issues/00d.html new file mode 100644 index 0000000..379aca0 --- /dev/null +++ b/src/test/resources/issues/00d.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/test/resources/issues/00d.jade b/src/test/resources/issues/00d.jade new file mode 100644 index 0000000..33fd7fb --- /dev/null +++ b/src/test/resources/issues/00d.jade @@ -0,0 +1,15 @@ +doctype html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" +- var foo=true +- var bar=false +a(data-test=true) +a(data-test=false) +a(data-test=foo) +a(data-test=bar) +a(data-test="") +a(data-test="test") +a(test=true) +a(test=false) +a(test=foo) +a(test=bar) +a(test="") +a(test="test")