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.meta.*;
9 import edu.berkeley.sbp.util.*;
10 import edu.berkeley.sbp.chr.*;
11 import edu.berkeley.sbp.bind.*;
14 import static edu.berkeley.sbp.meta.MetaGrammar.*;
18 public static Text lf() { Chars ret = new Chars(); ret.text = "\n"; return ret; }
19 public static Text cr() { Chars ret = new Chars(); ret.text = "\r"; return ret; }
20 public static Text emdash() { return new Entity("mdash"); }
21 public static char urlescape(int a, int b) { return (char)(10*a+b); }
24 // Template Classes //////////////////////////////////////////////////////////////////////////////
26 public static abstract class Text implements ToHTML {
27 public static final Class[] subclasses = new Class[] { Chars.class, URL.class, Email.class };
28 public void toHTML(ToHTML.HTML sb) { }
29 public static class TextString extends Text {
31 public String tag() { return null; }
32 public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), text); }
34 public static class TextArray extends Text {
36 public String tag() { return null; }
37 public void toHTML(ToHTML.HTML sb) { sb.tag(tag(), t); }
42 // Structural //////////////////////////////////////////////////////////////////////////////
44 public static class Doc implements ToHTML {
47 public void toHTML(ToHTML.HTML sb) { sb.tag("html", body); }
48 public static class Header extends HashMap<String, Text[]> {
49 public static class KeyVal { public String key; public Text[] val; }
50 public void attrs(KeyVal[] KeyVals) { for(int i=0; i<KeyVals.length; i++) this.put(KeyVals[i].key, KeyVals[i].val); }
52 public static class Body implements ToHTML {
53 public Section[] sections;
54 public void toHTML(ToHTML.HTML sb) { sb.append(sections); }
55 public static class Section implements ToHTML {
57 public Paragraph[] paragraphs;
58 public void toHTML(ToHTML.HTML sb) {
60 sb.append(paragraphs);
67 // Paragraph //////////////////////////////////////////////////////////////////////////////
69 public static interface Paragraph extends ToHTML {
70 public static final Class[] subclasses = new Class[] { Blockquote.class, P.class, HR.class };
71 public static class HR implements Paragraph { public void toHTML(ToHTML.HTML sb) { sb.append("\n<hr>\n"); } }
72 public static class P extends Text.TextArray implements Paragraph { public String tag() { return "p"; } }
73 public static class Blockquote extends Text.TextArray implements Paragraph { public String tag() { return "blockquote"; } }
76 public static abstract class List extends Text {
77 public Text[][] points;
78 public abstract String tag();
79 public void toHTML(ToHTML.HTML sb) {
80 sb.append("<"+tag()+">\n");
81 for(Text[] t : points) sb.tag("li", t);
82 sb.append("</"+tag()+">\n");
85 public static class OL extends List { public String tag() { return "ol"; } }
86 public static class UL extends List { public String tag() { return "ul"; } }
90 // Tags //////////////////////////////////////////////////////////////////////////////
92 public static class Chars extends Text.TextString { }
93 public static class Symbol extends Text.TextString { }
94 public static class Keyword extends Text.TextString { public String tag() { return "tt"; } }
95 public static class Subscript extends Text.TextString { public String tag() { return "sub"; } }
96 public static class Superscript extends Text.TextString { public String tag() { return "super"; } }
97 public static class Bold extends Text.TextArray { public String tag() { return "b"; } }
98 public static class Smallcap extends Text.TextArray { public String tag() { return "sc"; } }
99 public static class Strikethrough extends Text.TextArray { public String tag() { return "strike"; } }
100 public static class TT extends Text.TextArray { public String tag() { return "tt"; } }
101 public static class Underline extends Text.TextArray { public String tag() { return "u"; } }
102 public static class Italic extends Text.TextArray { public String tag() { return "i"; } }
103 public static class Citation extends Text.TextArray { } // FIXME
104 public static class Footnote extends Text.TextArray { } // FIXME
105 public static class LineBreak extends Text { public void toHTML(ToHTML.HTML sb) { sb.tag("br"); } }
106 public static class Today extends Text { }
107 public static class Euro extends Text { public void toHTML(ToHTML.HTML sb) { sb.entity(8364); } }
108 public static class Link extends Text {
111 public void toHTML(ToHTML.HTML sb) { sb.tag("a", new Object[] { "href", href }, text); }
113 public static class Entity extends Text {
114 public final String entity;
115 public Entity(String entity) { this.entity = entity; }
116 public void toHTML(ToHTML.HTML sb) { sb.entity(entity); }
120 // Network //////////////////////////////////////////////////////////////////////////////
122 public static interface Host extends ToHTML {
123 public static class DNS implements Host {
124 public String[] part;
125 public void toHTML(ToHTML.HTML sb) {
126 for(int i=0; i<part.length; i++)
127 sb.append((i==0 ? "" : ".")+part[i]);
130 public static class IP implements Host {
131 public int a, b, c, d;
132 public void toHTML(ToHTML.HTML sb) { sb.append(a+"."+b+"."+c+"."+d); }
136 public static interface URI extends ToHTML {
137 public static final Class[] subclasses = new Class[] { URL.class, Email.class };
139 public static class URL extends Text implements URI {
140 public String method;
145 public void toHTML(ToHTML.HTML sb) {
148 // login.toHTML(sb); FIXME
150 // sb.append(":"); FIXME
156 public static class Email extends Text implements URI {
159 public void toHTML(ToHTML.HTML sb) {
166 public static class Login {
167 public String username;
168 public String password;
169 public void toHTML(ToHTML.HTML sb) {
182 public static void prefix(PrintWriter p) {
183 p.println("% generated by TIBDOC");
184 for(int i=0; i<packages.length; i++) p.println("\\usemodule["+packages[i]+"]");
185 p.println("\\setuppapersize[letter]");
186 p.println("\\setuppagenumbering[location=]");
187 p.println("\\setupcolors[state=start]");
188 //"\\setupinteraction[title={Title},author={Me},"++
189 //"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
190 //"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
191 p.println("\\setuphead[section][style={\\ss\\bfa},number=no,before=\\blank\\hairline\\nowhitespace]");
192 p.println("\\definelayout[mypage][backspace=1.75in,cutspace=1.75in,width=5in]");
193 p.println("\\setuplayout[mypage]");
194 p.println("\\definetypeface[myface][rm][Xserif][Warnock Pro]");
195 p.println("\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]");
196 p.println("\\definetypeface[myface][ss][Xsans][Myriad Pro][default]");
197 p.println("\\usesymbols[uni]");
198 p.println("\\definesymbol[1][{\\USymbCharZapf{39}{164}}]");
199 p.println("\\setupbodyfont[myface, 11pt]");
200 p.println("\\setupwhitespace[7pt]");
201 p.println("\\def\\MyDroppedCaps%");
202 p.println(" {\\DroppedCaps");
203 p.println(" {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}");
204 p.println("\\starttext");
205 p.println("\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Title}}\\switchtobodyfont[10pt]");
206 p.println("\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}");
207 p.println("\\blank[1cm,force]");
208 //p.println("\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}");
209 //p.println("\\setupparagraphs[mypar][1][width=.45\\textwidth");
210 //p.println("\\setupparagraphs[mypar][2][width=.55\\textwidth");
211 //p.println("\\startmypa");
212 //p.println("\\switchtobodyfont[sma");
215 public static void suffix(PrintWriter p) {
216 p.println("\\stoptext");
218 static String[] packages = new String[] { "supp-fun", "bib", "href" };
224 import Data.Array.IArray
232 toContex ll = prefix ++ (concatMap tl ll) ++ suffix
234 packages = [ "[supp-fun]",
237 prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
238 "\\setuppapersize[letter]\n" ++
239 "\\setuppagenumbering[location=]\n" ++
240 "\\setupcolors[state=start]\n" ++
241 --"\\setupinteraction[title={Title},author={Me},"++
242 --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
243 --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
244 "\\setuphead[section][style={\\ss\\bfa},\n" ++
246 " before=\\blank\\hairline\\nowhitespace,\n" ++
248 "\\definelayout[mypage][\n" ++
249 " backspace=1.75in, % the space for margin notes\n" ++
250 " cutspace=1.75in, % the space for right margin notes\n" ++
253 "\\setuplayout[mypage]\n" ++
254 "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
255 "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
256 "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
257 "\\usesymbols[uni]\n" ++
258 "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
259 "\\setupbodyfont[myface, 11pt]\n" ++
260 "\\setupwhitespace[7pt]\n" ++
261 "\\def\\MyDroppedCaps%\n" ++
262 " {\\DroppedCaps\n" ++
263 " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
265 "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
266 "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
267 "\\blank[1cm,force]\n" ++
268 "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
269 "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
270 "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
272 "\\switchtobodyfont[small]\n"
273 suffix = "\n\\stoptext\n"
274 addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
276 escapify ('%':t) = '\\':'%':(escapify t)
277 escapify ('$':t) = '\\':'$':(escapify t)
278 escapify (h:t) = h:(escapify t)
280 tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
281 tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
282 tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
283 tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
284 tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
285 --tl (Special _ Superscript l) =
286 --tl (Special _ Subscript l) =
287 --tl (Special _ (Image u) l) =
288 --tl (Special _ (Cite u) l) =
289 tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
290 (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
291 tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
292 tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
293 tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
294 tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
295 tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
296 tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
297 tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
298 tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
299 tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
300 tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
301 tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
302 tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
303 --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
304 --tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
305 tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
306 tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
307 tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
308 -- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
309 -- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
310 tl (Special _ Abstract l) =
311 "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
312 "\n\n"++(concatMap tl l)++"\\mypar" ++
313 "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
314 tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
315 tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
317 tl (BlankLine _) = "\n\n"
318 tl (Block _ l) = concatMap tl l
319 tl (Letter _ c) = escapify [c]
327 // Main //////////////////////////////////////////////////////////////////////////////
329 public static class Dump implements Reflection.Show {
330 public String toString() { return Reflection.show((Reflection.Show)this); }
333 public static class TD {
335 public @bind static class Doc extends Dump implements ToHTML {
336 public @bind.arg Header head;
337 public @bind.arg Body body;
338 public void toHTML(HTML h) { body.toHTML(h); }
341 public @bind.as("H") static class Header extends Dump {
342 public @bind.arg KeyVal[] attrs;
343 // FIXME: it would be nice to be able to
344 // void KeyVal(String, String) { ... } imperatively
347 public @bind.as("B") static class Body extends Dump implements ToHTML {
348 public Section[] sections;
349 public void toHTML(HTML h) { for(Section s : sections) s.toHTML(h); }
352 public @bind.as("Section") static class Section extends Dump implements ToHTML {
353 public String header;
354 public Paragraph[] paragraphs;
355 public void toHTML(HTML h) {
357 for(Paragraph p : paragraphs) h.tag("p", p);
361 public @bind static class KeyVal extends Dump {
362 public @bind.arg String key;
363 public @bind.arg Object val;
366 public abstract static class Paragraph extends Dump implements ToHTML { }
368 public @bind.as("P") static class P extends Paragraph {
371 public P(Text[] text) { this.text = text; }
372 public void toHTML(HTML h) { if (text != null) for (Text t : text) if (t != null) t.toHTML(h); }
373 public String toString() {
374 StringBuffer sb = new StringBuffer();
375 ToHTML.HTML h = new ToHTML.HTML(sb);
377 return sb.toString();
381 public @bind.as("email") static Object email(@bind.arg Object a, @bind.arg Object c) { return null; }
382 public static @bind class Login {
383 public @bind.arg String username;
384 public @bind.arg String password;
387 public @bind.as("Euro") Object euro() { return null; }
388 public @bind.as("Citation") Object cite(Object o) { return new Chars("*cite*"); }
389 public @bind.as("Symbol") Object sym(Object o) { return null; }
391 public static abstract class List extends Text {
392 public @bind.arg Text[] points;
393 public abstract String tag();
394 public void toHTML(ToHTML.HTML sb) {
395 sb.append("<"+tag()+">\n");
396 for(Text t : points) sb.tag("li", t);
397 sb.append("</"+tag()+">\n");
400 public static @bind class OL extends List { public String tag() { return "ol"; } }
401 public static @bind class UL extends List { public String tag() { return "ul"; } }
403 public static class HR extends Paragraph {
404 public @bind HR() { }
405 public @bind HR(Object o) { }
406 public void toHTML(HTML h) { h.tag("hr"); }
409 public static class Blockquote extends Paragraph {
411 public @bind Blockquote(Text[] t) { this.text = t; }
412 public void toHTML(HTML h) { h.tag("blockquote", new P(text)); }
415 public abstract static class Text extends Dump implements ToHTML { }
416 public @bind static class Chars extends Text {
419 public Chars(String text) { this.text = text; }
420 public void toHTML(HTML h) { h.appendText(" " + text + " "); }
421 public String toString() { return text; }
423 public @bind static class Block extends Text {
425 public void toHTML(HTML h) { for(Text t : text) t.toHTML(h); }
427 public static class TextWrap extends Text {
428 public @bind.arg Text[] text;
429 public void toHTML(HTML h) {
430 if (htmlTag()!=null) {
431 h.openTag(htmlTag(), htmlTagParams());
433 for(Text t : text) t.toHTML(h);
434 if (htmlTag()!=null) {
435 h.closeTag(htmlTag());
438 public String htmlTag() { return null; }
439 public Object[] htmlTagParams() { return null; }
441 public static @bind class Verbatim extends Text { public char[] c; public void toHTML(HTML h) { } }
442 //public @bind class Blockquote extends TextWrap { }
443 public static @bind class Underline extends TextWrap { public String htmlTag() { return "u"; } }
444 public static @bind class Footnote extends TextWrap { public String htmlTag() { return "small"; } }
445 public static @bind class TT extends TextWrap { public String htmlTag() { return "tt"; } }
446 //public @bind class Citation extends Text { "[" word "]" }
447 public static @bind class Strikethrough extends TextWrap { public String htmlTag() { return "strike"; } }
448 public static @bind class Superscript extends TextWrap { public String htmlTag() { return "sup"; } }
449 public static @bind class Subscript extends TextWrap { public String htmlTag() { return "sub"; } }
450 public static @bind class Smallcap extends TextWrap { public String htmlTag() { return "sc"; } }
451 public static @bind class Keyword extends TextWrap { public String htmlTag() { return "sc"; } }
452 public static @bind class Bold extends TextWrap { public String htmlTag() { return "b"; } }
453 public static @bind class Italic extends TextWrap { public String htmlTag() { return "i"; } }
455 public abstract static class Command extends Text { }
456 public static @bind class Today extends Command { public void toHTML(HTML h) { } }
457 public static @bind class LineBreak extends Command { public void toHTML(HTML h) { h.tag("br"); } }
459 public abstract static class Glyph extends Text { }
460 public static @bind.as("emdash") class Emdash extends Glyph { public void toHTML(HTML h) { h.appendLiterally("—"); } }
462 public static class Link extends Text {
465 public @bind.as("LinkText") Link(Text[] t, Url u) { this.t = t; this.u = u; }
466 public @bind.as("LinkChars") Link(String s, Url u) { this(new Text[] { new Chars(s) }, u); }
467 public void toHTML(HTML h) {
469 new Object[] { "href", u==null ? "" : u.toString() },
474 public static class Host {
476 public String toString() { return name; }
477 public @bind.as("DNS") Host(String[][] parts) {
479 for(String[] s : parts) {
485 public @bind.as("IP") Host(int a, int b, int c, int d) { name = a+"."+b+"."+c+"."+d; }
488 public static class Url extends Text {
489 public String method;
495 public @bind.as("URL") Url(String method, String[] login, Host host, String port, String path) {
496 this.method = method;
497 this.user = login==null ? null : login.length >= 1 ? login[0] : null;
498 this.pass = login==null ? null : login.length >= 2 ? login[1] : null;
503 public @bind.as("Mailto") Url(String email) { this("mailto", null, null, "25", email); }
504 public void toHTML(HTML h) { new Link(toString(), this).toHTML(h); }
505 public String toString() {
506 return method + "://" + host + "/" + path;
509 public static @bind.as("lf") String lf() { return "\r"; }
510 public static @bind.as("cr") String cr() { return "\n"; }
511 public static @bind.as("\"\"") String empty() { return ""; }
512 public static @bind.as("urlescape") String urlescape(char a, char b) { return ((char)((a-'0') * 16 + (b-'0')))+""; }
515 public static void main(String[] s) throws Exception {
518 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
520 Union tibgram = MetaGrammar.make(res, "s", new AnnotationGrammarBindingResolver(TD.class));
522 System.err.println("parsing " + s[1]);
523 Tree t = new CharParser(tibgram).parse(new Tib(new FileInputStream(s[1]))).expand1();
524 System.out.println("tree:\n" + t.toPrettyString());
526 Object result = ((Functor)t.head()).invoke(t);
527 TD.Doc doc = (TD.Doc)result;
528 System.out.println(doc);
530 StringBuffer sb = new StringBuffer();
531 ToHTML.HTML html = new ToHTML.HTML(sb);
533 FileOutputStream fos = new FileOutputStream("out.html");
534 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
542 System.out.println("parsing " + s[0]);
543 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
544 MetaGrammar gram = new Tib.Grammar(TibDoc.class);
545 gram = (MetaGrammar)gram.walk(res);
546 System.out.println("\nparsing " + s[1]);
547 Forest f = new CharParser(gram.done()).parse(new Tib(new FileInputStream(s[1])));
548 System.out.println();
549 System.out.println(f.expand1().toPrettyString());
550 System.out.println();
553 FileOutputStream fos = new FileOutputStream("out.html");
554 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
559 } catch (Ambiguous a) {
560 FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
561 PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
562 GraphViz gv = new GraphViz();
563 a.ambiguity.toGraphViz(gv);
569 } catch (Exception e) {