Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement .collectWithParameters() and .collectAllParameters() #637

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b4323ab
Implement collectAllParameters() and collectWithParams() with Ilya
andrewboni Jun 6, 2018
50b44a7
fix for Variable
dontgitit Jun 6, 2018
61b42d8
Update comments and add the TagType for TagWithParams.
andrewboni Jun 8, 2018
d213d17
Update comments
andrewboni Jun 8, 2018
46d885b
Add in tagType and fix test
andrewboni Jun 8, 2018
8ee631e
Add additional test cases
andrewboni Jun 11, 2018
6678deb
Update tests
andrewboni Jun 11, 2018
8ba0497
Add built snapshot JAR to use until changes merged upstream
Jun 12, 2018
95a12c7
remove handlebars jar
Jun 12, 2018
223e7c7
Update tests, add in custom .toString()
andrewboni Jun 21, 2018
4cbdfee
Merge branch 'master' of github.com:Iterable/handlebars.java
andrewboni Jun 21, 2018
775086e
Add comments, fix style
andrewboni Jun 22, 2018
ded97c1
refactor name and package
dontgitit Oct 31, 2018
6bce684
Merge remote-tracking branch 'upstream/master'
dontgitit Oct 31, 2018
acecec6
mark collect as deprecated
dontgitit Oct 31, 2018
8b46b71
fix whitespace
dontgitit Oct 31, 2018
1cd3eac
Merge remote-tracking branch 'upstream/master'
dontgitit Mar 3, 2020
fb4e295
add internal data
dontgitit Mar 3, 2020
5ed8e52
add tests
dontgitit Mar 4, 2020
25f80ba
add tests against direct context lookup
dontgitit Mar 4, 2020
ca2937d
fix test name
dontgitit Mar 4, 2020
e0f7722
Merge pull request #1 from Iterable/create-internal-storage
dontgitit Mar 4, 2020
ae4e84a
Merge remote-tracking branch 'upstream/master'
dontgitit Aug 8, 2022
888f789
Merge branch 'jknack:master' into master
vbabenkoru Feb 23, 2024
3b6506f
[TNT-7113] Use commit hashes for github repo actions
dennisiterable Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package com.github.jknack.handlebars;

import com.github.jknack.handlebars.internal.Param;
import com.github.jknack.handlebars.internal.TagWithParams;

import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
Expand Down Expand Up @@ -102,10 +105,18 @@ public List<String> collect(final TagType... tagType) {
return Collections.emptyList();
}

@Override
public List<TagWithParams> collectWithParameters(final TagType... tagType) {
return Collections.emptyList();
}

@Override
public List<String> collectReferenceParameters() {
return Collections.emptyList();
}

@Override
public List<Param> collectAllParameters() { return Collections.emptyList(); }
};

/**
Expand Down Expand Up @@ -204,6 +215,33 @@ public List<String> collectReferenceParameters() {
*/
List<String> collect(TagType... tagType);

/**
* Collect all the tag names under the given tagType and their parameters.
* <p>
* Usage:
* </p>
*
* <pre>
* {{hello}}
* {{var 1}}
* {{{tripleVar}}}
* </pre>
* <p>
* <code>collectWithParameters(TagType.VAR)</code> returns <code>[TagWithParams(hello, [], TagType.VAR), TagWithParams(var, [1], TagType.VAR)]</code>
* </p>
* <p>
* <code>collect(TagType.TRIPLE_VAR)</code> returns <code>[TagWithParams(tripleVar, [], TagType.TRIPLE_VAR)]</code>
* </p>
* <p>
* <code>collect(TagType.VAR, TagType.TRIPLE_VAR)</code> returns
* <code>[TagWithParams(hello, [], TagType.VAR), TagWithParams(var, [1], TagType.VAR), TagWithParams(tripleVar, [], TagType.TRIPLE_VAR)]</code>
* </p>
*
* @param tagType The tag type. Required.
* @return A list of TagWithParams.
*/
List<TagWithParams> collectWithParameters(TagType... tagType);

/**
* Collects all the parameters which are also variables.
* <p>
Expand All @@ -222,6 +260,24 @@ public List<String> collectReferenceParameters() {
*/
List<String> collectReferenceParameters();

/**
* Collects all the parameters.
* <p>
* Usage:
* </p>
*
* <pre>
* {{#if v1}}{{/if}}
* {{#each v2 "test"}}{{/each}}
* </pre>
* <p>
* <code>collectAllParameters()</code> returns <code>[v1, v2]</code>
* </p>
*
* @return A list of Param.
*/
List<Param> collectAllParameters();

/**
* @return The template file's name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ public List<String> collect(final TagType... tagType) {
return new ArrayList<>(tagNames);
}

@Override
public List<TagWithParams> collectWithParameters(final TagType... tagType) {
isTrue(tagType.length > 0, "At least one tag type is required.");
List<TagWithParams> tagsWithParams = new ArrayList<>();
for (TagType tt : tagType) {
collectWithParameters(tagsWithParams, tt);
}
return new ArrayList<>(tagsWithParams);
}

/**
* Child classes might want to check if they apply to the tagtype and append them self to the
* result list.
Expand All @@ -359,19 +369,39 @@ public List<String> collect(final TagType... tagType) {
protected void collect(final Collection<String> result, final TagType tagType) {
}

/**
* @param result The result list of TagWithParams.
* @param tagType The matching tagtype.
*/
protected void collectWithParameters(final Collection<TagWithParams> result, final TagType tagType) {
}

@Override
public List<String> collectReferenceParameters() {
Set<String> paramNames = new LinkedHashSet<>();
collectReferenceParameters(paramNames);
return new ArrayList<>(paramNames);
}

@Override
public List<Param> collectAllParameters() {
Set<Param> params = new LinkedHashSet<>();
collectAllParameters(params);
return new ArrayList<>(params);
}

/**
* @param result The result list to add new parameters to.
*/
protected void collectReferenceParameters(final Collection<String> result) {
}

/**
* @param result The result list of Params to add new Params to.
*/
protected void collectAllParameters(final Collection<Param> result) {
}

@Override
public String toJavaScript() {
if (javaScript == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,17 @@ public List<String> collect(final TagType... tagType) {
return new ArrayList<>(tagNames);
}

@Override
public List<TagWithParams> collectWithParameters(final TagType... tagType) {
List<TagWithParams> tagNames = new ArrayList<TagWithParams>();
if (body != null) {
tagNames.addAll(body.collectWithParameters(tagType));
}
tagNames.addAll(inverse.collectWithParameters(tagType));
tagNames.addAll(super.collectWithParameters(tagType));
return new ArrayList<TagWithParams>(tagNames);
}

@Override
protected void collect(final Collection<String> result, final TagType tagType) {
if (tagType == this.tagType) {
Expand All @@ -395,6 +406,14 @@ protected void collect(final Collection<String> result, final TagType tagType) {
super.collect(result, tagType);
}

@Override
protected void collectWithParameters(final Collection<TagWithParams> result, final TagType tagType) {
if (tagType == this.tagType) {
result.add(new TagWithParams(name, collectAllParameters(), tagType));
}
super.collectWithParameters(result, tagType);
}

@Override
public List<String> collectReferenceParameters() {
Set<String> paramNames = new LinkedHashSet<>();
Expand All @@ -405,4 +424,15 @@ public List<String> collectReferenceParameters() {
paramNames.addAll(super.collectReferenceParameters());
return new ArrayList<>(paramNames);
}

@Override
public List<Param> collectAllParameters() {
Set<Param> paramNames = new LinkedHashSet<>();
if (body != null) {
paramNames.addAll(body.collectAllParameters());
}
paramNames.addAll(inverse.collectAllParameters());
paramNames.addAll(super.collectAllParameters());
return new ArrayList<>(paramNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ public List<String> collect(final TagType... tagType) {
return template.collect(tagType);
}

@Override
public List<Param> collectAllParameters() {
return template.collectAllParameters();
}

@Override
public List<TagWithParams> collectWithParameters(final TagType... tagType) {
return template.collectWithParameters(tagType);
}

@Override
public List<String> collectReferenceParameters() {
return template.collectReferenceParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ protected void collect(final Collection<String> result, final TagType tagType) {
}
}

@Override
protected void collectWithParameters(final Collection<TagWithParams> result, final TagType tagType) {
for (Object param : this.params) {
if (param instanceof VarParam) {
((VarParam) param).fn.collectWithParameters(result, tagType);
}
}
}

@Override
protected void collectReferenceParameters(final Collection<String> result) {
for (Object param : params) {
Expand All @@ -279,4 +288,16 @@ protected void collectReferenceParameters(final Collection<String> result) {
}
}

@Override
protected void collectAllParameters(final Collection<Param> result) {
for (Param param : params) {
if (param instanceof VarParam) {
((VarParam) param).fn.collectAllParameters(result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this collecting nested hbars, for example {{#if (anotherHelper) }}? or is this doing something else?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't seem to currently; an input of {{capitalize (lower firstName)}} returns TagWithParams(capitalize, firstName, TagType.VAR)

If I include TagType.SUB_EXPRESSION, it does work. Let me change the implementation to template.collectWithParameters(TagType.values())

} else {
result.add(param);
}
}
result.addAll(hash.values());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.jknack.handlebars.internal;
import com.github.jknack.handlebars.TagType;

import java.util.List;

public class TagWithParams {
private String tag;
private List<Param> params;
private TagType tagType;

TagWithParams(String tag, List<Param> params, TagType tagType) {
this.tag = tag;
this.params = params;
this.tagType = tagType;
}

public String getTag() {
return tag;
}
public List<Param> getParams() {
return params;
}
public TagType getTagType() { return tagType; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public List<String> collect(final TagType... tagType) {
return new ArrayList<>(tagNames);
}

@Override
public List<TagWithParams> collectWithParameters(final TagType... tagType) {
List<TagWithParams> tagNames = new ArrayList<>();
for (Template node : nodes) {
tagNames.addAll(node.collectWithParameters(tagType));
}
return new ArrayList<>(tagNames);
}

@Override
public List<String> collectReferenceParameters() {
Set<String> paramNames = new LinkedHashSet<>();
Expand All @@ -142,6 +151,15 @@ public List<String> collectReferenceParameters() {
return new ArrayList<>(paramNames);
}

@Override
public List<Param> collectAllParameters() {
List<Param> paramNames = new ArrayList<>();
for (Template node : nodes) {
paramNames.addAll(node.collectAllParameters());
}
return new ArrayList<>(paramNames);
}

@Override
public String toString() {
return nodes.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ protected void collect(final Collection<String> result, final TagType tagType) {
super.collect(result, tagType);
}

@Override
protected void collectWithParameters(final Collection<TagWithParams> result, final TagType tagType) {
if (this.type == tagType) {
result.add(new TagWithParams(name, collectAllParameters(), tagType));
}
super.collectWithParameters(result, tagType);
}

/**
* Format and escape a var (if need it).
*
Expand Down
Loading