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 edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.util.*;
9 import edu.berkeley.sbp.chr.*;
15 public static Text lf() { Chars ret = new Chars(); ret.text = "\n"; return ret; }
16 public static Text cr() { Chars ret = new Chars(); ret.text = "\r"; return ret; }
17 public static Text emdash() { return new Entity("mdash"); }
18 public static char urlescape(int a, int b) { return (char)(10*a+b); }
21 // Template Classes //////////////////////////////////////////////////////////////////////////////
23 public static abstract class Text implements ToHTML {
24 public static final Class[] subclasses = new Class[] { Chars.class, URL.class, Email.class };
25 public void toHTML(ToHTML.HTML sb) { }
26 public static class TextString extends Text {
28 public String tag() { return null; }
29 public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), text); }
31 public static class TextArray extends Text {
33 public String tag() { return null; }
34 public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); }
39 // Structural //////////////////////////////////////////////////////////////////////////////
41 public static class Doc implements ToHTML {
44 public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
45 public static class Header extends HashMap<String, Text[]> {
46 public static class kv { public String key; public Text[] val; }
47 public void attrs(kv[] kvs) { for(int i=0; i<kvs.length; i++) this.put(kvs[i].key, kvs[i].val); }
49 public static class Body implements ToHTML {
50 public Section[] sections;
51 public void toHTML(ToHTML.HTML sb) { sb.append(sections); }
52 public static class Section implements ToHTML {
54 public Paragraph[] paragraphs;
55 public void toHTML(ToHTML.HTML sb) {
57 sb.append(paragraphs);
64 // Paragraph //////////////////////////////////////////////////////////////////////////////
66 public static interface Paragraph extends ToHTML {
67 public static final Class[] subclasses = new Class[] { Blockquote.class, P.class, HR.class };
68 public static class HR implements Paragraph { public void toHTML(ToHTML.HTML sb) { sb.append("\n<hr>\n"); } }
69 public static class P extends Text.TextArray implements Paragraph { public String tag() { return "p"; } }
70 public static class Blockquote extends Text.TextArray implements Paragraph { public String tag() { return "blockquote"; } }
73 public static abstract class List extends Text {
74 public Text[][] points;
75 public abstract String tag();
76 public void toHTML(ToHTML.HTML sb) {
77 sb.append("<"+tag()+">\n");
78 for(Text[] t : points) sb.tag("li", t);
79 sb.append("</"+tag()+">\n");
82 public static class OL extends List { public String tag() { return "ol"; } }
83 public static class UL extends List { public String tag() { return "ul"; } }
87 // Tags //////////////////////////////////////////////////////////////////////////////
89 public static class Chars extends Text.TextString { }
90 public static class Symbol extends Text.TextString { }
91 public static class Keyword extends Text.TextString { public String tag() { return "tt"; } }
92 public static class Subscript extends Text.TextString { public String tag() { return "sub"; } }
93 public static class Superscript extends Text.TextString { public String tag() { return "super"; } }
94 public static class Bold extends Text.TextArray { public String tag() { return "b"; } }
95 public static class Smallcap extends Text.TextArray { public String tag() { return "sc"; } }
96 public static class Strikethrough extends Text.TextArray { public String tag() { return "strike"; } }
97 public static class TT extends Text.TextArray { public String tag() { return "tt"; } }
98 public static class Underline extends Text.TextArray { public String tag() { return "u"; } }
99 public static class Italic extends Text.TextArray { public String tag() { return "i"; } }
100 public static class Citation extends Text.TextArray { } // FIXME
101 public static class Footnote extends Text.TextArray { } // FIXME
102 public static class LineBreak extends Text { public void toHTML(ToHTML.HTML sb) { sb.tag("br"); } }
103 public static class Today extends Text { }
104 public static class Euro extends Text { public void toHTML(ToHTML.HTML sb) { sb.entity(8364); } }
105 public static class Link extends Text {
108 public void toHTML(ToHTML.HTML sb) { sb.tag("a", new Object[] { "href", href }, text); }
110 public static class Entity extends Text {
111 public final String entity;
112 public Entity(String entity) { this.entity = entity; }
113 public void toHTML(ToHTML.HTML sb) { sb.entity(entity); }
117 // Network //////////////////////////////////////////////////////////////////////////////
119 public static interface Host extends ToHTML {
120 public static class DNS implements Host {
121 public String[] part;
122 public void toHTML(ToHTML.HTML sb) {
123 for(int i=0; i<part.length; i++)
124 sb.append((i==0 ? "" : ".")+part[i]);
127 public static class IP implements Host {
128 public int a, b, c, d;
129 public void toHTML(ToHTML.HTML sb) { sb.append(a+"."+b+"."+c+"."+d); }
133 public static interface URI extends ToHTML {
134 public static final Class[] subclasses = new Class[] { URL.class, Email.class };
136 public static class URL extends Text implements URI {
137 public String method;
142 public void toHTML(ToHTML.HTML sb) {
145 // login.toHTML(sb); FIXME
147 // sb.append(":"); FIXME
153 public static class Email extends Text implements URI {
156 public void toHTML(ToHTML.HTML sb) {
163 public static class Login {
164 public String username;
165 public String password;
166 public void toHTML(ToHTML.HTML sb) {
176 // Main //////////////////////////////////////////////////////////////////////////////
178 public static void main(String[] s) throws Exception {
180 System.out.println("parsing " + s[0]);
181 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
182 MetaGrammar gram = new Tib.Grammar(TibDoc.class);
183 gram = (MetaGrammar)gram.walk(res);
184 System.out.println("\nparsing " + s[1]);
185 Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
186 System.out.println();
187 System.out.println(f.expand1().toPrettyString());
188 System.out.println();
189 Doc doc = (Doc)new ReflectiveGrammar(TibDoc.class).build(f.expand1());
190 System.out.println(doc);
191 System.out.println();
192 System.out.println();
193 System.out.println();
194 System.out.println();
195 StringBuffer sb = new StringBuffer();
196 doc.toHTML(new ToHTML.HTML(sb));
197 System.out.println(sb);
199 FileOutputStream fos = new FileOutputStream("out.html");
200 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
205 } catch (Ambiguous a) {
206 FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
207 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
208 GraphViz gv = new GraphViz();
209 a.ambiguity.toGraphViz(gv);
215 } catch (Exception e) {
222 public static void prefix(PrintWriter p) {
223 p.println("% generated by TIBDOC");
224 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
225 p.println("\\setuppapersize[letter]");
226 p.println("\\setuppagenumbering[location=]");
227 p.println("\\setupcolors[state=start]");
228 //"\\setupinteraction[title={Title},author={Me},"++
229 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
230 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
231 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
232 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
233 p.println("\\setuplayout[mypage]");
234 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
235 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
236 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
237 p.println("\\usesymbols[uni]");
238 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
239 p.println("\\setupbodyfont[myface, 11pt]");
240 p.println("\\setupwhitespace[7pt]");
241 p.println("\\def\\MyDroppedCaps%");
242 p.println(" {\\DroppedCaps");
243 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
244 p.println("\\starttext");
245 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
246 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
247 p.println("\\blank[1cm,force]");
248 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
249 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
250 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
251 //p.println("\\startmypa");
252 //p.println("\\switchtobodyfont[sma");
255 public static void suffix(PrintWriter p) {
256 p.println("\\stoptext");
258 static String[] packages = new String[] { "supp-fun", "bib", "href" };
264 import Data.Array.IArray
272 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
274 packages = [ "[supp-fun]",
277 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
278 "\\setuppapersize[letter]\n" ++
279 "\\setuppagenumbering[location=]\n" ++
280 "\\setupcolors[state=start]\n" ++
281 --"\\setupinteraction[title={Title},author={Me},"++
282 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
283 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
284 "\\setuphead[section][style={\\ss\\bfa},\n" ++
286 " before=\\blank\\hairline\\nowhitespace,\n" ++
288 "\\definelayout[mypage][\n" ++
289 " backspace=1.75in, % the space for margin notes\n" ++
290 " cutspace=1.75in, % the space for right margin notes\n" ++
293 "\\setuplayout[mypage]\n" ++
294 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
295 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
296 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
297 "\\usesymbols[uni]\n" ++
298 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
299 "\\setupbodyfont[myface, 11pt]\n" ++
300 "\\setupwhitespace[7pt]\n" ++
301 "\\def\\MyDroppedCaps%\n" ++
302 " {\\DroppedCaps\n" ++
303 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
305 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
306 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
307 "\\blank[1cm,force]\n" ++
308 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
309 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
310 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
312 "\\switchtobodyfont[small]\n"
313 suffix = "\n\\stoptext\n"
314 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
316 escapify ('%':t) = '\\':'%':(escapify t)
317 escapify ('$':t) = '\\':'$':(escapify t)
318 escapify (h:t) = h:(escapify t)
320 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
321 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
322 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
323 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
324 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
325 --tl (Special _ Superscript l) =
326 --tl (Special _ Subscript l) =
327 --tl (Special _ (Image u) l) =
328 --tl (Special _ (Cite u) l) =
329 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
330 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
331 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
332 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
333 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
334 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
335 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
336 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
337 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
338 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
339 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
340 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
341 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
342 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
343 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
344 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
345 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
346 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
347 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
348 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
349 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
350 tl (Special _ Abstract l) =
351 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
352 "\n\n"++(concatMap tl l)++"\\mypar" ++
353 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
354 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
355 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
357 tl (BlankLine _) = "\n\n"
358 tl (Block _ l) = concatMap tl l
359 tl (Letter _ c) = escapify [c]