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.
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.*;
16 public static void main(String[] s) throws Exception {
17 System.out.println("parsing " + s[0]);
18 Tree<String> res = new CharToStringParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
19 MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
20 //System.out.println(gram);
21 Union mg = gram.done();
23 System.out.println("\nparsing " + s[1]);
24 Forest f = new CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
27 System.out.println(f);
29 System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString());
31 String st = new HTMLWalker().walk(f.expand1()).toString();
32 System.out.println(st);
33 FileOutputStream fos = new FileOutputStream("out.html");
34 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
40 public static class StringifyWalker extends ReflectiveWalker {
41 public Object walk(String head, Object[] children) {
42 if ("stringify".equals(head)) {
43 StringBuffer ret = new StringBuffer();
44 for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
45 return new Tree<String>(null, ret.toString());
47 if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
48 return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
52 public static String join(String[] sa, String sep) {
53 StringBuffer ret = new StringBuffer();
56 if (!first) ret.append(sep);
60 return ret.toString();
63 public static class HTMLWalker extends ReflectiveWalker {
64 //public void header() { throw new Error(); }
65 public String li(Object o) { return "<li>"+o+"</li>"; }
66 public String li(Object a, Object o) { return "<li>"+o+"</li>"; }
67 public String ul(String[] li) { return "<ul>"+join(li,"")+"</ul>"; }
68 public String ol(String[] li) { return "<ol>"+join(li,"")+"</ol>"; }
69 public String hr() { return "\n<hr/>\n"; }
70 public String it(Object o) { return "<i>"+o+"</i>"; }
71 public String tt(Object o) { return "<tt>"+o+"</tt>"; }
72 public String underline(Object o) { return "<ul>"+o+"</ul>"; }
73 public String p(Object o) { return "<p>"+o+"</p>"; }
74 public String smallcap(Object o) { return "<span style='font-variant: small-caps'>"+o+"</span>"; }
75 public String blockquote(Object o) { return "<blockquote>"+o+"</blockquote>"; }
76 public String superscript(Object o) { return "<sup>"+o+"</sup>"; }
77 public String subscript(Object o) { return "<sub>"+o+"</sub>"; }
78 public String bold(Object o) { return "<b>"+o+"</b>"; }
79 public String strikethrough(Object o) { throw new Error();/*return "<b>"+o+"</b>";*/ }
80 public Object top(Object o) { return "<html><body>"+o+"</body></html>"; }
81 public Object doc(Object header, Object body) { return body; }
82 public String text(Object[] body) {
83 StringBuffer ret = new StringBuffer();
84 for(Object o : body) { ret.append(o); ret.append(" "); }
85 return ret.toString();
87 public String body(String[] sections) { return join(sections, "\n\n"); }
88 public String domain(String[] parts) { return join(parts, "."); }
89 public String ip(String[] parts) { return join(parts, "."); }
90 public String emailaddr(String user, String host) {
91 return link(user+"@"+host, "mailto:"+user+"@"+host);
93 //public String url(String method) {
94 public String link(Object text, Object target) {
95 return "<a href='"+target+"'>"+text+"</a>";
97 public String section(Object header, Object[] body) {
98 StringBuffer ret = new StringBuffer();
101 for(Object o : body) ret.append(o);
102 return ret.toString();
104 private String escapify(Object o) {
105 String s = o==null ? "" : o.toString();
106 StringBuffer sb = new StringBuffer();
107 for(int i=0; i<s.length(); i++) {
108 switch(s.charAt(i)) {
109 case '&': sb.append("&"); break;
110 case '<': sb.append("<"); break;
111 case '>': sb.append(">"); break;
112 case '\'': sb.append("'"); break;
113 case '\"': sb.append("""); break;
114 default: sb.append(s.charAt(i)); break;
117 return sb.toString();
119 private Tree<String> lone(String s) {
120 return new Tree<String>(null, s, new Tree[0]);
122 public Object walk(Tree<String> t) {
123 String head = t.head();
124 if ("stringify".equals(head)) {
125 StringBuffer ret = new StringBuffer();
126 for(Tree<String> child : t.child(0)) ret.append(child);
127 return ret.toString();
129 return super.walk(t);
131 protected Object defaultWalk(String head, Object[] children) {
132 Tree<String>[] kids = new Tree[children.length];
133 for(int i=0; i<children.length; i++) {
134 if (children[i]==null) kids[i]=null;
135 else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i]));
136 else if (children[i] instanceof Tree) kids[i] = (Tree<String>)children[i];
137 else kids[i] = lone(children[i].toString());
139 return new Tree<String>(null, head, kids);
144 public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
146 public static AST h(AST a) { return new Gather(a, Style.H); }
147 public static AST ul(AST a) { return new Gather(a, Style.UL); }
148 public static AST tt(AST a) { return new Gather(a, Style.TT); }
149 public static AST so(AST a) { return new Gather(a, Style.SO); }
150 public static AST it(AST a) { return new Gather(a, Style.IT); }
151 public static AST q(AST a) { return new Gather(a, Style.Q); }
152 public static AST b(AST a) { return new Gather(a, Style.B); }
153 public static AST pre(AST a) { return new Gather(a, Style.PRE); }
154 public static AST list(AST a) { return new Gather(a, Style.LIST); }
155 public static AST emdash() { return new Gather(Style.EMDASH); }
157 public static AST seq(AST a) { return new Gather(a); }
159 public static class Latex {
160 public static void emit(PrintWriter p, AST a) {
165 public static void emit2(PrintWriter p, AST ast, String head) {
166 for(AST a = ast.getFirstChild(); a != null; a = a.getNextSibling()) emit(p, a, head);
168 public static void emit(PrintWriter p, AST ast, String head) {
169 if (!(ast instanceof Gather)) {
170 if (ast.getNumberOfChildren()==0) {
171 p.print(ast.getText());
177 Gather a = (Gather)ast;
183 case H: p.println(); p.println(); p.print("\\"+head+"section{"); emit2(p, a, "sub"+head); p.println("}"); break;
184 case B: p.print("{\\bf{"); emit2(p, a, head); p.print("}}"); break;
185 case UL: p.print("{\\ul{"); emit2(p, a, head); p.print("}}"); break;
186 case IT: p.print("{\\it{"); emit2(p, a, head); p.print("}}"); break;
187 case TT: p.print("{\\tt{"); emit2(p, a, head); p.print("}}"); break;
188 case SO: p.print("{\\overstrike{"); emit2(p, a, head); p.print("}}"); break;
189 case Q: p.print("``"); emit2(p, a, head); p.print("''"); break;
190 case EMDASH: p.print(" \\emdash "); break;
191 case LIST: p.println(); p.println("\\startitemize[symbol]"); emit2(p, a, head); p.println("\\stopitemize"); break;
193 if (a.getFirstChild() != null) {
195 p.println("\\begin{verbatim}");
196 p.println(a.getFirstChild().getText());
197 p.println("\\end{verbatim}");
201 public static void prefix(PrintWriter p) {
202 p.println("% generated by TIBDOC");
203 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
204 p.println("\\setuppapersize[letter]");
205 p.println("\\setuppagenumbering[location=]");
206 p.println("\\setupcolors[state=start]");
207 //"\\setupinteraction[title={Title},author={Me},"++
208 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
209 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
210 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
211 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
212 p.println("\\setuplayout[mypage]");
213 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
214 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
215 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
216 p.println("\\usesymbols[uni]");
217 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
218 p.println("\\setupbodyfont[myface, 11pt]");
219 p.println("\\setupwhitespace[7pt]");
220 p.println("\\def\\MyDroppedCaps%");
221 p.println(" {\\DroppedCaps");
222 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
223 p.println("\\starttext");
224 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
225 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
226 p.println("\\blank[1cm,force]");
227 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
228 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
229 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
230 //p.println("\\startmypa");
231 //p.println("\\switchtobodyfont[sma");
234 public static void suffix(PrintWriter p) {
235 p.println("\\stoptext");
237 static String[] packages = new String[] { "supp-fun", "bib", "href" };
243 import Data.Array.IArray
251 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
253 packages = [ "[supp-fun]",
256 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
257 "\\setuppapersize[letter]\n" ++
258 "\\setuppagenumbering[location=]\n" ++
259 "\\setupcolors[state=start]\n" ++
260 --"\\setupinteraction[title={Title},author={Me},"++
261 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
262 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
263 "\\setuphead[section][style={\\ss\\bfa},\n" ++
265 " before=\\blank\\hairline\\nowhitespace,\n" ++
267 "\\definelayout[mypage][\n" ++
268 " backspace=1.75in, % the space for margin notes\n" ++
269 " cutspace=1.75in, % the space for right margin notes\n" ++
272 "\\setuplayout[mypage]\n" ++
273 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
274 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
275 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
276 "\\usesymbols[uni]\n" ++
277 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
278 "\\setupbodyfont[myface, 11pt]\n" ++
279 "\\setupwhitespace[7pt]\n" ++
280 "\\def\\MyDroppedCaps%\n" ++
281 " {\\DroppedCaps\n" ++
282 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
284 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
285 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
286 "\\blank[1cm,force]\n" ++
287 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
288 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
289 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
291 "\\switchtobodyfont[small]\n"
292 suffix = "\n\\stoptext\n"
293 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
295 escapify ('%':t) = '\\':'%':(escapify t)
296 escapify ('$':t) = '\\':'$':(escapify t)
297 escapify (h:t) = h:(escapify t)
299 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
300 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
301 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
302 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
303 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
304 --tl (Special _ Superscript l) =
305 --tl (Special _ Subscript l) =
306 --tl (Special _ (Image u) l) =
307 --tl (Special _ (Cite u) l) =
308 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
309 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
310 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
311 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
312 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
313 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
314 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
315 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
316 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
317 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
318 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
319 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
320 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
321 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
322 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
323 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
324 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
325 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
326 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
327 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
328 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
329 tl (Special _ Abstract l) =
330 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
331 "\n\n"++(concatMap tl l)++"\\mypar" ++
332 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
333 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
334 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
336 tl (BlankLine _) = "\n\n"
337 tl (Block _ l) = concatMap tl l
338 tl (Letter _ c) = escapify [c]