Skip to content

Commit

Permalink
Fixed issue #14: Default Mixin Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chbloemer committed Oct 6, 2023
1 parent d02947e commit 572cf6d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/de/neuland/pug4j/lexer/token/Mixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/de/neuland/pug4j/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

public class Parser {

public static final Pattern PATTERN_REST = Pattern.compile("^\\.\\.\\.");
private final Lexer lexer;
private LinkedHashMap<String, BlockNode> blocks = new LinkedHashMap<>();
private final TemplateLoader templateLoader;
Expand Down Expand Up @@ -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<String> newArgs = new ArrayList<>();
HashMap<String,String> defaultValues = new HashMap<String,String>();
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);
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/de/neuland/pug4j/parser/node/CallNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ public void execute(IndentWriter writer, PugModel model, PugTemplate template) t
model.putLocal("block",null);
model.popScope();

}else{

}




}

private List<MixinBlockNode> getInjectionPoints(Node block) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/neuland/pug4j/parser/node/MixinNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> defaultValues = new HashMap<String,String>();

@Override
public void execute(IndentWriter writer, PugModel model, PugTemplate template) throws PugCompilerException {
Expand All @@ -24,4 +27,12 @@ public void setRest(String rest) {
public String getRest() {
return rest;
}

public HashMap<String,String> getDefaultValues() {
return defaultValues;
}

public void setDefaultValues(final HashMap<String,String> defaultValues) {
this.defaultValues = defaultValues;
}
}
22 changes: 22 additions & 0 deletions src/test/resources/issues/pug014.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="article">
<div class="article-wrapper">
<h1>Default Title</h1>
<p>This is a body</p>
<ul>
<li>1</li>
<li>2</li>
</ul>
</div>
</div>
<div class="article">
<div class="article-wrapper">
<h1>Hello world</h1>
<p>What a beautiful day</p>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</div>
</div>

14 changes: 14 additions & 0 deletions src/test/resources/issues/pug014.jade
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 572cf6d

Please sign in to comment.