X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Ftib%2FTibDoc.java;h=538d80874ef04876b00e6b882c62c007b8c6dd5b;hb=92632cd2b60dc20e157128696edc81e3a07eaec6;hp=504e3a432dcb3948149668213c17dab5e1533b5c;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4;p=sbp.git diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index 504e3a4..538d808 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -5,10 +5,140 @@ package edu.berkeley.sbp.tib; //import org.ibex.util.*; //import org.ibex.io.*; +import edu.berkeley.sbp.*; +import edu.berkeley.sbp.misc.*; +import edu.berkeley.sbp.util.*; import java.util.*; import java.io.*; public class TibDoc { + + public static void main(String[] s) throws Exception { + System.out.println("parsing " + s[0]); + Tree res = new CharToken.CharToStringParser(MetaGrammar.make()).parse(new CharToken.Stream(new FileInputStream(s[0]))).expand1(); + MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res); + //System.out.println(gram); + Union mg = gram.done(); + + System.out.println("\nparsing " + s[1]); + Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1]))); + + System.out.println(); + System.out.println(f); + System.out.println(); + System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString()); + + String st = new HTMLWalker().walk(f.expand1()).toString(); + System.out.println(st); + FileOutputStream fos = new FileOutputStream("out.html"); + PrintWriter p = new PrintWriter(new OutputStreamWriter(fos)); + p.println(st); + p.flush(); + p.close(); + } + + 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 ul(String[] li) { return "
      "+join(li,"")+"
    "; } + public String ol(String[] li) { return "
      "+join(li,"")+"
    "; } + public String hr() { 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; } @@ -105,6 +235,111 @@ public class TibDoc { } 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) + + + + */ }