make ANSI colors disable-able
[sbp.git] / src / edu / berkeley / sbp / tib / TibDoc.java
index 42daf15..273c639 100644 (file)
@@ -1,6 +1,4 @@
-// Copyright 2005 the Contributors, as shown in the revision logs.
-// Licensed under the Apache Public Source License 2.0 ("the License").
-// You may not use this file except in compliance with the License.
+// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp.tib;
 import edu.berkeley.sbp.*;
@@ -394,7 +392,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public P() { }
             public P(Text[] text) { this.text = text; }
             public P(String string) { this.text = new Text[] { new Chars(string) }; }
-            public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
+            public void toHTML(HTML h) { if (text != null) h.append(text); }
             public String toString() {
                 StringBuffer sb = new StringBuffer();
                 ToHTML.HTML h = new ToHTML.HTML(sb);
@@ -408,14 +406,15 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public @bind.arg String password;
         }
 
-        public static @bind.as("Euro") Object euro() { return null; }
         public static @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
-        public static @bind.as("Symbol") Object sym(Object o) { return null; }
+        public static @bind.as("Symbol") Object sym(String s) { return new Chars(s); }
 
-        public static abstract class List extends Text {
-            public @bind.arg Text[][] points;
+        public static abstract class List extends Paragraph {
+            public @bind.arg Text[] preface;
+            public @bind.arg Text[][]  points;
             public abstract String tag();
             public void toHTML(ToHTML.HTML sb) {
+                sb.append(preface);
                 sb.openTag(tag());
                 for(Text[] t : points)
                     sb.tag("li", t);
@@ -448,20 +447,20 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         }
 
         public abstract static class Text extends Dump implements ToHTML { }
+
+        public @bind static class Space extends Chars {
+            public Space() { super(" "); }
+        }
         public @bind 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 void toHTML(HTML h) { h.appendText(text); }
             public String toString() { return text; }
         }
         public @bind static class Block extends Text {
             public Text[] text;
-            public void toHTML(HTML h) {
-                for(Text t : text)
-                    if (t != null)
-                        t.toHTML(h);
-            }
+            public void toHTML(HTML h) { h.append(text); }
         }
         public static class TextWrap extends Text {
             public @bind.arg Text[] text;
@@ -469,7 +468,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                 if (htmlTag()!=null) {
                     h.openTag(htmlTag(), htmlTagParams());
                 }
-                for(Text t : text) t.toHTML(h);
+                h.append(text);
                 if (htmlTag()!=null) {
                     h.closeTag(htmlTag());
                 }
@@ -477,7 +476,8 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public String   htmlTag() { return null; }
             public Object[] htmlTagParams() { return null; }
         }
-        public static @bind class Verbatim   extends Text {
+
+        public static @bind class Verbatim  extends Text {
             public @bind.arg String s;
             public void toHTML(HTML h) {
                 h.openTag("div", new Object[] { "class", "terminal" });
@@ -493,6 +493,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
                 h.closeTag("div");
             }
         }
+
         //public @bind class Blockquote extends TextWrap { }
         public static @bind class Underline extends TextWrap { public String htmlTag() { return "u"; } }
         public static @bind class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
@@ -510,23 +511,20 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         public static @bind class Today extends Command { public void toHTML(HTML h) { } }
         public static @bind class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
 
-        public static abstract class Glyph extends Text implements ToHTML { }
-        public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.appendLiterally("—"); } }
-        public static @bind.as("ellipses") class Ellipses extends Glyph { public void toHTML(HTML h) { h.appendLiterally("…"); } }
-        public static @bind.as("r") class RegTm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("®"); } }
-        public static @bind.as("c") class Copyright extends Glyph { public void toHTML(HTML h) { h.appendLiterally("©"); } }
-        public static @bind.as("tm") class Tm extends Glyph { public void toHTML(HTML h) { h.appendLiterally("&;"); } }
-        public static @bind.as("euro") class Euro extends Glyph { public void toHTML(HTML h) { h.appendLiterally("€"); } }
+        public static @bind.as("emdash") Entity emdash() { return new Entity("mdash"); }
+        public static @bind.as("ellipses") Entity ellipses() { return new Entity("#8230"); }
+        public static @bind.as("r") Entity r() { return new Entity("#xAE"); }
+        public static @bind.as("c") Entity c() { return new Entity("#xA9"); }
+        public static @bind.as("tm") Entity tm() { return new Entity(""); }
+        public static @bind.as("euro") Entity euro() { return new Entity("euro"); }
 
-        public static @bind.as("#") Text comment() { return new Chars(""); }
         public static class Entity extends Text implements ToHTML {
-            public int code;
             public String name;
-            public Entity(String name, int code) { this.name = name; this.code = code; }
+            public String code;
+            public Entity(String code) { this.name = ""; this.code = code; }
+            public Entity(String name, int code) { this.code = "#"+code; this.name = name; }
             public Entity(String name, String abbrev, int code) { this(name, code); }
-            public void toHTML(HTML h) {
-                h.appendLiterally("&#x"+Integer.toString(code, 16)+";");
-            }
+            public void toHTML(HTML h) { h.appendLiterally("&"+code+";"); }
         }
 
         public static Entity[] entities = new Entity[] {
@@ -609,16 +607,24 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             return null;
         }
 
+        public static @bind class Quotes extends Text {
+            public Text[] text;
+            public void toHTML(HTML h) {
+                h.append("\"");
+                h.append(text);
+                h.append("\"");
+            }
+        }
+
         public static class Link extends Text {
             public Text[] t;
             public Url u;
-            public Input.Region region;
-            public @bind.as("link")  Link(Input.Region region, @bind.arg Text[] t, @bind.arg Url u)  { this.region = region; this.t = t; this.u = u; }
-            public Link(String s, Url u) { this(null,new Text[] { new Chars(s) }, u); }
+            public @bind.as("link")  Link(@bind.arg Text[] t, @bind.arg Url u)  { this.t = t; this.u = u; }
+            public 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*/region+""));
+                      new P(t));
             }
         }
 
@@ -647,25 +653,33 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             public String   pass;
             public String   port;
             public String   path;
-            public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) {
+            public String   ref;
+            public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path, String ref) {
                 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;
+                this.ref  = ref;
             }
             public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
             public String toString() {
-                return method + "://" + host + "/" + path;
+                return method + "://" + host + "/" + path + (ref==null ? "" : ("#"+ref));
             }
         }
         public static class Email extends Url {
             public @bind.as("Mailto") Email(String email) {
-                super("mailto", null, new Host(email.substring(email.indexOf('@'))), "25", email.substring(email.indexOf('@')));
+                super("mailto",
+                      null,
+                      new Host(email.substring(email.indexOf('@'))),
+                      "25",
+                      email.substring(email.indexOf('@')),
+                      null
+                      );
             }
             public @bind.as("email")  Email(String user, Host host) {
-                super("mailto", null, host, "25", user);
+                super("mailto", null, host, "25", user, null);
             }
             public void toHTML(HTML h) {
                 h.tag("a",
@@ -678,15 +692,20 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
         public static @bind.as("lf")        String lf() { return "\r"; }
         public static @bind.as("cr")        String cr() { return "\n"; }
         public static @bind.as("\"\"")      String empty() { return ""; }
-        public static @bind.as("urlescape") char urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+        public static @bind.as("urlescape") char   urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0'))); }
+        public static @bind String urlpath(String[] s) {
+            StringBuffer sb = new StringBuffer();
+            for(String st : s) sb.append(st);
+            return sb.toString();
+        }
     }
 
     public static void main(String[] s) throws Exception {
-        try {
 
+        try {
             Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
             
-            AnnotationGrammarBindingResolver resolver = new AnnotationGrammarBindingResolver(TD.class);
+            AnnotationGrammarBindings resolver = new AnnotationGrammarBindings(TD.class);
             resolver.add(MetaGrammarBindings.class, "meta");
             Union tibgram = Grammar.create(res, "s", resolver);
 
@@ -706,32 +725,11 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             p.println(sb);
             p.flush();
             p.close();
-            
-            
-
-            /*
-            System.out.println("parsing " + s[0]);
-            Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
-            MetaGrammar gram = new Tib.Grammar(TibDoc.class);
-            gram = (MetaGrammar)gram.walk(res);
-            System.out.println("\nparsing " + s[1]);
-            Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
-            System.out.println();
-            System.out.println(f.expand1().toPrettyString());
-            System.out.println();
-
-
-            FileOutputStream fos = new FileOutputStream("out.html");
-            PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
-            p.println(sb);
-            p.flush();
-            p.close();
-            */
         } catch (Ambiguous a) {
             FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
             PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
             GraphViz gv = new GraphViz();
-            a.ambiguity.toGraphViz(gv);
+            a.getAmbiguity().toGraphViz(gv);
             gv.dump(p);
             p.flush();
             p.close();