From 0d1d3c357c8274c519270abc61b668bff637c40a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Fri, 13 Feb 2015 23:36:37 +0100 Subject: [PATCH] UPD: new inline sprintf syntax --- doc/TemplateSyntax.txt | 20 ++++--- .../gigi/output/template/SprintfCommand.java | 58 +++++++++++++++---- .../cacert/gigi/output/template/Template.java | 31 ++++------ .../cacert/gigi/template/TestTemplate.java | 12 ++-- 4 files changed, 74 insertions(+), 47 deletions(-) diff --git a/doc/TemplateSyntax.txt b/doc/TemplateSyntax.txt index 75a42b2b..ad8e09ae 100644 --- a/doc/TemplateSyntax.txt +++ b/doc/TemplateSyntax.txt @@ -1,19 +1,21 @@ A template is constructed from a charstream. Everything that is not in "" will be outputted directly. Text in these markers will be interpreted is template scripting syntax. The following strings are valid: -- will output "variablename". +General remarks: +- $variablename: a variablename matches the regex [a-zA-Z0-9_-] +Syntax: +- will output "variablename". if "variablename" is an Outputable output this thing recursively. else turn it into a String (Object.toString()) and output it. + +- will output the variable "variablename" but not HTML-escaped - will translate "This is free Text." into the users language, (escaped) and output it. - -- - Translate the last string. Output $var1,...,$varn replaced into the %s positions in the translated string. + Text may not contain "?>". + If the text contains "$" or "!'" it is interpreted as "advanced replacement". + - ${variablename} is interpreted as "output this variable at this point" + - !'literal content' output "literal content" here and do not translate or escape. (literal content may not contain any of: {}'$ ) + Then the whole text than also may not contain "{" and "}". -- will output the variable "variablename" but not HTML-escaped -- will insert the variable "variablename" into the translated text but not HTML-escaped -- will insert "some data" into the translated text -- will insert "some data" into the translated text literally (not HTML-escaped) - - ... Output/execute the text until "" only if $variable is Boolean.TRUE (<=> !Boolean.FALSE) or not null. - ... ... diff --git a/src/org/cacert/gigi/output/template/SprintfCommand.java b/src/org/cacert/gigi/output/template/SprintfCommand.java index e2ff0ba6..47389643 100644 --- a/src/org/cacert/gigi/output/template/SprintfCommand.java +++ b/src/org/cacert/gigi/output/template/SprintfCommand.java @@ -1,8 +1,11 @@ package org.cacert.gigi.output.template; import java.io.PrintWriter; +import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.cacert.gigi.localisation.Language; import org.cacert.gigi.util.HTMLEncoder; @@ -18,23 +21,56 @@ public final class SprintfCommand implements Outputable { this.store = store.toArray(new String[store.size()]); } + private static final String VARIABLE = "\\$!?\\{[a-zA-Z0-9_-]+\\}"; + + private static final Pattern processingInstruction = Pattern.compile("(" + VARIABLE + ")|(!'[^{}'\\$]*)'"); + + public SprintfCommand(String content) { + StringBuffer raw = new StringBuffer(); + List var = new LinkedList(); + int counter = 0; + Matcher m = processingInstruction.matcher(content); + int last = 0; + while (m.find()) { + raw.append(content.substring(last, m.start())); + String group = null; + if ((group = m.group(1)) != null) { + var.add(group); + } else if ((group = m.group(2)) != null) { + var.add(group); + } else { + throw new Error("Regex is broken??"); + } + last = m.end(); + raw.append("{" + (counter++) + "}"); + } + raw.append(content.substring(last)); + text = raw.toString(); + store = var.toArray(new String[var.size()]); + } + + private final Pattern replacant = Pattern.compile("\\{([0-9]+)\\}"); + @Override public void output(PrintWriter out, Language l, Map vars) { - String[] parts = l.getTranslation(text).split("%s", -1); - String[] myvars = store; - out.print(HTMLEncoder.encodeHTML(parts[0])); - for (int j = 1; j < parts.length; j++) { - String var = myvars[j - 1]; + String parts = l.getTranslation(text); + Matcher m = replacant.matcher(parts); + int pos = 0; + while (m.find()) { + out.print(HTMLEncoder.encodeHTML(parts.substring(pos, m.start()))); + String var = store[Integer.parseInt(m.group(1))]; if (var.startsWith("$!")) { - Template.outputVar(out, l, vars, myvars[j - 1].substring(2), true); - } else if (var.startsWith("!\"")) { + Template.outputVar(out, l, vars, var.substring(3, var.length() - 1), true); + } else if (var.startsWith("!'")) { out.print(var.substring(2)); - } else if (var.startsWith("\"")) { - out.print(HTMLEncoder.encodeHTML(var.substring(1))); + } else if (var.startsWith("$")) { + Template.outputVar(out, l, vars, var.substring(2, var.length() - 1), false); } else { - Template.outputVar(out, l, vars, myvars[j - 1].substring(1), false); + throw new Error("Processing error in template."); } - out.print(HTMLEncoder.encodeHTML(parts[j])); + pos = m.end(); + } + out.print(HTMLEncoder.encodeHTML(parts.substring(pos))); } } diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index b345a059..4789a365 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -36,13 +36,15 @@ public class Template implements Outputable { } public TemplateBlock getBlock(String reqType) { - if (endType == null && reqType == null) + if (endType == null && reqType == null) { return block; + } if (endType == null || reqType == null) { throw new Error("Invalid block type: " + endType); } - if (endType.equals(reqType)) + if (endType.equals(reqType)) { return block; + } throw new Error("Invalid block type: " + endType); } } @@ -149,30 +151,17 @@ public class Template implements Outputable { private Outputable parseCommand(String s2) { if (s2.startsWith("=_")) { final String raw = s2.substring(2); - return new TranslateCommand(raw); + if ( !s2.contains("$") && !s2.contains("!'")) { + return new TranslateCommand(raw); + } else { + return new SprintfCommand(raw); + } } else if (s2.startsWith("=$")) { final String raw = s2.substring(2); return new OutputVariableCommand(raw); - } else if (s2.startsWith("=s,")) { - String command = s2.substring(3); - final LinkedList store = new LinkedList(); - while (command.startsWith("$") || command.startsWith("\"") || command.startsWith("!\"")) { - int idx; - if (command.startsWith("\"") || command.startsWith("!\"")) { - idx = command.indexOf("\"", command.charAt(0) == '!' ? 2 : 1) + 1; - store.add(command.substring(0, idx - 1)); - } else { - idx = command.indexOf(","); - store.add(command.substring(0, idx)); - } - command = command.substring(idx + 1); - } - final String text = command; - return new SprintfCommand(text, store); } else { - System.out.println("Unknown processing instruction: " + s2); + throw new Error("Unknown processing instruction: " + s2); } - return null; } @Override diff --git a/tests/org/cacert/gigi/template/TestTemplate.java b/tests/org/cacert/gigi/template/TestTemplate.java index 1e5a9fb6..7dca58e0 100644 --- a/tests/org/cacert/gigi/template/TestTemplate.java +++ b/tests/org/cacert/gigi/template/TestTemplate.java @@ -49,14 +49,14 @@ public class TestTemplate { vars.put("var", "val\">"); vars.put("var2", "val3<\""); vars.put("var3", "val4>"); - assertEquals("This val"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); - assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); + assertEquals("This val"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); + assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); - assertEquals("This val"> val3<" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); - assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); + assertEquals("This val"> val3<" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); + assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "l")); - assertEquals("This blargh<>!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "!\",!\"<>!\",This %s, %s text?>l")); - assertEquals("This blargh<>!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "!\",!\"<>!\",This %s, %s?>l")); + assertEquals("This blargh<>!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "! 'text?>l")); + assertEquals("This blargh<>!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "!'?>l")); } @Test -- 2.39.2