summaryrefslogtreecommitdiffstats
path: root/node-admin
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@yahooinc.com>2022-01-12 00:40:54 +0100
committerHåkon Hallingstad <hakon@yahooinc.com>2022-01-12 01:26:55 +0100
commit39466201d45b2326ea11349e647649c94f18b491 (patch)
treec81c182d3c4bd59e6673b150ec4fcfde99002c98 /node-admin
parentdf27a41055cad92454324c9b4bfd94b19abfd15b (diff)
Override template default for remove-newline in descriptor
Diffstat (limited to 'node-admin')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java3
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateDescriptor.java12
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java12
3 files changed, 18 insertions, 9 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
index 344424c7946..bb8466a0393 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java
@@ -16,9 +16,6 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template;
* id: a valid Java identifier
* </pre>
*
- * <p>If the directive's end delimiter (}) is preceded by a "-" char, then any newline (\n)
- * immediately following the end delimiter is removed.</p>
- *
* <p>To use the template create a form ({@link #newForm()}), fill the form (e.g.
* {@link Form#set(String, String) Form.set()}), and render the String ({@link Form#render()}).</p>
*
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateDescriptor.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateDescriptor.java
index 05d4f82d8d3..2735b05dc34 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateDescriptor.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateDescriptor.java
@@ -10,12 +10,14 @@ public class TemplateDescriptor {
private String startDelimiter = "%{";
private String endDelimiter = "}";
+ private boolean removeNewline = false;
public TemplateDescriptor() {}
public TemplateDescriptor(TemplateDescriptor that) {
this.startDelimiter = that.startDelimiter;
this.endDelimiter = that.endDelimiter;
+ this.removeNewline = that.removeNewline;
}
/** Use these delimiters instead of the standard "%{" and "}" to start and end a template directive. */
@@ -25,6 +27,16 @@ public class TemplateDescriptor {
return this;
}
+ /**
+ * Whether to remove a newline that immediately follows a non-variable directive. The opposite
+ * effect can be achieved by preceding the end delimiter with a "-" char, e.g. %{if foo-}.
+ */
+ public TemplateDescriptor setRemoveNewline(boolean removeNewline) {
+ this.removeNewline = removeNewline;
+ return this;
+ }
+
public String startDelimiter() { return startDelimiter; }
public String endDelimiter() { return endDelimiter; }
+ public boolean removeNewline() { return removeNewline; }
}
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
index 93a83a3d29f..a1a48c5e282 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateParser.java
@@ -96,7 +96,7 @@ class TemplateParser {
private void parseVariableSection(SectionList sectionList) {
var nameStart = new Cursor(current);
String name = parseId();
- parseEndDelimiter(true);
+ parseEndDelimiter(false);
sectionList.appendVariableSection(name, nameStart, current);
}
@@ -148,14 +148,14 @@ class TemplateParser {
private Optional<String> skipId() { return Token.skipId(current); }
- private boolean parseEndDelimiter(boolean skipNewline) {
- boolean removeNewline = current.skip(Token.REMOVE_NEWLINE_CHAR);
+ private void parseEndDelimiter(boolean allowSkipNewline) {
+ boolean removeNewlineCharPresent = current.skip(Token.REMOVE_NEWLINE_CHAR);
+
if (!current.skip(descriptor.endDelimiter()))
throw new BadTemplateException(current, "Expected section end (" + descriptor.endDelimiter() + ")");
- if (skipNewline && removeNewline)
+ // The presence of the remove-newline-char means the opposite behavior is wanted.
+ if (allowSkipNewline && (removeNewlineCharPresent != descriptor.removeNewline()))
current.skip('\n');
-
- return removeNewline;
}
}