4 import org.ibex.util.*;
6 public class Doc extends XML {
7 public static void main(String[] s) throws Exception {
9 d.parse(new InputStreamReader(System.in));
10 ((Node)d.nodeStack.elementAt(0)).dumpLatex(System.out);
11 System.out.println("\\end{document}");
15 Vec nodeStack = new Vec();
16 public Doc() { nodeStack.addElement(new Node()); }
18 public void startElement(Element e) throws Exn {
19 String name = e.getLocalName();
20 if (nodeStack.lastElement() != null && (nodeStack.lastElement() instanceof PRE)) {
21 ((PRE)nodeStack.lastElement()).addText("<" + e.getQName() + ">");
25 if (name.equals("ibex-doc")) {
26 String title = "You forgot the title, you idiot!";
27 String author = "Your Mom";
29 String subtitle = null;
30 for(int i=0; i<e.getAttrLen(); i++) {
31 if (e.getAttrKey(i).equals("title")) title = e.getAttrVal(i);
32 if (e.getAttrKey(i).equals("author")) author = e.getAttrVal(i);
33 if (e.getAttrKey(i).equals("email")) email = e.getAttrVal(i);
34 if (e.getAttrKey(i).equals("subtitle")) subtitle = e.getAttrVal(i);
36 System.out.println("\\documentclass{article}");
37 System.out.println("\\def\\ninept{\\def\\baselinestretch{.95}\\let\\normalsize\\small\\normalsize}");
38 System.out.println("\\ninept");
39 System.out.println("\\usepackage{graphicx}");
40 System.out.println("\\usepackage{amssymb,amsmath,epsfig,alltt}");
41 System.out.println("\\sloppy");
42 System.out.println("\\usepackage{palatino}");
43 System.out.println("\\usepackage{sectsty}");
44 System.out.println("\\allsectionsfont{\\sffamily}");
45 System.out.println("\\sectionfont{\\pagebreak\\leftskip=-2cm\\hrulefill\\\\\\sffamily\\bfseries\\raggedleft\\vspace{1cm}}");
46 System.out.println("\\subsectionfont{\\dotfill\\\\\\sffamily\\raggedright\\hspace{-4cm}}");
47 System.out.println("\\newdimen\\sectskip");
48 System.out.println("\\newdimen\\subsectskip");
49 System.out.println("\\newdimen\\saveskip");
50 System.out.println("\\saveskip=\\leftskip");
51 System.out.println("\\sectskip=-2cm");
52 System.out.println("\\subsectskip=0cm");
53 System.out.println("\\let\\oldsection\\section");
54 System.out.println("\\let\\oldsubsection\\subsection");
55 System.out.println("\\def\\subsection#1{\\leftskip=\\sectskip\\oldsubsection{#1}\\leftskip=0cm}");
56 System.out.println("\\usepackage{parskip}");
57 System.out.println("\\usepackage{tabularx}");
58 System.out.println("\\usepackage{alltt}");
59 System.out.println("\\usepackage[pdftex,bookmarks=true]{hyperref}");
60 System.out.println("");
61 System.out.println("\\begin{document}");
62 System.out.println("");
63 System.out.println("\\title{\\textbf{\\textsf{");
64 System.out.println(title);
65 if (subtitle != null) System.out.println("\\\\{\\large " + subtitle + "}");
66 System.out.println("}}}");
68 System.out.println("\\author{");
69 System.out.println(author);
70 if (email != null) System.out.println("\\\\{\\tt " + email + "}");
71 System.out.println("}");
73 System.out.println("");
74 System.out.println("\\maketitle");
75 System.out.println("\\clearpage");
76 System.out.println("\\tableofcontents");
77 System.out.println("\\clearpage");
78 System.out.println("\\onecolumn");
79 nodeStack.addElement(new Node());
80 } else if (name.equals("section") || name.equals("appendix")) {
81 String secname = "unknown";
82 for(int i=0; i<e.getAttrLen(); i++) if (e.getAttrKey(i).equals("title")) secname = e.getAttrVal(i);
83 nodeStack.addElement(new Section(secname, name.equals("appendix")));
84 } else if (name.equals("b")) { nodeStack.addElement(new B());
85 } else if (name.equals("i")) { nodeStack.addElement(new I());
86 } else if (name.equals("tt")) { nodeStack.addElement(new TT());
87 } else if (name.equals("list")) { nodeStack.addElement(new List());
88 } else if (name.equals("pre")) { nodeStack.addElement(new PRE());
89 } else if (name.equals("ref")) { buffer++;
90 } else if (name.equals("link")) {
92 for(int i=0; i<e.getAttrLen(); i++) if (e.getAttrKey(i).equals("text")) addText(e.getAttrVal(i));
93 } else if (name.equals("definition")) { buffer++;
94 } else if (name.equals("property")) { buffer++;
95 } else { System.err.println("warning: unknown tag " + name);
102 public void whitespace(char[] ch, int start, int length) throws Exn, IOException { characters(ch, start, length); }
103 void addText(String s) { ((Node)nodeStack.lastElement()).addText(s); }
104 public void endElement(Element e) throws Exn, IOException {
107 if (nodeStack.lastElement() instanceof PRE)
108 ((PRE)nodeStack.lastElement()).addText("</" + e.getLocalName() + ">");
110 nodeStack.setSize(nodeStack.size() - 1);
113 public void characters(char[] ch, int start, int length) throws Exn, IOException {
114 Node n = ((Node)nodeStack.lastElement());
115 if (n != null) n.addText(new String(ch, start, length));
118 boolean intt = false;
120 Vec children = new Vec();
122 public Node() { this((Node)nodeStack.lastElement()); }
123 public Node(Node parent) { this.parent = parent; if (parent != null) parent.add(this); }
124 public void add(Node child) { children.addElement(child); }
125 public void addText(String s) { children.addElement(s); }
126 void printText(PrintStream p, String mt2) { p.print(fix(mt2)); }
127 String fix(String mt2) {
128 mt2 = mt2.replaceAll("\\\\", "\\backslash ");
129 mt2 = mt2.replaceAll("LaTeX", "\\LaTeX");
130 mt2 = mt2.replaceAll("\\$", "\\\\\\$ ");
131 mt2 = mt2.replaceAll("\\%", "\\\\% ");
132 mt2 = mt2.replaceAll("#", "\\\\# ");
133 mt2 = mt2.replaceAll("\\{", "\\\\{ ");
134 mt2 = mt2.replaceAll("\\}", "\\\\} ");
135 mt2 = mt2.replaceAll("\\&", "\\\\& ");
136 mt2 = mt2.replaceAll("\\~", "\\\\~ ");
137 mt2 = mt2.replaceAll("_", "\\\\_");
139 mt2 = mt2.replaceAll("\" ", "'' ");
140 mt2 = mt2.replaceAll("\"\n", "''\n");
142 mt2 = mt2.replaceAll(" \"", " ``");
143 mt2 = mt2.replaceAll("\"", "``");
146 public void dumpLatex(PrintStream p) {
147 for(int i=0; i<children.size(); i++) {
148 if (children.elementAt(i) instanceof String) {
149 printText(p, (String)children.elementAt(i));
151 ((Node)children.elementAt(i)).dumpLatex(p);
157 class PRE extends Node {
159 public void addText(String s) { myText += s; }
160 public void dumpLatex(PrintStream p) {
161 p.println("\n\\begin{verbatim}\n");
163 p.println("\n\\end{verbatim}\n");
167 class I extends Node { public void dumpLatex(PrintStream p) { p.print("{\\it "); super.dumpLatex(p); p.print("}"); } }
168 class B extends Node { public void dumpLatex(PrintStream p) { p.print("{\\bf "); super.dumpLatex(p); p.print("}"); } }
169 class TT extends Node { public void dumpLatex(PrintStream p) {
176 class Section extends Node {
178 boolean appendix = false;
179 public Section(String secname, boolean appendix) { this.secname = secname; this.appendix = appendix;}
180 public void dumpLatex(PrintStream p) {
182 for(Node n = parent; n != null; n = n.parent) if (n instanceof Section) secs += "sub";
184 p.println("\n\n\\appendix{"+secname+"}\n");
186 p.println("\n\n\\" + secs + "section{"+secname+"}\n");
192 class List extends Node {
193 boolean ordered = false;
194 public void dumpLatex(PrintStream p) {
195 p.println("\n\\begin{itemize}%\n");
197 p.print("\n\\item%\n");
198 boolean used = false;
199 for(int i=0; i<children.size(); i++) {
200 if (children.elementAt(i) instanceof String) {
201 acc += fix(children.elementAt(i).toString());
203 if (acc.length() > 0) {
204 if (!used) acc = acc.replaceAll("^\\s*", "");
205 if (children.elementAt(i) instanceof List) acc = acc.replaceAll("\\n\\s*$", "");
206 acc = acc.replaceAll("\\n\\s*\\n", "\n\n\\\\item ");
207 if (acc.trim().length() > 0) {
213 ((Node)children.elementAt(i)).dumpLatex(p);
216 if (acc.length() > 0) {
217 if (!used) acc = acc.replaceAll("^\\s*", "");
218 acc = acc.replaceAll("\\n\\s*$", "");
219 p.print(acc.replaceAll("\\n\\s*\\n", "\n\n\\\\item "));
222 p.println("\n\\end{itemize}%\n");