X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2Ftemplate%2FTemplate.java;h=8c633c422934d415b7da3b813bc5d44512f00753;hb=7add0317b164483ad82ed696cc8d63c516273714;hp=15f3408d28ce41d10d55b4f9491fdaf223cb0d84;hpb=d7be034f96e06985f57d86d2779c434276b5bd4d;p=gigi.git diff --git a/src/org/cacert/gigi/output/template/Template.java b/src/org/cacert/gigi/output/template/Template.java index 15f3408d..8c633c42 100644 --- a/src/org/cacert/gigi/output/template/Template.java +++ b/src/org/cacert/gigi/output/template/Template.java @@ -27,7 +27,7 @@ import org.cacert.gigi.util.HTMLEncoder; */ public class Template implements Outputable { - private static class ParseResult { + protected static class ParseResult { TemplateBlock block; @@ -75,8 +75,7 @@ public class Template implements Outputable { * the URL to load the template from. UTF-8 is chosen as charset. */ public Template(URL u) { - try { - Reader r = new InputStreamReader(u.openStream(), "UTF-8"); + try (Reader r = new InputStreamReader(u.openStream(), "UTF-8")) { try { if (u.getProtocol().equals("file")) { source = new File(u.toURI()); @@ -86,7 +85,6 @@ public class Template implements Outputable { e.printStackTrace(); } data = parse(r).getBlock(null); - r.close(); } catch (IOException e) { throw new Error(e); } @@ -108,7 +106,11 @@ public class Template implements Outputable { } } - private ParseResult parse(Reader r) throws IOException { + protected ParseResult parse(Reader r) throws IOException { + return parseContent(r); + } + + protected ParseResult parseContent(Reader r) throws IOException { LinkedList splitted = new LinkedList(); LinkedList commands = new LinkedList(); StringBuffer buf = new StringBuffer(); @@ -121,6 +123,9 @@ public class Template implements Outputable { break outer; } buf.append((char) ch); + if (endsWith(buf, "\\\n")) { + buf.delete(buf.length() - 2, buf.length()); + } } buf.delete(buf.length() - 2, buf.length()); splitted.add(buf.toString()); @@ -139,10 +144,10 @@ public class Template implements Outputable { if (m.matches()) { String type = m.group(1); String variable = m.group(2); - ParseResult body = parse(r); + ParseResult body = parseContent(r); if (type.equals("if")) { if ("else".equals(body.getEndType())) { - commands.add(new IfStatement(variable, body.getBlock("else"), parse(r).getBlock("}"))); + commands.add(new IfStatement(variable, body.getBlock("else"), parseContent(r).getBlock("}"))); } else { commands.add(new IfStatement(variable, body.getBlock("}"))); } @@ -188,6 +193,11 @@ public class Template implements Outputable { @Override public void output(PrintWriter out, Language l, Map vars) { + tryReload(); + data.output(out, l, vars); + } + + protected void tryReload() { if (source != null && lastLoaded < source.lastModified()) { try { System.out.println("Reloading template.... " + source); @@ -199,10 +209,12 @@ public class Template implements Outputable { e.printStackTrace(); } } - data.output(out, l, vars); } protected static void outputVar(PrintWriter out, Language l, Map vars, String varname, boolean unescaped) { + if (vars.containsKey(Outputable.OUT_KEY_PLAIN)) { + unescaped = true; + } Object s = vars.get(varname); if (s == null) { @@ -212,6 +224,8 @@ public class Template implements Outputable { ((Outputable) s).output(out, l, vars); } else if (s instanceof DayDate) { out.print(DateSelector.getDateFormat().format(((DayDate) s).toDate())); + } else if (s instanceof Boolean) { + out.print(((Boolean) s) ? l.getTranslation("yes") : l.getTranslation("no")); } else if (s instanceof Date) { SimpleDateFormat sdfUI = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");