diff --git a/src/main/java/de/neuland/pug4j/lexer/token/Mixin.java b/src/main/java/de/neuland/pug4j/lexer/token/Mixin.java index 759c9e0..5da0f09 100644 --- a/src/main/java/de/neuland/pug4j/lexer/token/Mixin.java +++ b/src/main/java/de/neuland/pug4j/lexer/token/Mixin.java @@ -8,6 +8,8 @@ public class Mixin extends Token { public Mixin(String value, int lineNumber) { super(value, lineNumber); } + public Mixin() { + } public void setArguments(String args) { this.arguments = args; diff --git a/src/main/java/de/neuland/pug4j/parser/Parser.java b/src/main/java/de/neuland/pug4j/parser/Parser.java index 8846f90..489cdab 100644 --- a/src/main/java/de/neuland/pug4j/parser/Parser.java +++ b/src/main/java/de/neuland/pug4j/parser/Parser.java @@ -20,6 +20,7 @@ public class Parser { + public static final Pattern PATTERN_REST = Pattern.compile("^\\.\\.\\."); private final Lexer lexer; private LinkedHashMap blocks = new LinkedHashMap<>(); private final TemplateLoader templateLoader; @@ -259,14 +260,28 @@ private Node parseMixin() { String rest; if (args.size() > 0) { - Matcher matcher = Pattern.compile("^\\.\\.\\.").matcher(args.get(args.size() - 1).trim()); + Matcher matcher = PATTERN_REST.matcher(args.get(args.size() - 1).trim()); if (matcher.find(0)) { rest = args.remove(args.size() - 1).trim().replaceAll("^\\.\\.\\.", ""); node.setRest(rest); } } - + List newArgs = new ArrayList<>(); + HashMap defaultValues = new HashMap(); + for (String arg : args) { + String key; + Matcher matcher = Pattern.compile("^([a-zA-Z][a-zA-Z0-9]*)=(.*)$").matcher(arg); + if(matcher.find(0) && matcher.groupCount() > 1){ + key=matcher.group(1); + defaultValues.put(key,matcher.group(2)); + }else{ + key=arg; + } + newArgs.add(key); + } + node.setArguments(newArgs); + node.setDefaultValues(defaultValues); node.setBlock(block()); node.setCall(false); this.mixins.put(mixinToken.getValue(), node); diff --git a/src/main/java/de/neuland/pug4j/parser/node/CallNode.java b/src/main/java/de/neuland/pug4j/parser/node/CallNode.java index dc47796..e811697 100644 --- a/src/main/java/de/neuland/pug4j/parser/node/CallNode.java +++ b/src/main/java/de/neuland/pug4j/parser/node/CallNode.java @@ -61,13 +61,7 @@ public void execute(IndentWriter writer, PugModel model, PugTemplate template) t model.putLocal("block",null); model.popScope(); - }else{ - } - - - - } private List getInjectionPoints(Node block) { @@ -99,18 +93,20 @@ private void writeVariables(PugModel model, MixinNode mixin, PugTemplate templat for (int i = 0; i < names.size(); i++) { String key = names.get(i); + String valueExpression = mixin.getDefaultValues().get(key); Object value = null; if (i < values.size()) { - value = values.get(i); + valueExpression = values.get(i); } - if (value != null) { + if (valueExpression != null) { try { - value = template.getExpressionHandler().evaluateExpression(values.get(i), model); + value = template.getExpressionHandler().evaluateExpression(valueExpression, model); } catch (Throwable e) { throw new PugCompilerException(this, template.getTemplateLoader(), e); } } if (key != null) { + model.putLocal(key, value); } } diff --git a/src/main/java/de/neuland/pug4j/parser/node/MixinNode.java b/src/main/java/de/neuland/pug4j/parser/node/MixinNode.java index ad61f04..175956d 100644 --- a/src/main/java/de/neuland/pug4j/parser/node/MixinNode.java +++ b/src/main/java/de/neuland/pug4j/parser/node/MixinNode.java @@ -5,8 +5,11 @@ import de.neuland.pug4j.model.PugModel; import de.neuland.pug4j.template.PugTemplate; +import java.util.HashMap; + public class MixinNode extends CallNode { private String rest; + private HashMap defaultValues = new HashMap(); @Override public void execute(IndentWriter writer, PugModel model, PugTemplate template) throws PugCompilerException { @@ -24,4 +27,12 @@ public void setRest(String rest) { public String getRest() { return rest; } + + public HashMap getDefaultValues() { + return defaultValues; + } + + public void setDefaultValues(final HashMap defaultValues) { + this.defaultValues = defaultValues; + } } diff --git a/src/test/resources/issues/pug014.html b/src/test/resources/issues/pug014.html new file mode 100644 index 0000000..9543791 --- /dev/null +++ b/src/test/resources/issues/pug014.html @@ -0,0 +1,22 @@ +
+
+

Default Title

+

This is a body

+
    +
  • 1
  • +
  • 2
  • +
+
+
+
+
+

Hello world

+

What a beautiful day

+
    +
  • 1
  • +
  • 2
  • +
  • 3
  • +
+
+
+ diff --git a/src/test/resources/issues/pug014.jade b/src/test/resources/issues/pug014.jade new file mode 100644 index 0000000..6c85964 --- /dev/null +++ b/src/test/resources/issues/pug014.jade @@ -0,0 +1,14 @@ +//- Declaration +mixin article(body, title='Default Title', list=[1,2]) + .article + .article-wrapper + h1= title + p= body + ul + each val in list + li= val + +//- Use ++article("This is a body") + ++article('What a beautiful day', 'Hello world', [1,2,3]) \ No newline at end of file