checkpoint
[sbp.git] / src / edu / berkeley / sbp / tib / TibDoc.java
1 // Copyright 2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package edu.berkeley.sbp.tib;
6 //import org.ibex.util.*;
7 //import org.ibex.io.*;
8 import edu.berkeley.sbp.*;
9 import edu.berkeley.sbp.misc.*;
10 import java.util.*;
11 import java.io.*;
12
13 public class TibDoc extends ReflectiveWalker {
14     
15     public static void main(String[] s) throws Exception {
16         System.out.println("parsing " + s[0]);
17         Tree<String> res = new CharToken.CharToStringParser(MetaGrammar.make()).parse(new CharToken.Stream(new FileInputStream(s[0]))).expand1();
18         MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
19         //System.out.println(gram);
20         Union mg = gram.done();
21         
22         System.out.println("\nparsing " + s[1]);
23         Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
24         System.out.println(f);
25         //System.out.println(((Tree)walk(f.expand1())).toString(0, 0, 120));
26         System.out.println(((Tree)new TibDoc().walk(f.expand1())).toPrettyString());
27     }
28
29     public Tree<String> walk(Tree<String> tree) {
30         String head = tree.head();
31         if ("stringify".equals(head)) {
32             StringBuffer ret = new StringBuffer();
33             for(Tree<String> t : tree.child(0)) ret.append(t);
34             return new Tree<String>(null, ret.toString());
35         }
36         Tree<String>[] children = new Tree[tree.numChildren()];
37         for(int i=0; i<children.length; i++) children[i] = walk(tree.child(i));
38         return new Tree<String>(null, head, children);
39     }
40
41
42     /*
43     public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
44
45     public static AST h(AST a)      { return new Gather(a, Style.H); }
46     public static AST ul(AST a)     { return new Gather(a, Style.UL); }
47     public static AST tt(AST a)     { return new Gather(a, Style.TT); }
48     public static AST so(AST a)     { return new Gather(a, Style.SO); }
49     public static AST it(AST a)     { return new Gather(a, Style.IT); }
50     public static AST q(AST a)      { return new Gather(a, Style.Q); }
51     public static AST b(AST a)      { return new Gather(a, Style.B); }
52     public static AST pre(AST a)    { return new Gather(a, Style.PRE); }
53     public static AST list(AST a)   { return new Gather(a, Style.LIST); }
54     public static AST emdash()      { return new Gather(Style.EMDASH); }
55
56     public static AST seq(AST a) { return new Gather(a); }
57
58     public static class Latex {
59         public static void emit(PrintWriter p, AST a) {
60             prefix(p);
61             emit(p, a, "");
62             suffix(p);
63         }
64         public static void emit2(PrintWriter p, AST ast, String head) {
65             for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
66         }
67         public static void emit(PrintWriter p, AST ast, String head) {
68             if (!(ast instanceof Gather)) {
69                 if (ast.getNumberOfChildren()==0) {
70                     p.print(ast.getText());
71                 } else {
72                     emit2(p, ast, head);
73                 }
74                 return;
75             }
76             Gather a = (Gather)ast;
77             if (a.style==null) {
78                 emit2(p, a, head);
79                 return;
80             }
81             switch(a.style) {
82                 case H:    p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
83                 case B:    p.print("{\\bf{");                          emit2(p, a, head); p.print("}}"); break;
84                 case UL:   p.print("{\\ul{");                          emit2(p, a, head); p.print("}}"); break;
85                 case IT:   p.print("{\\it{");                          emit2(p, a, head); p.print("}}"); break;
86                 case TT:   p.print("{\\tt{");                          emit2(p, a, head); p.print("}}"); break;
87                 case SO:   p.print("{\\overstrike{");                  emit2(p, a, head); p.print("}}"); break;
88                 case Q:    p.print("``");                              emit2(p, a, head); p.print("''"); break;
89                 case EMDASH: p.print(" \\emdash "); break;
90                 case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
91                 case PRE:
92                     if (a.getFirstChild() != null) {
93                         p.println();
94                         p.println("\\begin{verbatim}");
95                         p.println(a.getFirstChild().getText());
96                         p.println("\\end{verbatim}");
97                     }
98             }
99         }
100         public static void prefix(PrintWriter p) {
101             p.println("% generated by TIBDOC");
102             for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
103             p.println("\\setuppapersize[letter]");
104             p.println("\\setuppagenumbering[location=]");
105             p.println("\\setupcolors[state=start]");
106             //"\\setupinteraction[title={Title},author={Me},"++
107             //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
108             //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
109             p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
110             p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
111             p.println("\\setuplayout[mypage]");
112             p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
113             p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
114             p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
115             p.println("\\usesymbols[uni]");
116             p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
117             p.println("\\setupbodyfont[myface, 11pt]");
118             p.println("\\setupwhitespace[7pt]");
119             p.println("\\def\\MyDroppedCaps%");
120             p.println("    {\\DroppedCaps");
121             p.println("        {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
122             p.println("\\starttext");
123             p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
124             p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
125             p.println("\\blank[1cm,force]");
126             //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
127             //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
128             //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
129             //p.println("\\startmypa");
130             //p.println("\\switchtobodyfont[sma");
131         }
132
133         public static void suffix(PrintWriter p) {
134             p.println("\\stoptext");
135         }
136         static String[] packages = new String[] { "supp-fun", "bib", "href" };
137     }
138
139     // ConTex
140
141 module Contex where
142 import Data.Array.IArray
143 import Data.Char
144 import Util
145 import Lexer
146 import IR
147 import Data.List
148 import Beautify
149
150 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
151  where
152   packages                         = [ "[supp-fun]",
153                                        "[bib]",
154                                        "[href]" ]
155   prefix                           = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
156                                      "\\setuppapersize[letter]\n" ++
157                                      "\\setuppagenumbering[location=]\n" ++
158                                      "\\setupcolors[state=start]\n" ++
159                                      --"\\setupinteraction[title={Title},author={Me},"++
160                                      --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
161                                      --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
162                                      "\\setuphead[section][style={\\ss\\bfa},\n" ++
163                                      "                     number=no,\n" ++
164                                      "                     before=\\blank\\hairline\\nowhitespace,\n" ++
165                                      "                     ]\n" ++
166                                      "\\definelayout[mypage][\n" ++
167                                      " backspace=1.75in, % the space for margin notes\n" ++
168                                      " cutspace=1.75in, % the space for right margin notes\n" ++
169                                      " width=5in" ++
170                                      "]\n" ++
171                                      "\\setuplayout[mypage]\n" ++
172                                      "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
173                                      "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
174                                      "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
175                                      "\\usesymbols[uni]\n" ++
176                                      "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
177                                      "\\setupbodyfont[myface, 11pt]\n" ++
178                                      "\\setupwhitespace[7pt]\n" ++
179                                      "\\def\\MyDroppedCaps%\n" ++
180                                      "    {\\DroppedCaps\n" ++
181                                      "        {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
182                                      "\\starttext\n" ++
183                                      "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++ 
184                                      "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
185                                      "\\blank[1cm,force]\n" ++
186                                      "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
187                                      "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
188                                      "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
189                                      "\\startmypar"++
190                                      "\\switchtobodyfont[small]\n"
191   suffix                           = "\n\\stoptext\n"
192   addItem i                        = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
193   escapify []                      = []
194   escapify ('%':t)                 = '\\':'%':(escapify t)
195   escapify ('$':t)                 = '\\':'$':(escapify t)
196   escapify (h:t)                   = h:(escapify t)
197   escapeMath s                     = s
198   tl (Special _ BulletList l)      = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
199   tl (Special _ NumberList l)      = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
200   tl (Special _ Section (h:t))     = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
201   tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
202   tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
203 --tl (Special _ Superscript l)     = 
204 --tl (Special _ Subscript l)       = 
205 --tl (Special _ (Image u) l)       = 
206 --tl (Special _ (Cite u) l)        = 
207   tl (Special _ BlockQuote l)      = "\n\n\\startquote\n     "++
208                                      (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
209   tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
210   tl (Special _ Italic l)          = "{\\it{"++(concatMap tl l)++"}}"
211   tl (Special _ Bold l)            = "{\\bf{"++(concatMap tl l)++"}}"
212   tl (Special _ DropCap (h:t))     = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
213   tl (Special _ Typewriter l)      = "{\\tt{"++(concatMap tl l)++"}}"
214   tl (Special _ StrikeThrough l)   = "" --"\\sout{"++(concatMap tl l)++"}"
215   tl (Special _ Quotes l)          = "``"++(concatMap tl l)++"''"
216   tl (Special _ Underline l)       = "" --"\\uline{"++(concatMap tl l)++"}"
217   tl (Special _ Underline2 l)      = "" --"\\uuline{"++(concatMap tl l)++"}"
218   tl (Special _ (Math s) [])       = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
219   tl (Special _ Footnote l)        = "\\footnote{"++(concatMap tl l)++"}"
220   tl (Special _ Float l)           = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
221                                      --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
222 --tl (Special _ Figure l)          = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
223   tl (Special _ Figure l)          = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
224   tl (Special _ (Link u) l)        = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
225   tl (Special _ (Verbatim s) [])   = "\\starttyping\n"++s++"\n\\stoptyping\n"
226 --  tl (Special _ TwoColumn l)       = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
227 --  tl (Special _ Title l)           = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
228   tl (Special _ Abstract l) =
229       "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
230       "\n\n"++(concatMap tl l)++"\\mypar" ++
231       "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
232   tl (Special _ (Command c) l)     = "\\"++c++"["++(concatMap tl l)++"]"
233   tl (Special _ t l)               = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
234   tl (WS _)                        = " "
235   tl (BlankLine _)                 = "\n\n"
236   tl (Block _ l)                   = concatMap tl l
237   tl (Letter _ c)                  = escapify [c]
238   tl z                             = (show z)
239
240
241
242
243     */
244 }
245