preliminary experiments with a wiki tag
[org.ibex.xt.git] / src / org / ibex / xt / Template.java
index e941364..36611cc 100644 (file)
@@ -109,6 +109,7 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
                 transform(n, scope);
                 return graft((Node.Stream.Functor)("true".equals(n.attr("if"))?new DropTag():new DropAll()), n).upstreamRead(n);
             case "js":       return graft(new JsTag(scope), n).upstreamRead(n);
+            case "wiki":     return graft(new WikiTag(n, scope), n).upstreamRead(n);
             case "foreach":  return graft(new ForEach(n, scope), n).upstreamRead(n);
             case "children":
                 if (children == null) return true;
@@ -134,6 +135,7 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
                 JSArray a = ((JSArray)exec("return (" + n.attr("in").toString() + ");", this.scope = s));
                 while(true) {
                     JS o = a.call(JSU.S("pop"), new JS[] { });
+                   System.out.println("called pop on a "+a.getClass().getName()+" of length " +a.size()+" , got " + (o==null ? null : o.getClass().getName()));
                     if (o == null) break;
                     array.push(o);
                 }
@@ -163,6 +165,28 @@ public class Template extends Node.Stream.Filter implements Node.Stream.Functor
         }
     }
 
+    private class WikiTag implements Node.Stream.Functor {
+        public WikiTag(Node n, Scope s) { }
+        public Node.Stream wrap(final Node.Stream kids) {
+            return new Node.Stream() {
+                    public boolean _read(Node n) {
+                        if (!kids.read(n)) return false;
+                        System.out.println("kids.cdata == " + n.cdata);
+                        if (n.cdata == null) return true;
+
+                        // FIXME: links, code, and math
+                        n.cdata = n.cdata.replaceAll("__([^_]+)__", "<u>$1</u>");
+                        n.cdata = n.cdata.replaceAll("\\~\\~([^\\~]+)\\~\\~", "<i>$1</i>");
+                        n.cdata = n.cdata.replaceAll("\\*\\*([^\\*]+)\\*\\*", "<b>$1</b>");
+                        n.cdata = n.cdata.replaceAll("==([^=]+)==", "<h2>$1</h2>");
+                        n.cdata = n.cdata.replaceAll("=<h2>([^=]+)</h2>=", "<h3>$1</h3>");
+                        n.cdata = n.cdata.replaceAll("=<h3>([^=]+)</h3>=", "<h4>$1</h4>");
+                        n.cdata = n.cdata.replaceAll("(\n|\r\n)[ ]*(\n|\r\n)", "\n<br/><br/>\n");
+                        return true;
+                    } };
+        }
+    }
+
     private class InputTag implements Node.Stream.Functor {
         final String fieldName;
         public InputTag(String fieldName) { this.fieldName = fieldName; }