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