checkpoint
authoradam <adam@megacz.com>
Sun, 2 Jul 2006 20:26:18 +0000 (16:26 -0400)
committeradam <adam@megacz.com>
Sun, 2 Jul 2006 20:26:18 +0000 (16:26 -0400)
darcs-hash:20060702202618-5007d-2c45a42b7ef390661bb8671af178c60fe944907c.gz

src/edu/berkeley/sbp/tib/TibDoc.java
src/edu/berkeley/sbp/util/Reflection.java
src/edu/berkeley/sbp/util/ToHTML.java
tests/tibdoc.g

index 3bc6f96..00ef9ce 100644 (file)
@@ -325,7 +325,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
     // Main //////////////////////////////////////////////////////////////////////////////
 
     public static class Dump implements Reflection.Show {
     // Main //////////////////////////////////////////////////////////////////////////////
 
     public static class Dump implements Reflection.Show {
-        public String toString() { return Reflection.show(this); }
+        public String toString() { return Reflection.show((Reflection.Show)this); }
     }
 
     public static class TD {
     }
 
     public static class TD {
@@ -355,43 +355,125 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public @arg Object val;
         }
 
             public @arg Object val;
         }
 
-        public static class Paragraph extends Dump { }
+        public abstract static class Paragraph extends Dump implements ToHTML { }
+
         public @tag("P") static class P extends Paragraph {
             public Text[] text;
         public @tag("P") static class P extends Paragraph {
             public Text[] text;
+            public P() { }
+            public P(Text[] text) { this.text = text; }
+            public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
+            public String toString() {
+                StringBuffer sb = new StringBuffer();
+                ToHTML.HTML h = new ToHTML.HTML(sb);
+                toHTML(h);
+                return sb.toString();
+            }
         }
         }
-        public @tag("HR") static class HR extends Paragraph { }
+
+        public @tag("HR") static class HR extends Paragraph {
+            public void toHTML(HTML h) { h.tag("hr"); }
+        }
+
         public @tag("Blockquote") static class Blockquote extends Paragraph {
         public @tag("Blockquote") static class Blockquote extends Paragraph {
-            Object text;
+            Text[] text;
+            public void toHTML(HTML h) { h.tag("blockquote", new P(text)); }
         }
 
         }
 
-        public static class Text extends Dump { }
+        public abstract static class Text extends Dump implements ToHTML { }
         public @tag static class Chars extends Text {
             public String text;
         public @tag static class Chars extends Text {
             public String text;
+            public Chars() { }
+            public Chars(String text) { this.text = text; }
+            public void toHTML(HTML h) { h.appendText(" " + text + " "); }
+            public String toString() { return text; }
+        }
+        public @tag static class Block extends Text {
+            public Text[] text;
+            public void toHTML(HTML h) { for(Text t : text) t.toHTML(h); }
         }
         }
-        public @tag static class Block extends Text { public Text[] text; }
         public static class TextWrap extends Text {
             public Text text;
         public static class TextWrap extends Text {
             public Text text;
+            public void toHTML(HTML h) {
+                if (htmlTag()!=null)
+                    h.tag(htmlTag(), htmlTagParams(), text);
+                else
+                    text.toHTML(h);
+            }
+            public String   htmlTag() { return null; }
+            public Object[] htmlTagParams() { return null; }
         }
         }
-        public static @tag class Verbatim   extends Text { public char[] c; }
+        public static @tag class Verbatim   extends Text { public char[] c; public void toHTML(HTML h) { } }
         //public @tag class Blockquote extends TextWrap { }
         //public @tag class Blockquote extends TextWrap { }
-        public static @tag class Underline extends TextWrap { }
-        public static @tag class Footnote extends TextWrap { }
-        public static @tag class TT extends TextWrap { }
+        public static @tag class Underline extends TextWrap { public String htmlTag() { return "u"; } }
+        public static @tag class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
+        public static @tag class TT extends TextWrap { public String htmlTag() { return "tt"; } }
         //public @tag class Citation extends Text {       "[" word "]" }
         //public @tag class Citation extends Text {       "[" word "]" }
-        public static @tag class Strikethrough extends TextWrap { }
-        public static @tag class Superscript extends TextWrap { }
-        public static @tag class Subscript extends TextWrap { }
-        public static @tag class Smallcap extends TextWrap { }
-        public static @tag class Keyword extends TextWrap { }
-        public static @tag class Bold extends TextWrap { }
-        public static @tag class Italic extends TextWrap { }
-
-        public static class Command extends Text { }
-        public static @tag class Today extends Command { }
-        public static @tag class LineBreak extends Command { }
-
-        public static class Glyph extends Text { }
-        public static @tag("emdash") class Emdash extends Glyph { }
+        public static @tag class Strikethrough extends TextWrap { public String htmlTag() { return "strikethrough"; } }
+        public static @tag class Superscript extends TextWrap { public String htmlTag() { return "sup"; } }
+        public static @tag class Subscript extends TextWrap { public String htmlTag() { return "sub"; } }
+        public static @tag class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } }
+        public static @tag class Keyword extends TextWrap { public String htmlTag() { return "sc"; } }
+        public static @tag class Bold extends TextWrap { public String htmlTag() { return "b"; } }
+        public static @tag class Italic extends TextWrap { public String htmlTag() { return "i"; } }
+
+        public abstract static class Command extends Text { }
+        public static @tag class Today extends Command { public void toHTML(HTML h) { } }
+        public static @tag class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
+
+        public abstract static class Glyph extends Text { }
+        public static @tag("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.append("&emdash;"); } }
+
+        public static class Link extends Text {
+            public Text[] t;
+            public Url u;
+            public @tag("LinkText") Link(Text[] t, Url u)  { this.t = t; this.u = u; }
+            public @tag("LinkChars") Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); }
+            public void toHTML(HTML h) {
+                h.tag("a",
+                      new Object[] { "href", u==null ? "" : u.toString() },
+                      new P(t));
+            }
+        }
+
+        public static class Host {
+            public String name;
+            public String toString() { return name; }
+            public @tag("DNS") Host(String[][] parts) {
+                name = "";
+                for(String[] s : parts) {
+                    for(String ss : s)
+                        name += ss;
+                    name += ".";
+                }
+            }
+            public @tag("IP")  Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; }
+        }
+
+        public static class Url extends Text {
+            public String   method;
+            public Host     host;
+            public String   user;
+            public String   pass;
+            public String   port;
+            public String   path;
+            public @tag("URL") Url(String method, String[] login, Host host, String port, String path) {
+                this.method = method;
+                this.user = login==null ? null : login.length >= 1 ? login[0] : null;
+                this.pass = login==null ? null : login.length >= 2 ? login[1] : null;
+                this.host = host;
+                this.port = port;
+                this.path = path;
+            }
+            public @tag("Mailto") Url(String email) { this("mailto", null, null, "25", email); }
+            public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
+            public String toString() {
+                return method + "://" + host + "/" + path;
+            }
+        }
+        public static @tag("lf")        String lf() { return "\r"; }
+        public static @tag("cr")        String cr() { return "\n"; }
+        public static @tag("\"\"")      String empty() { return ""; }
+        public static @tag("urlescape") String urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0')))+""; }
     }
 
     public static void main(String[] s) throws Exception {
     }
 
     public static void main(String[] s) throws Exception {
@@ -403,6 +485,9 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                                             TibDoc.TD.Doc.class,
                                             TibDoc.TD.Header.class,
                                             TibDoc.TD.Section.class,
                                             TibDoc.TD.Doc.class,
                                             TibDoc.TD.Header.class,
                                             TibDoc.TD.Section.class,
+                                            TibDoc.TD.Url.class,
+                                            TibDoc.TD.Host.class,
+                                            TibDoc.TD.Link.class,
                                             TibDoc.TD.Body.class,
                                             TibDoc.TD.Paragraph.class,
                                             TibDoc.TD.P.class,
                                             TibDoc.TD.Body.class,
                                             TibDoc.TD.Paragraph.class,
                                             TibDoc.TD.P.class,
index 247652a..da16336 100644 (file)
@@ -53,19 +53,7 @@ public final class Reflection {
 
     public static String show(Object o) {
         if (o==null) return "null";
 
     public static String show(Object o) {
         if (o==null) return "null";
-        if (o instanceof Show) {
-            StringBuffer ret = new StringBuffer();
-            for(Field f : o.getClass().getFields()) {
-                ret.append("\n" + f.getName() + " = ");
-                try {
-                    ret.append(show(f.get(o)));
-                } catch (Exception e) {
-                    ret.append("**"+e+"**");
-                    throw new RuntimeException(e);
-                }
-            }
-            return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
-        }
+        if (o instanceof Show) return ((Show)o).toString();
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
         if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName();
         Object[] arr = (Object[])o;
         StringBuffer ret = new StringBuffer();
@@ -76,6 +64,20 @@ public final class Reflection {
         return ret.toString();
     }
 
         return ret.toString();
     }
 
+    public static String show(Show o) {
+        StringBuffer ret = new StringBuffer();
+        for(Field f : o.getClass().getFields()) {
+            ret.append("\n" + f.getName() + " = ");
+            try {
+                ret.append(show(f.get(o)));
+            } catch (Exception e) {
+                ret.append("**"+e+"**");
+                throw new RuntimeException(e);
+            }
+        }
+        return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}";
+    }
+
     public static Object lub(Object argo) {
         if (argo instanceof Object[]) return lub((Object[])argo);
         return argo;
     public static Object lub(Object argo) {
         if (argo instanceof Object[]) return lub((Object[])argo);
         return argo;
@@ -197,5 +199,6 @@ public final class Reflection {
         return null;
     }
 
         return null;
     }
 
-    public static interface Show { }
+    public static interface Show {
+    }
 }
 }
index fe3b3a3..34d03bf 100644 (file)
@@ -14,6 +14,11 @@ public interface ToHTML {
             sb.append(s);
         }
 
             sb.append(s);
         }
 
+        public void appendText(String s) {
+            /* FIXME: escapify this!!! */
+            sb.append(s);
+        }
+
         public void entity(int entity) { sb.append("&#"+entity+";"); }
         public void entity(String entity) { sb.append("&"+entity+";"); }
 
         public void entity(int entity) { sb.append("&#"+entity+";"); }
         public void entity(String entity) { sb.append("&"+entity+";"); }
 
index ed71ad3..88ee6f8 100644 (file)
@@ -115,8 +115,8 @@ styled       = Underline::     "__" text "__"
 
 block         = { text }
 
 
 block         = { text }
 
-link          = Link:: text:({ text })      "->" href:(url|email)
-              > Link:: text:alphanum++  !ws "->" href:(url|email)
+link          = LinkText:: text:({ text })      "->" href:(url|email)
+              > LinkChars:: text:alphanum++  !ws "->" href:(url|email)
 
 structured    = command & "\\" !([a-zA-Z0-9]++) block?
               > glyph
 
 structured    = command & "\\" !([a-zA-Z0-9]++) block?
               > glyph
@@ -141,8 +141,15 @@ urlc         = [a-zA-Z0-9;/?:&=$\-_.+@]
 urlv         = urlc | [%]
 urlchar      = urlc
              | urlescape:: "%" [0-9] [0-9]
 urlv         = urlc | [%]
 urlchar      = urlc
              | urlescape:: "%" [0-9] [0-9]
-url          = "mailto" ":"   email -> ~urlv
-             > URL:: method:method "://" login:url_login? host:host port:(":" nums)? path:("/" urlpath)? -> ~urlv
+url          = Mailto:: "mailto" ":"   email -> ~urlv
+             > URL::
+                  method:method 
+                  "://"
+                  login:url_login?
+                  host:host
+                  port:(":" nums)?
+                  path:("/" urlpath)?
+                     -> ~urlv
 url_login    = Login:: username:username password:(":" password) "@"
 method       = [+\-.a-z0-9]+
 domain       = (part +/ ".") -> ~"."
 url_login    = Login:: username:username password:(":" password) "@"
 method       = [+\-.a-z0-9]+
 domain       = (part +/ ".") -> ~"."
@@ -163,7 +170,7 @@ word       = alphanum++
            | quoted
 
 quoted     = "\"" ((~[\"\\] | escaped)+) "\""
            | quoted
 
 quoted     = "\"" ((~[\"\\] | escaped)+) "\""
-           | "":: "\"\""
+           | "\"\"":: "\"\""
 escaped    = lf:: "\\n"
            | cr:: "\\r"
            | "\\" ~[nr]
 escaped    = lf:: "\\n"
            | cr:: "\\r"
            | "\\" ~[nr]
@@ -172,7 +179,6 @@ escaped    = lf:: "\\n"
 // Chars ///////////////////////////////////////////////////////////////
 
 alpha      = [a-zA-Z]
 // Chars ///////////////////////////////////////////////////////////////
 
 alpha      = [a-zA-Z]
-//num        = [0-9]
 alphanum   = [a-zA-Z0-9]
 sym        = ~[a-zA-Z0-9\ \r\n=\">]
 
 alphanum   = [a-zA-Z0-9]
 sym        = ~[a-zA-Z0-9\ \r\n=\">]