latex support in org.ibex.util.Doc
[org.ibex.core.git] / src / org / ibex / util / Doc.java
1 package org.ibex.util;
2 import java.util.*;
3 import java.io.*;
4 import org.ibex.util.*;
5
6 public class Doc extends XML {
7     public static void main(String[] s) throws Exception {
8         Doc d = new Doc();
9         d.parse(new InputStreamReader(System.in));
10         ((Node)d.nodeStack.elementAt(0)).dumpLatex(System.out);
11         System.out.println("\\end{document}");
12     }
13     
14     Vec top = new Vec();
15     Vec nodeStack = new Vec();
16     public Doc() { nodeStack.addElement(new Node()); }
17
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() + ">");
22             buffer++;
23             return;
24         }
25         if (name.equals("ibex-doc")) {
26             String title = "You forgot the title, you idiot!";
27             String author = "Your Mom";
28             String email = null;
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);
35             }
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("}}}");
67             if (author != null) {
68                 System.out.println("\\author{");
69                 System.out.println(author);
70                 if (email != null) System.out.println("\\\\{\\tt " + email + "}");
71                 System.out.println("}");
72             }
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")) {
91             buffer++;
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);
96         buffer++;
97         }
98     }
99     
100     int buffer = 0;
101     String pending = "";
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 {
105         if (buffer > 0) {
106             buffer--;
107             if (nodeStack.lastElement() instanceof PRE)
108                 ((PRE)nodeStack.lastElement()).addText("</" + e.getLocalName() + ">");
109         } else {
110             nodeStack.setSize(nodeStack.size() - 1);
111         }
112     }
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));
116     }
117
118     boolean intt = false;
119     class Node {
120         Vec children = new Vec();
121         final Node parent;
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("_", "\\\\_");
138             if (!intt) {
139                 mt2 = mt2.replaceAll("\" ", "'' ");
140                 mt2 = mt2.replaceAll("\"\n", "''\n");
141             }
142             mt2 = mt2.replaceAll(" \"", " ``");
143             mt2 = mt2.replaceAll("\"", "``");
144             return mt2;
145         }
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));
150                 } else {
151                     ((Node)children.elementAt(i)).dumpLatex(p);
152                 }
153             }
154         }
155     }
156
157     class PRE extends Node {
158         String myText = "";
159         public void addText(String s) { myText += s; }
160         public void dumpLatex(PrintStream p) {
161             p.println("\n\\begin{verbatim}\n");
162             p.print(myText);
163             p.println("\n\\end{verbatim}\n");
164         }
165     }
166
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) {
170         p.print("{\\tt ");
171         intt = true;
172         super.dumpLatex(p);
173         intt = false;
174         p.print("}"); } }
175
176     class Section extends Node {
177         String secname;
178         boolean appendix = false;
179         public Section(String secname, boolean appendix) { this.secname = secname; this.appendix = appendix;}
180         public void dumpLatex(PrintStream p) {
181             String secs = "";
182             for(Node n = parent; n != null; n = n.parent) if (n instanceof Section) secs += "sub";
183             if (appendix) {
184                 p.println("\n\n\\appendix{"+secname+"}\n");
185             } else {
186                 p.println("\n\n\\" + secs + "section{"+secname+"}\n");
187             }
188             super.dumpLatex(p);
189         }
190     }
191
192     class List extends Node {
193         boolean ordered = false;
194         public void dumpLatex(PrintStream p) {
195             p.println("\n\\begin{itemize}%\n");
196             String acc = "";
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());
202                 } else {
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) {
208                             used = true;
209                             p.print(acc);
210                         }
211                         acc = "";
212                     }
213                     ((Node)children.elementAt(i)).dumpLatex(p);
214                 }
215             }
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 "));
220                 acc = "";
221             }
222             p.println("\n\\end{itemize}%\n");
223         }
224     }
225
226 }