]> WPIA git - gigi.git/commitdiff
UPD: new inline sprintf syntax
authorFelix Dörre <felix@dogcraft.de>
Fri, 13 Feb 2015 22:36:37 +0000 (23:36 +0100)
committerFelix Dörre <felix@dogcraft.de>
Fri, 13 Feb 2015 23:18:22 +0000 (00:18 +0100)
doc/TemplateSyntax.txt
src/org/cacert/gigi/output/template/SprintfCommand.java
src/org/cacert/gigi/output/template/Template.java
tests/org/cacert/gigi/template/TestTemplate.java

index 75a42b2b247f5360dc6d8b1d931503af163d9fa2..ad8e09ae170f0e9f82326af1f60b06199f37f890 100644 (file)
@@ -1,19 +1,21 @@
 A template is constructed from a charstream. Everything that is not in "<?" to "?>" will be outputted directly. Text in these markers will be interpreted is template scripting syntax. The following strings are valid:
 
-- <?=$variblename?> will output "variablename".
+General remarks:
+- $variablename: a variablename matches the regex [a-zA-Z0-9_-]
+Syntax:
+- <?=$variablename?> will output "variablename".
    if "variablename" is an Outputable output this thing recursively.
    else turn it into a String (Object.toString()) and output it.
+
+- <?=$!variablename?> will output the variable "variablename" but not HTML-escaped
    
 - <?=_This is free Text.?> will translate "This is free Text." into the users language, (escaped) and output it.
-   
-- <?=s,$var1,$var2,$var3,...,$varn,This %s is, %s free.?>
-  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 "}".
 
-- <?=$!variablename?> will output the variable "variablename" but not HTML-escaped
-- <?=s,$!variablename,My %s text?> will insert the variable "variablename" into the translated text but not HTML-escaped
-- <?=s,"some data",My %s text?> will insert "some data" into the translated text
-- <?=s,!"some data",My %s text?> will insert "some data" into the translated text literally (not HTML-escaped)
-  
 - <? if($variable) { ?> ... <? } ?>
   Output/execute the text until "<? } ?>" only if $variable is Boolean.TRUE (<=> !Boolean.FALSE) or not null. 
 - <? if(...) { ?> ... <? } else { ?> ... <? } ?>
index e2ff0ba6ce6395c4bfd3e3e8c587bc3c572c13fa..47389643995640afa854a63469abfc345b45bbf9 100644 (file)
@@ -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<String> var = new LinkedList<String>();
+        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<String, Object> 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)));
     }
 }
index b345a059f36873a53b8d2eb5eb3e87763aac71cf..4789a36573c6bae3b230f3485f543b85f50d2827 100644 (file)
@@ -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<String> store = new LinkedList<String>();
-            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
index 1e5a9fb6823215d4a5b0e762b7cbbba2b21ad3c6..7dca58e0eec50b785f818739cf0f3bb21cd4766f 100644 (file)
@@ -49,14 +49,14 @@ public class TestTemplate {
         vars.put("var", "val\">");
         vars.put("var2", "val3<\"");
         vars.put("var3", "val4>");
-        assertEquals("This val&quot;&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s text?>l"));
-        assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s text?>l"));
+        assertEquals("This val&quot;&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This ${var} text?>l"));
+        assertEquals("This val\"> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This $!{var} text?>l"));
 
-        assertEquals("This val&quot;&gt; val3&lt;&quot; the val4&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$var,$var2,$var3,This %s %s the %s text?>l"));
-        assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,$!var,$!var2,$!var3,This %s %s the %s text?>l"));
+        assertEquals("This val&quot;&gt; val3&lt;&quot; the val4&gt; textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This ${var} ${var2} the ${var3} text?>l"));
+        assertEquals("This val\"> val3<\" the val4> textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This $!{var} $!{var2} the $!{var3} text?>l"));
 
-        assertEquals("This blargh&lt;&gt;!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,\"blargh<>!\",!\"<>!\",This %s, %s text?>l"));
-        assertEquals("This blargh&lt;&gt;!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=s,\"blargh<>!\",!\"<>!\",This %s, %s?>l"));
+        assertEquals("This blargh&lt;&gt;!, <>! textl", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This !'blargh&lt;&gt;!', !'<>! 'text?>l"));
+        assertEquals("This blargh&lt;&gt;!, <>!l", testExecute(Language.getInstance(Locale.ENGLISH), vars, "<?=_This !'blargh&lt;&gt;!', !'<>!'?>l"));
     }
 
     @Test