From 4a350c2f763a1ab772a2baa7f5d95f37c229ec9d Mon Sep 17 00:00:00 2001 From: Patrick Hensley Date: Fri, 23 Jan 2026 15:07:32 -0500 Subject: [PATCH] Add line-breaks formatter and test cases. --- .../com/squarespace/template/Patterns.java | 1 + .../template/plugins/CoreFormatters.java | 22 +++++++++++++++- .../template/plugins/CoreFormattersTest.java | 11 ++++++++ .../template/plugins/f-line-breaks-1.html | 26 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/squarespace/template/plugins/f-line-breaks-1.html diff --git a/core/src/main/java/com/squarespace/template/Patterns.java b/core/src/main/java/com/squarespace/template/Patterns.java index 2954dab..e9820b7 100644 --- a/core/src/main/java/com/squarespace/template/Patterns.java +++ b/core/src/main/java/com/squarespace/template/Patterns.java @@ -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]+"); diff --git a/core/src/main/java/com/squarespace/template/plugins/CoreFormatters.java b/core/src/main/java/com/squarespace/template/plugins/CoreFormatters.java index 6331921..15b9aed 100644 --- a/core/src/main/java/com/squarespace/template/plugins/CoreFormatters.java +++ b/core/src/main/java/com/squarespace/template/plugins/CoreFormatters.java @@ -83,9 +83,10 @@ public void registerFormatters(SymbolTable 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()); @@ -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("
"); + var.set(replaced); + } + + } + /** * OUTPUT */ diff --git a/core/src/test/java/com/squarespace/template/plugins/CoreFormattersTest.java b/core/src/test/java/com/squarespace/template/plugins/CoreFormattersTest.java index 1e9043a..900895d 100644 --- a/core/src/test/java/com/squarespace/template/plugins/CoreFormattersTest.java +++ b/core/src/test/java/com/squarespace/template/plugins/CoreFormattersTest.java @@ -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; @@ -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(); @@ -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 \"", "
"); + assertFormatter(LINE_BREAKS, "\"A\\nB\\n\\n\\nC\\nD\"", "A
B


C
D"); + } + @Test public void testOutput() throws CodeException { CodeMaker mk = maker(); diff --git a/core/src/test/resources/com/squarespace/template/plugins/f-line-breaks-1.html b/core/src/test/resources/com/squarespace/template/plugins/f-line-breaks-1.html new file mode 100644 index 0000000..16c6c5a --- /dev/null +++ b/core/src/test/resources/com/squarespace/template/plugins/f-line-breaks-1.html @@ -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 +|
| + + + +"abc" + +"A
B
C" + +"
Long

quote

with

several


line

breaks
"