+++ /dev/null
-// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
-
-package edu.berkeley.sbp.tib;
-import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.misc.*;
-import edu.berkeley.sbp.meta.*;
-import edu.berkeley.sbp.util.*;
-import edu.berkeley.sbp.chr.*;
-import edu.berkeley.sbp.bind.*;
-import java.util.*;
-import java.io.*;
-import static edu.berkeley.sbp.meta.MetaGrammar.*;
-
-public class TibDoc {
- /*
- 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 Text emdash() { return new Entity("mdash"); }
- 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<String, Text[]> {
- public static class KeyVal { public String key; public Text[] val; }
- public void attrs(KeyVal[] KeyVals) { for(int i=0; i<KeyVals.length; i++) this.put(KeyVals[i].key, KeyVals[i].val); }
- }
- public static class Body implements ToHTML {
- public Section[] sections;
- public void toHTML(ToHTML.HTML sb) { sb.append(sections); }
- public static class Section implements ToHTML {
- public Text header;
- public Paragraph[] paragraphs;
- public void toHTML(ToHTML.HTML sb) {
- sb.tag("h3", header);
- sb.append(paragraphs);
- }
- }
- }
- }
-
-
- // 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<hr>\n"); } }
- public static class P extends Text.TextArray implements Paragraph { public String tag() { return "p"; } }
- public static class Blockquote extends Text.TextArray implements Paragraph { public String tag() { return "blockquote"; } }
- }
-
- 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.tag("li", t);
- sb.append("</"+tag()+">\n");
- }
- }
- public static class OL extends List { public String tag() { return "ol"; } }
- public static class UL extends List { public String tag() { return "ul"; } }
-
-
-
- // Tags //////////////////////////////////////////////////////////////////////////////
-
- public static class Chars extends Text.TextString { }
- public static class Symbol extends Text.TextString { }
- public static class Keyword extends Text.TextString { public String tag() { return "tt"; } }
- public static class Subscript extends Text.TextString { public String tag() { return "sub"; } }
- public static class Superscript extends Text.TextString { public String tag() { return "super"; } }
- public static class Bold extends Text.TextArray { public String tag() { return "b"; } }
- public static class Smallcap extends Text.TextArray { public String tag() { return "sc"; } }
- public static class Strikethrough extends Text.TextArray { public String tag() { return "strike"; } }
- public static class TT extends Text.TextArray { public String tag() { return "tt"; } }
- public static class Underline extends Text.TextArray { public String tag() { return "u"; } }
- public static class Italic extends Text.TextArray { public String tag() { return "i"; } }
- public static class Citation extends Text.TextArray { } // FIXME
- public static class Footnote extends Text.TextArray { } // FIXME
- public static class LineBreak extends Text { public void toHTML(ToHTML.HTML sb) { sb.tag("br"); } }
- public static class Today extends Text { }
- public static class Euro extends Text { public void toHTML(ToHTML.HTML sb) { sb.entity(8364); } }
- public static class Link extends Text {
- public Text[] text;
- public URI href;
- public void toHTML(ToHTML.HTML sb) { sb.tag("a", new Object[] { "href", href }, text); }
- }
- public static class Entity extends Text {
- public final String entity;
- public Entity(String entity) { this.entity = entity; }
- public void toHTML(ToHTML.HTML sb) { sb.entity(entity); }
- }
-
-
- // Network //////////////////////////////////////////////////////////////////////////////
-
- public static interface Host extends ToHTML {
- public static class DNS implements Host {
- public String[] part;
- public void toHTML(ToHTML.HTML sb) {
- for(int i=0; i<part.length; i++)
- sb.append((i==0 ? "" : ".")+part[i]);
- }
- }
- 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 interface URI extends ToHTML {
- public static final Class[] subclasses = new Class[] { URL.class, Email.class };
- }
- 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 void prefix(PrintWriter p) {
- p.println("% generated by TIBDOC");
- for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
- p.println("\\setuppapersize[letter]");
- p.println("\\setuppagenumbering[location=]");
- p.println("\\setupcolors[state=start]");
- //"\\setupinteraction[title={Title},author={Me},"++
- //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
- //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
- p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
- p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
- p.println("\\setuplayout[mypage]");
- p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
- p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
- p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
- p.println("\\usesymbols[uni]");
- p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
- p.println("\\setupbodyfont[myface, 11pt]");
- p.println("\\setupwhitespace[7pt]");
- p.println("\\def\\MyDroppedCaps%");
- p.println(" {\\DroppedCaps");
- p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
- p.println("\\starttext");
- p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
- p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
- p.println("\\blank[1cm,force]");
- //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
- //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
- //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
- //p.println("\\startmypa");
- //p.println("\\switchtobodyfont[sma");
- }
-
- public static void suffix(PrintWriter p) {
- p.println("\\stoptext");
- }
- static String[] packages = new String[] { "supp-fun", "bib", "href" };
- }
-
- // ConTex
-
-module Contex where
-import Data.Array.IArray
-import Data.Char
-import Util
-import Lexer
-import IR
-import Data.List
-import Beautify
-
-toContex ll = prefix ++ (concatMap tl ll) ++ suffix
- where
- packages = [ "[supp-fun]",
- "[bib]",
- "[href]" ]
- prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
- "\\setuppapersize[letter]\n" ++
- "\\setuppagenumbering[location=]\n" ++
- "\\setupcolors[state=start]\n" ++
- --"\\setupinteraction[title={Title},author={Me},"++
- --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
- --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
- "\\setuphead[section][style={\\ss\\bfa},\n" ++
- " number=no,\n" ++
- " before=\\blank\\hairline\\nowhitespace,\n" ++
- " ]\n" ++
- "\\definelayout[mypage][\n" ++
- " backspace=1.75in, % the space for margin notes\n" ++
- " cutspace=1.75in, % the space for right margin notes\n" ++
- " width=5in" ++
- "]\n" ++
- "\\setuplayout[mypage]\n" ++
- "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
- "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
- "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
- "\\usesymbols[uni]\n" ++
- "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
- "\\setupbodyfont[myface, 11pt]\n" ++
- "\\setupwhitespace[7pt]\n" ++
- "\\def\\MyDroppedCaps%\n" ++
- " {\\DroppedCaps\n" ++
- " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
- "\\starttext\n" ++
- "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
- "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
- "\\blank[1cm,force]\n" ++
- "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
- "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
- "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
- "\\startmypar"++
- "\\switchtobodyfont[small]\n"
- suffix = "\n\\stoptext\n"
- addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
- escapify [] = []
- escapify ('%':t) = '\\':'%':(escapify t)
- escapify ('$':t) = '\\':'$':(escapify t)
- escapify (h:t) = h:(escapify t)
- escapeMath s = s
- tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
- tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
- tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
- tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
- tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
---tl (Special _ Superscript l) =
---tl (Special _ Subscript l) =
---tl (Special _ (Image u) l) =
---tl (Special _ (Cite u) l) =
- tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
- (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
- tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
- tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
- tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
- tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
- tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
- tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
- tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
- tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
- tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
- tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
- tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
- tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
- --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
---tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
- tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
- tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
- tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
--- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
--- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
- tl (Special _ Abstract l) =
- "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
- "\n\n"++(concatMap tl l)++"\\mypar" ++
- "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
- tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
- tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
- tl (WS _) = " "
- tl (BlankLine _) = "\n\n"
- tl (Block _ l) = concatMap tl l
- tl (Letter _ c) = escapify [c]
- tl z = (show z)
-
-
-
-
- */
-
- // Main //////////////////////////////////////////////////////////////////////////////
-
- public static class Dump implements Reflection.Show {
- public String toString() { return Reflection.show((Reflection.Show)this); }
- }
-
- public static class TD {
-
- public @bind static class Doc extends Dump implements ToHTML {
- public @bind.arg Header head;
- public @bind.arg Body body;
- public void toHTML(HTML h) {
- h.tag("html", new ToHTML[] { head, body });
- }
- }
-
- public @bind static class Header extends Dump implements ToHTML {
- public @bind.arg KeyVal[] attrs;
- public Text[] get(String key) {
- for(KeyVal kv : attrs)
- if (kv.key.equals(key))
- return kv.val;
- return null;
- }
- public void toHTML(HTML h) {
- h.openTag("head");
- h.tag("title", get("title"));
- h.tag("style", stylesheet);
- h.closeTag("head");
- }
- // FIXME: it would be nice to be able to
- // void KeyVal(String, String) { ... } imperatively
- }
-
- public @bind static class Body extends Dump implements ToHTML {
- public Section[] sections;
- public void toHTML(HTML h) {
- h.openTag("body");
- h.openTag("center");
- h.openTag("table", new Object[] { "width", "600px" });
- h.openTag("tr");
- h.tag("td", sections);
- h.closeTag("tr");
- h.closeTag("table");
- h.closeTag("center");
- h.closeTag("body");
- }
- }
-
- public @bind.as("Section") static class Section extends Dump implements ToHTML {
- public String header;
- public Paragraph[] paragraphs;
- public void toHTML(HTML h) {
- h.tag("h1", header);
- for(Paragraph p : paragraphs) h.tag("p", p);
- }
- }
-
- public @bind static class KeyVal extends Dump {
- public @bind.arg String key;
- public @bind.arg Text[] val;
- }
-
- public abstract static class Paragraph extends Dump implements ToHTML { }
-
- public @bind.as("P") static class P extends Paragraph {
- public Text[] text;
- 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) h.append(text); }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- ToHTML.HTML h = new ToHTML.HTML(sb);
- toHTML(h);
- return sb.toString();
- }
- }
-
- public static @bind class Login {
- public @bind.arg String username;
- public @bind.arg String password;
- }
-
- public static @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
- public static @bind.as("Symbol") Object sym(String s) { return new Chars(s); }
-
- 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);
- sb.closeTag(tag());
- }
- }
- public static @bind class OL extends List { public String tag() { return "ol"; } }
- public static @bind class UL extends List { public String tag() { return "ul"; } }
-
- public static class HR extends Paragraph {
- public @bind HR() { }
- public @bind HR(Object o) { }
- public void toHTML(HTML h) { h.tag("hr"); }
- }
-
- public static class Blockquote extends Paragraph {
- Text[] text;
- public @bind Blockquote(Text[] t) { this.text = t; }
- public @bind Blockquote(Text[] t, Text[] t2) {
- if (t2==null) {
- this.text = t;
- } else {
- Text[] t3 = new Text[t.length + t2.length];
- System.arraycopy(t, 0, t3, 0, t.length);
- System.arraycopy(t2, 0, t3, t.length, t2.length);
- this.text = t3;
- }
- }
- public void toHTML(HTML h) { h.tag("blockquote", new P(text)); }
- }
-
- 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 String toString() { return text; }
- }
- public @bind static class Block extends Text {
- public Text[] text;
- public void toHTML(HTML h) { h.append(text); }
- }
- public static class TextWrap extends Text {
- public @bind.arg Text[] text;
- public void toHTML(HTML h) {
- if (htmlTag()!=null) {
- h.openTag(htmlTag(), htmlTagParams());
- }
- h.append(text);
- if (htmlTag()!=null) {
- h.closeTag(htmlTag());
- }
- }
- public String htmlTag() { return null; }
- public Object[] htmlTagParams() { return null; }
- }
-
- public static @bind class Verbatim extends Text {
- public @bind.arg String s;
- public void toHTML(HTML h) {
- h.openTag("div", new Object[] { "class", "terminal" });
- for(int i=0; i<s.length(); i++) {
- char c = s.charAt(i);
- switch(c) {
- case '\r': break;
- case '\n': h.tag("br"); break;
- case ' ': h.appendLiterally(" "); break;
- default: h.appendText(c+""); break;
- }
- }
- 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"; } }
- public static @bind class TT extends TextWrap { public String htmlTag() { return "tt"; } }
- //public @bind class Citation extends Text { "[" word "]" }
- public static @bind class Strikethrough extends TextWrap { public String htmlTag() { return "strike"; } }
- public static @bind class Superscript extends TextWrap { public String htmlTag() { return "sup"; } }
- public static @bind class Subscript extends TextWrap { public String htmlTag() { return "sub"; } }
- public static @bind class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } }
- public static @bind class Keyword extends TextWrap { public String htmlTag() { return "sc"; } }
- public static @bind class Bold extends TextWrap { public String htmlTag() { return "b"; } }
- public static @bind class Italic extends TextWrap { public String htmlTag() { return "i"; } }
-
- public abstract static class Command extends Text { }
- 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 @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 class Entity extends Text implements ToHTML {
- public String name;
- 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("&"+code+";"); }
- }
-
- public static Entity[] entities = new Entity[] {
- new Entity("tm", 0x2122),
- new Entity("alef", 0x2135),
- new Entity("leftArrow", "<--", 0x2190),
- new Entity("rightArrow", "-->", 0x2192),
- new Entity("leftDoubleArrow", "<==", 0x21D0),
- new Entity("rightDoubleArrow", "==>", 0x21D2),
- new Entity("doubleLeftRightArrow", "<==>", 0x21D4),
- new Entity("upArrow", 0x2191),
- new Entity("downArrow", 0x2193),
- new Entity("upDoubleArrow", 0x21D1),
- new Entity("downDoubleArrow", 0x21D3),
- new Entity("forall", 0x2200),
- new Entity("exists", 0x2203),
- new Entity("emptySet", 0x2205),
- new Entity("in", 0x2208),
- new Entity("cent", 0xA2),
- new Entity("pi", 0x220F),
- new Entity("sigma", 0x2211),
- new Entity("infinity", 0x221E),
- new Entity("proportional", 0x221D),
- new Entity("check", 0x221A),
- new Entity("asterisk", 0x2217),
- new Entity("minus", 0x2212),
- new Entity("angle", 0x2220),
- new Entity("and", 0x2227),
- new Entity("or", 0x2228),
- new Entity("intersection", 0x2229),
- new Entity("union", 0x222A),
- new Entity("integral", 0x222B),
- new Entity("therefore", 0x2234),
- new Entity("congruent", 0x2245),
- new Entity("similarTo", 0x2248),
- new Entity("identical", 0x2261),
- new Entity("neq", 0x2260),
- new Entity("subset", 0x2282),
- new Entity("superset", 0x2283),
- new Entity("notSubset", 0x2284),
- new Entity("subsetEq", 0x2286),
- new Entity("supersetEq", 0x2287),
- new Entity("circlePlus", 0x2295),
- new Entity("circleTimes", 0x2297),
- new Entity("bottom", 0x22A5),
- new Entity("cdot", 0x22C5),
- new Entity("openDiamonds", 0x25CA),
- new Entity("spade", 0x2660),
- new Entity("clubs", 0x2663),
- new Entity("hearts", 0x2665),
- new Entity("diamonds", 0x2666),
- new Entity("prime", 0x2032),
- new Entity("reals", 0x211C),
- new Entity("powerSet", 0x2118),
- new Entity("overScore", 0x203E),
- new Entity("yen", 0xA5),
- new Entity("plusminus", 0xB1),
- new Entity("micro", 0xB5),
- new Entity("superScriptOne", 0xB9),
- new Entity("superScriptTwo", 0xB2),
- new Entity("superScriptThree", 0xB3),
- new Entity("oneQuarter", 0xBC),
- new Entity("oneHalf", 0xBD),
- new Entity("threeQuarters", 0xBE),
- new Entity("paragraphSymbol", 0xB6),
- new Entity("times", 0xD7),
- new Entity("daggar", 0x86),
- new Entity("sectionSymbol", 0xA7),
- new Entity("not", 0xAC),
- new Entity("cr", 0x2193),
- new Entity("dot", 0xB7),
- };
-
- public static @bind Object command(String s) {
- if (s.equals("br")) return new LineBreak();
- if (s.equals("today")) return new Today();
- for(Entity e : entities)
- if (e.name.equals(s))
- return e;
- 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 @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));
- }
- }
-
- public static class Host {
- public String name;
- public String toString() { return name; }
- public @bind.as("DNS") Host(String[][] parts) {
- name = "";
- for(String[] s : parts) {
- for(String ss : s)
- name += ss;
- name += ".";
- }
- }
- public @bind.as("IP") Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; }
- public Host(String hostname) {
- this.name = hostname;
- }
-
- }
-
- 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 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 + (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('@')),
- null
- );
- }
- public @bind.as("email") Email(String user, Host host) {
- super("mailto", null, host, "25", user, null);
- }
- public void toHTML(HTML h) {
- h.tag("a",
- new Object[] { "href", "mailto:"+path+"@"+host },
- new P(toString()));
- }
- public String toString() { return path+"@"+host; }
- }
-
- 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 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 {
- Tree<String> res = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream(s[0])).expand1();
-
- AnnotationGrammarBindings resolver = new AnnotationGrammarBindings(TD.class);
- resolver.add(MetaGrammarBindings.class, "meta");
- Union tibgram = Grammar.create(res, "s", resolver);
-
- System.err.println("parsing " + s[1]);
- Tree t = new CharParser(tibgram).parse(new Tib(new FileInputStream(s[1]))).expand1();
- System.out.println("tree:\n" + t.toPrettyString());
-
- Object result = ((Functor)t.head()).invoke(t);
- TD.Doc doc = (TD.Doc)result;
- System.out.println(doc);
-
- StringBuffer sb = new StringBuffer();
- ToHTML.HTML html = new ToHTML.HTML(sb);
- doc.toHTML(html);
- 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.getAmbiguity().toGraphViz(gv);
- gv.dump(p);
- p.flush();
- p.close();
- a.printStackTrace();
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
-
- private static final String[] stylesheet = new String[] {
- "H1 {",
- " margin-left: -20px;",
- " margin-top: 30px;",
- " font-family: helvetica, verdana, arial, sans-serif;",
- " font-size: 14pt;",
- " font-weight: bold;",
- " text-align: left;",
- " width : 100%;",
- " border-top-width: 2pt;",
- " border-top-style: solid;",
- "} ",
- "",
- "H2 {",
- " font-family: helvetica, verdana, arial, sans-serif;",
- " font-size: 12pt;",
- " font-weight: bold;",
- "} ",
- "",
- "H3 {",
- " margin-left: -10px;",
- " font-family: helvetica, verdana, arial, sans-serif;",
- " font-size: 12pt;",
- " font-weight: bold;",
- "} ",
- "",
- "TH, TD, P, LI {",
- " font-family: helvetica, verdana, arial, sans-serif;",
- " font-size: 13px; ",
- " text-decoration:none; ",
- "}",
- "",
- "LI { margin-top: 5px; }",
- "div.terminal {",
- " text-align: left;",
- " font-family: monospace;",
- " border-style: solid;",
- " border-width: 2px 2px 2px 2px;",
- " white-space: pre;",
- " border-color: #6666aa;",
- " color: #FFFFFF;",
- " background-color: #000000;",
- " margin-bottom: 25px;",
- " margin-right: 25px;",
- " margin-left: 25px;",
- " padding: 10px;",
- "}"
- };
-}