Skip to content

Commit

Permalink
Merge pull request #18 from superkonduktr/master
Browse files Browse the repository at this point in the history
add newline_to_br filter
  • Loading branch information
bkiers committed Mar 11, 2016
2 parents 54f9bda + 61b7c14 commit b516f76
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/liqp/filters/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class Filter extends LValue {
registerFilter(new liqp.filters.Map());
registerFilter(new Minus());
registerFilter(new Modulo());
registerFilter(new Newline_To_Br());
registerFilter(new Plus());
registerFilter(new Prepend());
registerFilter(new Remove());
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/liqp/filters/Newline_To_Br.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package liqp.filters;

class Newline_To_Br extends Filter {

/*
* newline_to_br(input)
*
* Add <br /> tags in front of all newlines in input string
*/
@Override
public Object apply(Object value, Object... params) {

return super.asString(value).replaceAll("[\n]", "<br />\n");
}
}
29 changes: 29 additions & 0 deletions src/test/java/liqp/filters/Newline_To_BrTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package liqp.filters;

import liqp.Template;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class Newline_To_BrTest {

@Test
public void applyTest() throws RecognitionException {

String[][] tests = {
{"{{ '' | newline_to_br }}", ""},
{"{{ nil | newline_to_br }}", ""},
{"{{ 'Line 1\nLine 2' | newline_to_br }}", "Line 1<br />\nLine 2"}
};

for (String[] test : tests) {

Template template = Template.parse(test[0]);
String rendered = String.valueOf(template.render());

assertThat(rendered, is(test[1]));
}
}
}

0 comments on commit b516f76

Please sign in to comment.