package edu.berkeley.sbp.tib;
//import org.ibex.util.*;
//import org.ibex.io.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.util.*;
import java.util.*;
import java.io.*;
public class TibDoc {
+
+ public static void main(String[] s) throws Exception {
+ System.out.println("parsing " + s[0]);
+ Tree<String> res = new CharToken.CharToStringParser(MetaGrammar.make()).parse(new CharToken.Stream(new FileInputStream(s[0]))).expand1();
+ MetaGrammar gram = (MetaGrammar)new Tib.Grammar().walk(res);
+ //System.out.println(gram);
+ Union mg = gram.done();
+
+ System.out.println("\nparsing " + s[1]);
+ Forest f = new CharToken.CharToStringParser(mg).parse(new Tib(new FileInputStream(s[1])));
+
+ System.out.println();
+ System.out.println(f);
+ System.out.println();
+ System.out.println(((Tree)new StringifyWalker().walk(f.expand1())).toPrettyString());
+
+ String st = new HTMLWalker().walk(f.expand1()).toString();
+ System.out.println(st);
+ FileOutputStream fos = new FileOutputStream("out.html");
+ PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
+ p.println(st);
+ p.flush();
+ p.close();
+ }
+
+ public static class StringifyWalker extends ReflectiveWalker {
+ public Object walk(String head, Object[] children) {
+ if ("stringify".equals(head)) {
+ StringBuffer ret = new StringBuffer();
+ for(Tree<String> t : (Tree<String>)children[0]) ret.append(t);
+ return new Tree<String>(null, ret.toString());
+ }
+ if (children.length==0) return new Tree<String>(null, head, new Tree[0]);
+ return new Tree<String>(null, head, (Tree<String>[])Reflection.lub(children));
+ }
+ }
+
+ public static String join(String[] sa, String sep) {
+ StringBuffer ret = new StringBuffer();
+ boolean first = true;
+ for(String s : sa) {
+ if (!first) ret.append(sep);
+ first = false;
+ ret.append(s);
+ }
+ return ret.toString();
+ }
+
+ public static class HTMLWalker extends ReflectiveWalker {
+ //public void header() { throw new Error(); }
+ public String li(Object o) { return "<li>"+o+"</li>"; }
+ public String ul(String[] li) { return "<ul>"+join(li,"")+"</ul>"; }
+ public String ol(String[] li) { return "<ol>"+join(li,"")+"</ol>"; }
+ public String hr() { return "\n<hr/>\n"; }
+ public String it(Object o) { return "<i>"+o+"</i>"; }
+ public String tt(Object o) { return "<tt>"+o+"</tt>"; }
+ public String underline(Object o) { return "<ul>"+o+"</ul>"; }
+ public String p(Object o) { return "<p>"+o+"</p>"; }
+ public String smallcap(Object o) { return "<span style='font-variant: small-caps'>"+o+"</span>"; }
+ public String blockquote(Object o) { return "<blockquote>"+o+"</blockquote>"; }
+ public String superscript(Object o) { return "<sup>"+o+"</sup>"; }
+ public String subscript(Object o) { return "<sub>"+o+"</sub>"; }
+ public String bold(Object o) { return "<b>"+o+"</b>"; }
+ public String strikethrough(Object o) { throw new Error();/*return "<b>"+o+"</b>";*/ }
+ public Object top(Object o) { return "<html><body>"+o+"</body></html>"; }
+ public Object doc(Object header, Object body) { return body; }
+ public String text(Object[] body) {
+ StringBuffer ret = new StringBuffer();
+ for(Object o : body) { ret.append(o); ret.append(" "); }
+ return ret.toString();
+ }
+ public String body(String[] sections) { return join(sections, "\n\n"); }
+ public String domain(String[] parts) { return join(parts, "."); }
+ public String ip(String[] parts) { return join(parts, "."); }
+ public String emailaddr(String user, String host) {
+ return link(user+"@"+host, "mailto:"+user+"@"+host);
+ }
+ //public String url(String method) {
+ public String link(Object text, Object target) {
+ return "<a href='"+target+"'>"+text+"</a>";
+ }
+ public String section(Object header, Object[] body) {
+ StringBuffer ret = new StringBuffer();
+ ret.append(header);
+ ret.append(" ");
+ for(Object o : body) ret.append(o);
+ return ret.toString();
+ }
+ private String escapify(Object o) {
+ String s = o==null ? "" : o.toString();
+ StringBuffer sb = new StringBuffer();
+ for(int i=0; i<s.length(); i++) {
+ switch(s.charAt(i)) {
+ case '&': sb.append("&"); break;
+ case '<': sb.append("<"); break;
+ case '>': sb.append(">"); break;
+ case '\'': sb.append("'"); break;
+ case '\"': sb.append("""); break;
+ default: sb.append(s.charAt(i)); break;
+ }
+ }
+ return sb.toString();
+ }
+ private Tree<String> lone(String s) {
+ return new Tree<String>(null, s, new Tree[0]);
+ }
+ public Object walk(Tree<String> t) {
+ String head = t.head();
+ if ("stringify".equals(head)) {
+ StringBuffer ret = new StringBuffer();
+ for(Tree<String> child : t.child(0)) ret.append(child);
+ return ret.toString();
+ }
+ return super.walk(t);
+ }
+ protected Object defaultWalk(String head, Object[] children) {
+ Tree<String>[] kids = new Tree[children.length];
+ for(int i=0; i<children.length; i++) {
+ if (children[i]==null) kids[i]=null;
+ else if (children[i] instanceof String) kids[i] = lone(escapify((String)children[i]));
+ else if (children[i] instanceof Tree) kids[i] = (Tree<String>)children[i];
+ else kids[i] = lone(children[i].toString());
+ }
+ return new Tree<String>(null, head, kids);
+ }
+ }
+
/*
public static enum Style { H, UL, TT, SO, IT, Q, B, PRE, LIST, EMDASH; }
}
static String[] packages = new String[] { "supp-fun", "bib", "href" };
}
+
+ // ConTex
+
+module Contex where
+import Data.Array.IArray
+import Data.Char
+import Util
+import Lexer
+import IR
+import Data.List
+import Beautify
+
+toContex ll = prefix ++ (concatMap tl ll) ++ suffix
+ where
+ packages = [ "[supp-fun]",
+ "[bib]",
+ "[href]" ]
+ prefix = (concatMap (\x -> "\\usemodule"++x++"\n") packages) ++
+ "\\setuppapersize[letter]\n" ++
+ "\\setuppagenumbering[location=]\n" ++
+ "\\setupcolors[state=start]\n" ++
+ --"\\setupinteraction[title={Title},author={Me},"++
+ --"subtitle={Deez Nutz},keywords={blargh},color=blue]\n" ++
+ --"\\setuppublications[database={me},numbering=yes,sort=author]\n" ++
+ "\\setuphead[section][style={\\ss\\bfa},\n" ++
+ " number=no,\n" ++
+ " before=\\blank\\hairline\\nowhitespace,\n" ++
+ " ]\n" ++
+ "\\definelayout[mypage][\n" ++
+ " backspace=1.75in, % the space for margin notes\n" ++
+ " cutspace=1.75in, % the space for right margin notes\n" ++
+ " width=5in" ++
+ "]\n" ++
+ "\\setuplayout[mypage]\n" ++
+ "\\definetypeface[myface][rm][Xserif][Warnock Pro]\n" ++
+ "\\definetypeface[myface][tt][Xmono][CMU Typewriter Text Regular][default]\n" ++
+ "\\definetypeface[myface][ss][Xsans][Myriad Pro][default]\n" ++
+ "\\usesymbols[uni]\n" ++
+ "\\definesymbol[1][{\\USymbCharZapf{39}{164}}]\n" ++
+ "\\setupbodyfont[myface, 11pt]\n" ++
+ "\\setupwhitespace[7pt]\n" ++
+ "\\def\\MyDroppedCaps%\n" ++
+ " {\\DroppedCaps\n" ++
+ " {} {Serif} {2\\baselineskip} {2pt} {1\\baselineskip} {2}}\n" ++
+ "\\starttext\n" ++
+ "\\switchtobodyfont[16pt]\\midaligned{\\ss\\bfa{Hi5 Replicated Server Infrastructure}}\\switchtobodyfont[10pt]\n"++
+ "\\midaligned{Adam Megacz}\n\n\\nowhitespace\\midaligned{\\tt{adam@megacz.com}}\n\n"++
+ "\\blank[1cm,force]\n" ++
+ "\\defineparagraphs[mypar][n=2,before={\\blank},after={\\blank}]\n"++
+ "\\setupparagraphs[mypar][1][width=.45\\textwidth]\n"++
+ "\\setupparagraphs[mypar][2][width=.55\\textwidth]\n"++
+ "\\startmypar"++
+ "\\switchtobodyfont[small]\n"
+ suffix = "\n\\stoptext\n"
+ addItem i = "\\item " ++ (striptrail $ boost 8 $ wrap $ striplead $ tl i)
+ escapify [] = []
+ escapify ('%':t) = '\\':'%':(escapify t)
+ escapify ('$':t) = '\\':'$':(escapify t)
+ escapify (h:t) = h:(escapify t)
+ escapeMath s = s
+ tl (Special _ BulletList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
+ tl (Special _ NumberList l) = "\\startitemize[symbol]\n" ++ (concatMap addItem l) ++ "\\stopitemize\n"
+ tl (Special _ Section (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
+ tl (Special _ NumberedSection (h:t)) = "\\section{"++(tl h)++"}\n"++(concatMap tl t)
+ tl (Special _ (Glyph EmDash) []) = "{\\emdash}"
+--tl (Special _ Superscript l) =
+--tl (Special _ Subscript l) =
+--tl (Special _ (Image u) l) =
+--tl (Special _ (Cite u) l) =
+ tl (Special _ BlockQuote l) = "\n\n\\startquote\n "++
+ (striptrail $ boost 4 $ wrap $ striplead $ concatMap tl l)++"\n\\stopquote\n\n"
+ tl (Special _ HorizontalRule []) = "\\\\\\rule{4in}{0.5pt}\\\\"
+ tl (Special _ Italic l) = "{\\it{"++(concatMap tl l)++"}}"
+ tl (Special _ Bold l) = "{\\bf{"++(concatMap tl l)++"}}"
+ tl (Special _ DropCap (h:t)) = "\\MyDroppedCaps{"++(tl h)++"}{\\sc "++(concatMap tl t)++"}"
+ tl (Special _ Typewriter l) = "{\\tt{"++(concatMap tl l)++"}}"
+ tl (Special _ StrikeThrough l) = "" --"\\sout{"++(concatMap tl l)++"}"
+ tl (Special _ Quotes l) = "``"++(concatMap tl l)++"''"
+ tl (Special _ Underline l) = "" --"\\uline{"++(concatMap tl l)++"}"
+ tl (Special _ Underline2 l) = "" --"\\uuline{"++(concatMap tl l)++"}"
+ tl (Special _ (Math s) []) = "\\placeformula\n$$\n" ++ (escapeMath s) ++ "\n$$\n"
+ tl (Special _ Footnote l) = "\\footnote{"++(concatMap tl l)++"}"
+ tl (Special _ Float l) = "" --"\n\n\\begin{wrapfigure}{r}{0.4\\textwidth} \\framebox[0.4\\textwidth]{"++
+ --(concatMap tl l)++"} \\label{x}\\end{wrapfigure}\n\n"
+--tl (Special _ Figure l) = "\\placefigure[][fig:church]{}{"++(concatMap tl l)++"}"
+ tl (Special _ Figure l) = "\\startnarrower\n"++(concatMap tl l)++"\n\\stopnarrower\n"
+ tl (Special _ (Link u) l) = "\\href{"++(escapify u)++"}{"++(concatMap tl l)++"}"
+ tl (Special _ (Verbatim s) []) = "\\starttyping\n"++s++"\n\\stoptyping\n"
+-- tl (Special _ TwoColumn l) = "\\startcolumns[n=2]\n"++(concatMap tl l)++"\\stopcolumns"
+-- tl (Special _ Title l) = ""--"\\title{"++(concatMap tl l)++"}\n\\maketitle\n\n\n"
+ tl (Special _ Abstract l) =
+ "\\midaligned{\\ss\\bfa Abstract}\\par\n " ++
+ "\n\n"++(concatMap tl l)++"\\mypar" ++
+ "\\switchtobodyfont[8pt]{\\ss{\\placecontent}}\\switchtobodyfont[normal]\\stopmypar\n\n\\blank[1cm,force]"
+ tl (Special _ (Command c) l) = "\\"++c++"["++(concatMap tl l)++"]"
+ tl (Special _ t l) = error $ "formatting code "++(show t)++" not supported on {"++(concatMap show l)++"})"
+ tl (WS _) = " "
+ tl (BlankLine _) = "\n\n"
+ tl (Block _ l) = concatMap tl l
+ tl (Letter _ c) = escapify [c]
+ tl z = (show z)
+
+
+
+
*/
}