Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -53,6 +53,7 @@ public class Patterns {
public static final char POUND_CHAR = '#';

public static final Pattern ONESPACE = Pattern.compile("\\s");
public static final Pattern ONENEWLINE = Pattern.compile("\n");
public static final Pattern WHITESPACE_RE = Pattern.compile("\\s+");
public static final Pattern WHITESPACE_NBSP = Pattern.compile("[\\s\u200b\u00a0]+");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public void registerFormatters(SymbolTable<StringView, Formatter> table) {
table.add(new JsonFormatter());
table.add(new JsonPrettyFormatter());
table.add(new KeyByFormatter());
table.add(new OutputFormatter());
table.add(new LineBreaksFormatter());
table.add(new LookupFormatter());
table.add(new ModFormatter());
table.add(new OutputFormatter());
table.add(new PluralizeFormatter());
table.add(new PropFormatter());
table.add(new RawFormatter());
Expand Down Expand Up @@ -581,6 +582,25 @@ public void apply(final Context ctx, Arguments args, Variables variables) throws
}


/**
* LINE-BREAKS
*/
public static class LineBreaksFormatter extends BaseFormatter {

public LineBreaksFormatter() {
super("line-breaks", false);
}

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
String value = var.node().asText();
String replaced = Patterns.ONENEWLINE.matcher(value).replaceAll("<br/>");
var.set(replaced);
}

}

/**
* OUTPUT
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.squarespace.template.plugins.CoreFormatters.JsonFormatter;
import com.squarespace.template.plugins.CoreFormatters.JsonPrettyFormatter;
import com.squarespace.template.plugins.CoreFormatters.KeyByFormatter;
import com.squarespace.template.plugins.CoreFormatters.LineBreaksFormatter;
import com.squarespace.template.plugins.CoreFormatters.LookupFormatter;
import com.squarespace.template.plugins.CoreFormatters.ModFormatter;
import com.squarespace.template.plugins.CoreFormatters.OutputFormatter;
Expand Down Expand Up @@ -86,6 +87,7 @@ public class CoreFormattersTest extends UnitTestBase {
private static final Formatter JSON = new JsonFormatter();
private static final Formatter JSON_PRETTY = new JsonPrettyFormatter();
private static final Formatter KEY_BY = new KeyByFormatter();
private static final Formatter LINE_BREAKS = new LineBreaksFormatter();
private static final Formatter OUTPUT = new OutputFormatter();
private static final Formatter LOOKUP = new LookupFormatter();
private static final Formatter MOD = new ModFormatter();
Expand Down Expand Up @@ -542,6 +544,15 @@ public void testKeyBy() throws CodeException {
runner.exec("f-key-by-%N.html");
}

@Test
public void testLineBreaks() throws CodeException {
runner.exec("f-line-breaks-%N.html");

assertFormatter(LINE_BREAKS, "\"\"", "");
assertFormatter(LINE_BREAKS, "\" \\n \"", " <br/> ");
assertFormatter(LINE_BREAKS, "\"A\\nB\\n\\n\\nC\\nD\"", "A<br/>B<br/><br/><br/>C<br/>D");
}

@Test
public void testOutput() throws CodeException {
CodeMaker mk = maker();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:JSON
{
"quotes": [
"| \n |",
"",
"\"abc\"",
"\"A\nB\nC\"",
"\"\nLong\n\nquote\n\nwith\n\nseveral\n\n\nline\n\nbreaks\n\""
]
}

:TEMPLATE
{.repeated section quotes}
{@|line-breaks}
{.end}

:OUTPUT
| <br/> |



"abc"

"A<br/>B<br/>C"

"<br/>Long<br/><br/>quote<br/><br/>with<br/><br/>several<br/><br/><br/>line<br/><br/>breaks<br/>"
Loading