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