+package org.ibex.util;
+import java.util.*;
+import java.io.*;
+import org.ibex.util.*;
+
+public class Doc extends XML {
+ public static void main(String[] s) throws Exception {
+ Doc d = new Doc();
+ d.parse(new InputStreamReader(System.in));
+ ((Node)d.nodeStack.elementAt(0)).dumpLatex(System.out);
+ System.out.println("\\end{document}");
+ }
+
+ Vec top = new Vec();
+ Vec nodeStack = new Vec();
+ public Doc() { nodeStack.addElement(new Node()); }
+
+ public void startElement(Element e) throws Exn {
+ String name = e.getLocalName();
+ if (nodeStack.lastElement() != null && (nodeStack.lastElement() instanceof PRE)) {
+ ((PRE)nodeStack.lastElement()).addText("<" + e.getQName() + ">");
+ buffer++;
+ return;
+ }
+ if (name.equals("ibex-doc")) {
+ String title = "You forgot the title, you idiot!";
+ String author = "Your Mom";
+ String email = null;
+ String subtitle = null;
+ for(int i=0; i<e.getAttrLen(); i++) {
+ if (e.getAttrKey(i).equals("title")) title = e.getAttrVal(i);
+ if (e.getAttrKey(i).equals("author")) author = e.getAttrVal(i);
+ if (e.getAttrKey(i).equals("email")) email = e.getAttrVal(i);
+ if (e.getAttrKey(i).equals("subtitle")) subtitle = e.getAttrVal(i);
+ }
+ System.out.println("\\documentclass{article}");
+ System.out.println("\\def\\ninept{\\def\\baselinestretch{.95}\\let\\normalsize\\small\\normalsize}");
+ System.out.println("\\ninept");
+ System.out.println("\\usepackage{graphicx}");
+ System.out.println("\\usepackage{amssymb,amsmath,epsfig,alltt}");
+ System.out.println("\\sloppy");
+ System.out.println("\\usepackage{palatino}");
+ System.out.println("\\usepackage{sectsty}");
+ System.out.println("\\allsectionsfont{\\sffamily}");
+ System.out.println("\\sectionfont{\\pagebreak\\leftskip=-2cm\\hrulefill\\\\\\sffamily\\bfseries\\raggedleft\\vspace{1cm}}");
+ System.out.println("\\subsectionfont{\\dotfill\\\\\\sffamily\\raggedright\\hspace{-4cm}}");
+ System.out.println("\\newdimen\\sectskip");
+ System.out.println("\\newdimen\\subsectskip");
+ System.out.println("\\newdimen\\saveskip");
+ System.out.println("\\saveskip=\\leftskip");
+ System.out.println("\\sectskip=-2cm");
+ System.out.println("\\subsectskip=0cm");
+ System.out.println("\\let\\oldsection\\section");
+ System.out.println("\\let\\oldsubsection\\subsection");
+ System.out.println("\\def\\subsection#1{\\leftskip=\\sectskip\\oldsubsection{#1}\\leftskip=0cm}");
+ System.out.println("\\usepackage{parskip}");
+ System.out.println("\\usepackage{tabularx}");
+ System.out.println("\\usepackage{alltt}");
+ System.out.println("\\usepackage[pdftex,bookmarks=true]{hyperref}");
+ System.out.println("");
+ System.out.println("\\begin{document}");
+ System.out.println("");
+ System.out.println("\\title{\\textbf{\\textsf{");
+ System.out.println(title);
+ if (subtitle != null) System.out.println("\\\\{\\large " + subtitle + "}");
+ System.out.println("}}}");
+ if (author != null) {
+ System.out.println("\\author{");
+ System.out.println(author);
+ if (email != null) System.out.println("\\\\{\\tt " + email + "}");
+ System.out.println("}");
+ }
+ System.out.println("");
+ System.out.println("\\maketitle");
+ System.out.println("\\clearpage");
+ System.out.println("\\tableofcontents");
+ System.out.println("\\clearpage");
+ System.out.println("\\onecolumn");
+ nodeStack.addElement(new Node());
+ } else if (name.equals("section") || name.equals("appendix")) {
+ String secname = "unknown";
+ for(int i=0; i<e.getAttrLen(); i++) if (e.getAttrKey(i).equals("title")) secname = e.getAttrVal(i);
+ nodeStack.addElement(new Section(secname, name.equals("appendix")));
+ } else if (name.equals("b")) { nodeStack.addElement(new B());
+ } else if (name.equals("i")) { nodeStack.addElement(new I());
+ } else if (name.equals("tt")) { nodeStack.addElement(new TT());
+ } else if (name.equals("list")) { nodeStack.addElement(new List());
+ } else if (name.equals("pre")) { nodeStack.addElement(new PRE());
+ } else if (name.equals("ref")) { buffer++;
+ } else if (name.equals("link")) {
+ buffer++;
+ for(int i=0; i<e.getAttrLen(); i++) if (e.getAttrKey(i).equals("text")) addText(e.getAttrVal(i));
+ } else if (name.equals("definition")) { buffer++;
+ } else if (name.equals("property")) { buffer++;
+ } else { System.err.println("warning: unknown tag " + name);
+ buffer++;
+ }
+ }
+
+ int buffer = 0;
+ String pending = "";
+ public void whitespace(char[] ch, int start, int length) throws Exn, IOException { characters(ch, start, length); }
+ void addText(String s) { ((Node)nodeStack.lastElement()).addText(s); }
+ public void endElement(Element e) throws Exn, IOException {
+ if (buffer > 0) {
+ buffer--;
+ if (nodeStack.lastElement() instanceof PRE)
+ ((PRE)nodeStack.lastElement()).addText("</" + e.getLocalName() + ">");
+ } else {
+ nodeStack.setSize(nodeStack.size() - 1);
+ }
+ }
+ public void characters(char[] ch, int start, int length) throws Exn, IOException {
+ Node n = ((Node)nodeStack.lastElement());
+ if (n != null) n.addText(new String(ch, start, length));
+ }
+
+ boolean intt = false;
+ class Node {
+ Vec children = new Vec();
+ final Node parent;
+ public Node() { this((Node)nodeStack.lastElement()); }
+ public Node(Node parent) { this.parent = parent; if (parent != null) parent.add(this); }
+ public void add(Node child) { children.addElement(child); }
+ public void addText(String s) { children.addElement(s); }
+ void printText(PrintStream p, String mt2) { p.print(fix(mt2)); }
+ String fix(String mt2) {
+ mt2 = mt2.replaceAll("\\\\", "\\backslash ");
+ mt2 = mt2.replaceAll("LaTeX", "\\LaTeX");
+ mt2 = mt2.replaceAll("\\$", "\\\\\\$ ");
+ mt2 = mt2.replaceAll("\\%", "\\\\% ");
+ mt2 = mt2.replaceAll("#", "\\\\# ");
+ mt2 = mt2.replaceAll("\\{", "\\\\{ ");
+ mt2 = mt2.replaceAll("\\}", "\\\\} ");
+ mt2 = mt2.replaceAll("\\&", "\\\\& ");
+ mt2 = mt2.replaceAll("\\~", "\\\\~ ");
+ mt2 = mt2.replaceAll("_", "\\\\_");
+ if (!intt) {
+ mt2 = mt2.replaceAll("\" ", "'' ");
+ mt2 = mt2.replaceAll("\"\n", "''\n");
+ }
+ mt2 = mt2.replaceAll(" \"", " ``");
+ mt2 = mt2.replaceAll("\"", "``");
+ return mt2;
+ }
+ public void dumpLatex(PrintStream p) {
+ for(int i=0; i<children.size(); i++) {
+ if (children.elementAt(i) instanceof String) {
+ printText(p, (String)children.elementAt(i));
+ } else {
+ ((Node)children.elementAt(i)).dumpLatex(p);
+ }
+ }
+ }
+ }
+
+ class PRE extends Node {
+ String myText = "";
+ public void addText(String s) { myText += s; }
+ public void dumpLatex(PrintStream p) {
+ p.println("\n\\begin{verbatim}\n");
+ p.print(myText);
+ p.println("\n\\end{verbatim}\n");
+ }
+ }
+
+ class I extends Node { public void dumpLatex(PrintStream p) { p.print("{\\it "); super.dumpLatex(p); p.print("}"); } }
+ class B extends Node { public void dumpLatex(PrintStream p) { p.print("{\\bf "); super.dumpLatex(p); p.print("}"); } }
+ class TT extends Node { public void dumpLatex(PrintStream p) {
+ p.print("{\\tt ");
+ intt = true;
+ super.dumpLatex(p);
+ intt = false;
+ p.print("}"); } }
+
+ class Section extends Node {
+ String secname;
+ boolean appendix = false;
+ public Section(String secname, boolean appendix) { this.secname = secname; this.appendix = appendix;}
+ public void dumpLatex(PrintStream p) {
+ String secs = "";
+ for(Node n = parent; n != null; n = n.parent) if (n instanceof Section) secs += "sub";
+ if (appendix) {
+ p.println("\n\n\\appendix{"+secname+"}\n");
+ } else {
+ p.println("\n\n\\" + secs + "section{"+secname+"}\n");
+ }
+ super.dumpLatex(p);
+ }
+ }
+
+ class List extends Node {
+ boolean ordered = false;
+ public void dumpLatex(PrintStream p) {
+ p.println("\n\\begin{itemize}%\n");
+ String acc = "";
+ p.print("\n\\item%\n");
+ boolean used = false;
+ for(int i=0; i<children.size(); i++) {
+ if (children.elementAt(i) instanceof String) {
+ acc += fix(children.elementAt(i).toString());
+ } else {
+ if (acc.length() > 0) {
+ if (!used) acc = acc.replaceAll("^\\s*", "");
+ if (children.elementAt(i) instanceof List) acc = acc.replaceAll("\\n\\s*$", "");
+ acc = acc.replaceAll("\\n\\s*\\n", "\n\n\\\\item ");
+ if (acc.trim().length() > 0) {
+ used = true;
+ p.print(acc);
+ }
+ acc = "";
+ }
+ ((Node)children.elementAt(i)).dumpLatex(p);
+ }
+ }
+ if (acc.length() > 0) {
+ if (!used) acc = acc.replaceAll("^\\s*", "");
+ acc = acc.replaceAll("\\n\\s*$", "");
+ p.print(acc.replaceAll("\\n\\s*\\n", "\n\n\\\\item "));
+ acc = "";
+ }
+ p.println("\n\\end{itemize}%\n");
+ }
+ }
+
+}