From 2177e4743ea680e24f0cc0525185e8f95d0dd41b Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 5 Mar 2006 01:58:51 -0500 Subject: [PATCH] TibDoc updates darcs-hash:20060305065851-5007d-e7021f5dc7fb3d98c2a57b7b914e6cb280ddb9ed.gz --- src/edu/berkeley/sbp/tib/TibDoc.java | 329 +++++++++++++++++++++------------- tests/input.tibdoc | 8 +- tests/tibdoc.g | 76 ++++---- 3 files changed, 249 insertions(+), 164 deletions(-) diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 705ac3e..3a53cf2 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -12,32 +12,211 @@ import java.io.*; public class TibDoc { - public static class Doc { + public static Text lf() { Chars ret = new Chars(); ret.text = "\n"; return ret; } + public static Text cr() { Chars ret = new Chars(); ret.text = "\r"; return ret; } + public static char urlescape(int a, int b) { return (char)(10*a+b); } + + + // Template Classes ////////////////////////////////////////////////////////////////////////////// + + public static abstract class Text implements ToHTML { + public static final Class[] subclasses = new Class[] { Chars.class, URL.class, Email.class }; + public void toHTML(ToHTML.HTML sb) { } + } + public static class TextString extends Text { + public String text; + public String tag() { return null; } + public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), text); } + } + public static class TextArray extends Text { + public Text[] t; + public String tag() { return null; } + public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); } + } + + + // Structural ////////////////////////////////////////////////////////////////////////////// + + public static class Doc implements ToHTML { public Header head; public Body body; + public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); } + public static class Header extends HashMap { + public static class kv { public String key; public Text[] val; } + public void attrs(kv[] kvs) { for(int i=0; i"); + for(int i=0; i"); } } - public static class Body { - Section[] sections; + public static class IP implements Host { + public int a, b, c, d; + public void toHTML(ToHTML.HTML sb) { sb.append(""+a+"."+b+"."+c+"."+d+""); } } - public static class Section { } - public static abstract class Text { - public static final Class[] subclasses = new Class[] { Chars.class, URL.class, Email.class }; + + public static interface URI extends ToHTML { + public static final Class[] subclasses = new Class[] { URL.class, Email.class }; } - public static class Chars extends Text { String chars; } - public static class Symbol extends Text { String chars; } - public static class Email extends Text { String user; Host host; } - public static interface Host { } - public static class DNS implements Host { String[] part; } - public static class IP implements Host { int a, b, c, d; } - public static class URL extends Text { String method; Host host; int port; String path; } - public static class Italic extends Text { Text body; } + public static class URL extends Text implements URI { + public String method; + public Login login; + public Host host; + public int port; + public String path; + public void toHTML(ToHTML.HTML sb) { + sb.append(method); + sb.append("://"); + // login.toHTML(sb); FIXME + host.toHTML(sb); + // sb.append(":"); FIXME + // sb.append(port); + sb.append("/"); + sb.append(path); + } + } + public static class Email extends Text implements URI { + public String user; + public Host host; + public void toHTML(ToHTML.HTML sb) { + sb.append(user); + sb.append('@'); + host.toHTML(sb); + } + } + + public static class Login { + public String username; + public String password; + public void toHTML(ToHTML.HTML sb) { + sb.append(username); + sb.append(':'); + sb.append(password); + sb.append('@'); + } + } + public static class Italic extends Text { + public Text[] body; + public void toHTML(ToHTML.HTML sb) { + sb.append(""); + sb.append(body); + sb.append(""); + } + } + + public static class LineBreak extends Text { + public void toHTML(ToHTML.HTML sb) { + sb.append("
"); + } + } + public static class Today extends Text { } + public static class Euro extends Text { + public void toHTML(ToHTML.HTML sb) { + sb.append("€"); + } + } + public static class Link extends Text { + public static final Class[] subclasses = new Class[] { LinkWord.class, LinkText.class }; + public static class LinkWord extends Link { + public String word; + public URI href; + public void toHTML(ToHTML.HTML sb) { + sb.append(""); + sb.append(word); + sb.append(""); + } + } + public static class LinkText extends Link { + public Text[] text; + public URI href; + public void toHTML(ToHTML.HTML sb) { + sb.append(""); + sb.append(text); + sb.append(""); + } + } + } + + + // Paragraph ////////////////////////////////////////////////////////////////////////////// + + public static interface Paragraph extends ToHTML { + public static final Class[] subclasses = new Class[] { Blockquote.class, P.class, HR.class }; + public static class HR implements Paragraph { + public void toHTML(ToHTML.HTML sb) { sb.append("\n
\n"); } + } + public static class P extends TextArray implements Paragraph { + public void toHTML(ToHTML.HTML sb) { + sb.append("\n

"); + super.toHTML(sb); + sb.append("

\n"); + } + } + public static class Blockquote extends TextArray implements Paragraph { + public String tag() { return "blockquote"; } + } + } + + + // Lists ////////////////////////////////////////////////////////////////////////////// + + public static abstract class List extends Text { + public Text[][] points; + public abstract String tag(); + public void toHTML(ToHTML.HTML sb) { + sb.append("<"+tag()+">\n"); + for(Text[] t : points) { + sb.append("
  • "); + sb.append(t); + sb.append("
  • \n"); + } + sb.append("\n"); + } + } + public static class OL extends List { public String tag() { return "ol"; } } + public static class UL extends List { public String tag() { return "ul"; } } + + + // Main ////////////////////////////////////////////////////////////////////////////// public static void main(String[] s) throws Exception { try { @@ -52,8 +231,17 @@ public class TibDoc { Forest f = new CharParser(mg).parse(new Tib(new FileInputStream(s[1]))); //((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString() System.out.println(); + System.out.println(f.expand1().toPrettyString()); + System.out.println(); Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1()); System.out.println(doc); + System.out.println(); + System.out.println(); + System.out.println(); + System.out.println(); + StringBuffer sb = new StringBuffer(); + doc.toHTML(new ToHTML.HTML(sb)); + System.out.println(sb); /* String st = new HTMLWalker().walk(f.expand1()).toString(); System.out.println(st); @@ -78,109 +266,6 @@ public class TibDoc { } } - public static class StringifyWalker extends ReflectiveWalker { - public Object walk(String head, Object[] children) { - if ("stringify".equals(head)) { - StringBuffer ret = new StringBuffer(); - for(Tree t : (Tree)children[0]) ret.append(t); - return new Tree(null, ret.toString()); - } - if (children.length==0) return new Tree(null, head, new Tree[0]); - return new Tree(null, head, (Tree[])Reflection.lub(children)); - } - } - - public static String join(String[] sa, String sep) { - StringBuffer ret = new StringBuffer(); - boolean first = true; - for(String s : sa) { - if (!first) ret.append(sep); - first = false; - ret.append(s); - } - return ret.toString(); - } - - public static class HTMLWalker extends ReflectiveWalker { - //public void header() { throw new Error(); } - public String li(Object o) { return "
  • "+o+"
  • "; } - public String li(Object a, Object o) { return "
  • "+o+"
  • "; } - public String ul(String[] li) { return "
      "+join(li,"")+"
    "; } - public String ol(String[] li) { return "
      "+join(li,"")+"
    "; } - public String hr() { return "\n
    \n"; } - public String br() { return "\n
    \n"; } - public String it(Object o) { return ""+o+""; } - public String tt(Object o) { return ""+o+""; } - public String underline(Object o) { return "
      "+o+"
    "; } - public String p(Object o) { return "

    "+o+"

    "; } - public String smallcap(Object o) { return ""+o+""; } - public String blockquote(Object o) { return "
    "+o+"
    "; } - public String superscript(Object o) { return ""+o+""; } - public String subscript(Object o) { return ""+o+""; } - public String bold(Object o) { return ""+o+""; } - public String strikethrough(Object o) { throw new Error();/*return ""+o+"";*/ } - public Object top(Object o) { return ""+o+""; } - public Object doc(Object header, Object body) { return body; } - public String text(Object[] body) { - StringBuffer ret = new StringBuffer(); - for(Object o : body) { ret.append(o); ret.append(" "); } - return ret.toString(); - } - public String body(String[] sections) { return join(sections, "\n\n"); } - public String domain(String[] parts) { return join(parts, "."); } - public String ip(String[] parts) { return join(parts, "."); } - public String emailaddr(String user, String host) { - return link(user+"@"+host, "mailto:"+user+"@"+host); - } - //public String url(String method) { - public String link(Object text, Object target) { - return ""+text+""; - } - public String section(Object header, Object[] body) { - StringBuffer ret = new StringBuffer(); - ret.append(header); - ret.append(" "); - for(Object o : body) ret.append(o); - return ret.toString(); - } - private String escapify(Object o) { - String s = o==null ? "" : o.toString(); - StringBuffer sb = new StringBuffer(); - for(int i=0; i': sb.append(">"); break; - case '\'': sb.append("'"); break; - case '\"': sb.append("""); break; - default: sb.append(s.charAt(i)); break; - } - } - return sb.toString(); - } - private Tree lone(String s) { - return new Tree(null, s, new Tree[0]); - } - public Object walk(Tree t) { - String head = t.head(); - if ("stringify".equals(head)) { - StringBuffer ret = new StringBuffer(); - for(Tree child : t.child(0)) ret.append(child); - return ret.toString(); - } - return super.walk(t); - } - protected Object defaultWalk(String head, Object[] children) { - Tree[] kids = new Tree[children.length]; - for(int i=0; i)children[i]; - else kids[i] = lone(children[i].toString()); - } - return new Tree(null, head, kids); - } - } /* public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; } diff --git a/tests/input.tibdoc b/tests/input.tibdoc index 37d779f..73d9ed6 100644 --- a/tests/input.tibdoc +++ b/tests/input.tibdoc @@ -1,7 +1,7 @@ header - author = Adam Megacz - myemail = adam@foo.megacz.com - comment = my homepage is at http://www.megacz.com you should **check** it out + "auth\nor" = Adam Megacz + myemail = x adam@foo.megacz.com y + comment = my homepage is at http://www.megacz.com you should it out == Introduction == @@ -19,7 +19,7 @@ header Furthermore we can try things like - * this + * trhis is fun * this * this * that diff --git a/tests/tibdoc.g b/tests/tibdoc.g index 7841855..216a77c 100644 --- a/tests/tibdoc.g +++ b/tests/tibdoc.g @@ -58,76 +58,76 @@ s = Doc Doc = Doc:: head:{Header} body:Body /ws Header = Header:: "header" attrs:{ kv */ ws } /ws -Body = sections:Section*/ws -Section = { section:: SectionHeader Paragraph* /ws } +Body = Body:: sections:(Section*/ws) +Section = { Section:: header:SectionHeader paragraphs:Paragraph* /ws } SectionHeader = "==" SectionHeaderBody "==" SectionHeaderBody = "=" SectionHeaderBody "=" - > !ws text !ws + > !ws (Chars:: text:alphanum++) !ws sp = " "** blank = !sp "\n" !sp "\n" !ws + kv = kv:: key:word "=" val:text /ws wp = w++ num = [0-9]++ -Paragraph = blockquote:: { "\"\"" !ws text } - > hr:: { "---" "-"* } - > p:: { text } +Paragraph = Blockquote:: { "\"\" " text } + > HR:: { "---" "-"* } + > { P:: t:text } onums = nums !(". "|") ") any = ~[]* -uli = li:: "* " (!ws text &~ any (oli|uli)) -oli = li:: ("# "|onums) (!ws text &~ any (oli|uli)) +uli = "* " (!ws text &~ any (oli|uli)) +oli = !("# "|onums) (!ws text &~ any (oli|uli)) text = Item Itemx = !ws Item | () Item = blockquote - > "[]":: { ul:: uli+/ws } Itemx - | "[]":: { ol:: oli+/ws } Itemx + > "[]":: { UL:: uli+/ws } Itemx + | "[]":: { OL:: oli+/ws } Itemx > "[]":: pre Itemx > "[]":: link Itemx > "[]":: structured Itemx > "[]":: styled Itemx - > "[]":: (Chars:: alphanum++) Itemx - > "[]":: qtext Itemx + > "[]":: (Chars:: text:alphanum++) Itemx + > "[]":: "\"" text "\"" Itemx > "[]":: symbol Itemx > "[]":: (Symbol:: sym++) Itemx > "[]":: Paragraph Itemx -blockquote = blockquote:: "\"\"" text "\"\"" - | blockquote:: "\"\"" block +blockquote = Blockquote:: "\"\"" text "\"\"" + | Blockquote:: "\"\"" block -qtext = quoted:: "\"" text "\"" -pre = verbatim:: "[verbatim]" { ~[]+ } /ws // FIXME doesn't work - -styled = underline:: "__" text "__" - | footnote:: "((" text "))" - | tt:: "[[" text "]]" - | citation:: "[" word "]" - | strikethrough:: "!!" text "!!" - | superscript:: "^^" (word|block) - | subscript:: ",," (word|block) - | smallcap:: "\\sc" block - | bold:: "++" text "++" - | keyword:: "!" (word|block) +pre = Verbatim:: "[verbatim]" { ~[]+ } /ws // FIXME doesn't work + +styled = Underline:: "__" text "__" + | Footnote:: "((" text "))" + | TT:: "[[" text "]]" + | Citation:: "[" word "]" + | Strikethrough:: "!!" text "!!" + | Superscript:: "^^" (word|block) + | Subscript:: ",," (word|block) + | Smallcap:: "\\sc" block + | Bold:: "++" text "++" + | Keyword:: "!" (word|block) | Italic:: "**" text "**" block = { text } -link = link:: text:({ text }) "->" href:(url|email) - > link:: text:alphanum++ !ws "->" href:(url|email) +link = Link:: text:({ text }) "->" href:(url|email) + > Link:: text:alphanum++ !ws "->" href:(url|email) structured = command & "\\" [a-zA-Z0-9]++ block? > glyph > email > url -glyph = euro:: "(e)" | "(r)" | "(c)" | "(tm)" | "--" | "..." +glyph = Euro:: "(e)" | "(r)" | "(c)" | "(tm)" | "--" | "..." -command = today:: "\\today" - | bre:: "\\br" +command = Today:: "\\today" + | LineBreak:: "\\br" // URLs ////////////////////////////////////////////////////////////////////////////// @@ -141,17 +141,17 @@ password = [a-zA-Z0-9;/?:&=$\-_.+]++ urlc = [a-zA-Z0-9;/?:&=$\-_.+@] urlv = urlc | [%] urlchar = urlc - | "%":: "%" [0-9] [0-9] + | urlescape:: "%" [0-9] [0-9] url = "mailto" ":" email -> ~urlv - > method:method "://" url_login? host:host port:(":" nums)? path:("/" urlpath)? -> ~urlv -url_login = login:: username (":" password) "@" + > 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 +/ ".") -> ~"." part = [a-zA-Z0-9\-]++ // interesting use of boolean grammars // &~ ([\-0-9] ~[]* | ~[]* [\-0-9]) -email = user:username "@" host:host -> ~[.] +email = Email:: user:username "@" host:host -> ~[.] nums = [0-9]++ host = IP:: nums "." nums "." nums "." nums | DNS:: domain @@ -165,8 +165,8 @@ word = alphanum++ quoted = "\"" ((~[\"\\] | escaped)+) "\"" | "":: "\"\"" -escaped = "\n":: "\\n" - | "\r":: "\\r" +escaped = lf:: "\\n" + | cr:: "\\r" | "\\" ~[nr] -- 1.7.10.4