001 /*
002 * [Adapted from BSD licence] Copyright (c) 2002 Terence Parr All rights
003 * reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions are met:
007 * 1. Redistributions of source code must retain the above copyright notice,
008 * this list of conditions and the following disclaimer. 2. Redistributions in
009 * binary form must reproduce the above copyright notice, this list of
010 * conditions and the following disclaimer in the documentation and/or other
011 * materials provided with the distribution. 3. The name of the author may not
012 * be used to endorse or promote products derived from this software without
013 * specific prior written permission.
014 *
015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
016 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
017 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
018 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
019 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
020 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
021 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
022 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
023 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
025 */
026 package gate.wiki.antlr;
027
028 import java.io.*;
029 import java.util.Vector;
030 import java.util.regex.Pattern;
031
032 public class LaTeXTarget extends NOPTarget{
033 private static int tableColumnCount = 0;
034 private static boolean firstRow = false;
035 private boolean inTable = false;
036 private String tableContents = "";
037
038 public LaTeXTarget(){
039 this(new BufferedWriter(new OutputStreamWriter(System.out)));
040 }
041 public LaTeXTarget(Writer out){
042 this.out = out;
043 }
044 public String getTargetLanguage(){
045 return Tool.LATEX;
046 }
047
048 protected void write(String s){
049
050 try{
051 if(inTable) {
052 tableContents +=s;
053 }
054 else {
055 out.write(s);
056 }
057 String c = extractLastCommand(s);
058 // System.out.println("extracted "+c);
059 if(c != null){
060 lastCommand = c;
061 }
062 }catch(IOException ioe){
063 System.err.println("Problem writing output");
064 ioe.printStackTrace(System.err);
065 }
066 }
067
068
069 /** insert head.html if found */
070 public void begin(){
071 writeln("% AUTOGENERATED FILE: ALL EDITS WILL BE LOST!!!");
072 writeln("\\documentclass[a4paper,12pt]{report}");
073 writeln("\\usepackage{fancyvrb, graphicx}");
074 writeln("\\usepackage[breaklinks=true,colorlinks=true, anchorcolor={green} ]{hyperref}");
075 writeln("\\begin{document}");
076 }
077 /** insert tail.html if found */
078 public void end(){
079 writeln("\\bibliographystyle{apalike}");
080 writeln("\\bibliography{../big.bib}");
081 writeln("\\end{document}");
082 try{
083 out.flush();
084 }catch(IOException ioe){
085 System.err.println("Problem writing LaTeX output");
086 ioe.printStackTrace(System.err);
087 }
088 }
089 protected String extractLastCommand(String text){
090 if(text == null){ return null; }
091 int i = text.lastIndexOf("\\");
092 if(i < 0){ return null; }
093 int begin = i;
094 i++;
095 int end = 0;
096 while(i < text.length() && text.charAt(i) != '}'){
097 i++;
098 }
099 if(i >= text.length()){ return null; // <... not terminated
100 }
101 end = i;
102 return text.substring(begin, end);
103 }
104 public void text(String t){
105 //write(angleBracketsEscape(t));
106 write(t);
107 }
108 public void bold(String t){
109 write("{\\bf " + angleBracketsEscape(t) + "}");
110 }
111 public void italic(String t){
112 write("{\\em " + angleBracketsEscape(t) + "}");
113 }
114 public void tt(String t){
115 write("{\\tt " + angleBracketsEscape(t) + "}");
116 }
117 public void beginListItem(int level){
118 write("\\item{");
119 }
120 public void endListItem(int level){
121 writeln("}\n");
122 }
123 public void begin_ul(int level){
124 write("\\begin{itemize}\n");
125 }
126 public void end_ul(int level){
127 write("\\end{itemize}\n");
128 }
129 public void begin_ol(int level){
130 write("\\begin{enumerate}\n");
131 }
132 public void end_ol(int level){
133 write("\\end{enumerate}\n");
134 }
135 public void paragraph(){
136 write("\n\n");
137 }
138 public void linebreak(){
139 write("\\\\");
140 }
141 public void blankline(){
142 write("\\\\ \n \\\\ \n");
143 }
144 public void code(String c){
145 write("\n\\begin{Verbatim}");
146 write(c);
147 write("\\end{Verbatim}\n\n");
148 }
149 public void verbatim(String rawOutput){
150 write(rawOutput);
151 }
152 public void blockquote(String q){
153 write("\\begin{quote}\n");
154 write("" + angleBracketsEscape(q) + "");
155 write("\n\\end{quote}\n\n");
156 }
157 public void link(String url, String title){
158 if(title == null){
159 title = url;
160 }
161 title = angleBracketsEscape(title);
162 write("\\href{" + url + "}{" + title + "}");
163 }
164 public void anchor(String name){
165 write("\\hypertarget{" + name +"}{}");
166 }
167 public void title(String title) {
168 writeln("\\title{" + title+ "}\n");
169 // writeln("\\input{preamble}");
170 writeln("\\maketitle");
171 }
172 public void beginSection(String title, int level){
173 title = angleBracketsEscape(title);
174 write("\\");
175 if(level == 1) {
176 writeln("chapter{"+title+"}");
177 }
178 else {
179 for(int i = 2; i < level; ++i) {
180 write("sub");
181 }
182 writeln("section{" + title+"}");
183 }
184
185 }
186
187 public void begin_table(){
188 tableColumnCount = 0;
189 inTable=true;
190 firstRow=true;
191 tableContents = "";
192
193 }
194 public void end_table(){
195 if(firstRow) {
196 ++tableColumnCount;
197 firstRow = false;
198 }
199 write("\\\\ \\hline \\end{tabular}");
200 inTable=false;
201 String colWidth = Float.toString(1.0F/tableColumnCount);
202 String pre = "\n\n\\begin{tabular}{";
203 for(int i = 0; i < tableColumnCount; ++i) {
204 pre += "|p{"+colWidth + "\\textwidth}";
205 }
206 pre += "|} \n \\hline";
207 tableContents = pre + tableContents;
208 write(tableContents);
209
210 }
211 public void col(){
212 if(firstRow) {
213 ++tableColumnCount;
214 }
215 write("&");
216 }
217 public void row(){
218 if(firstRow) {
219 ++tableColumnCount;
220 firstRow = false;
221 }
222 write("\\\\ \\hline");
223 }
224 protected String angleBracketsEscape(String q){
225 q = q.replaceAll("%","\\\\%");
226 q = q.replaceAll("#","\\\\#");
227 q = q.replaceAll("_","\\\\_");
228 return q;
229 }
230 public void hr(){
231 write("\\hrule");
232 }
233 public String beginEM() {
234 return("{\\em ");
235 }
236 public String endEM() {
237 return("}");
238 }
239 public String beginTT() {
240 return("{\\tt ");
241 }
242 public String endTT() {
243 return("}");
244 }
245 public String beginBF() {
246 return("{\\b ");
247 }
248 public String endBF() {
249 return("}");
250 }
251 public void longdash() {
252 write("---");
253 }
254 public void contents() {
255 writeln("\\tableofcontents");
256 }
257
258
259
260 /** Former plugins were turned into methods */
261
262 public void image(YAMContext context, Vector args) {
263 // The call to super is necessary beacause the test of the arguments is done there
264 super.image(context, args);
265
266 String imageName = (String) args.elementAt(0);
267 String width = "";
268 String height = "";
269
270 if ( args.size()>1 ) {
271 width = (String)args.elementAt(1);
272 if ( args.size()>2 ) {
273 height = (String)args.elementAt(2);
274 }
275 }// fi
276
277 StringBuffer ret = new StringBuffer("%output(\\includegraphics");
278 boolean hw = false;
279 String heightwidth="";
280 if(width!= "") {
281 width = width.substring(0,(width.length()-2)) + "." +width.substring(width.length()-2);
282 heightwidth +="width=" + width + "\\textwidth";
283 hw = true;
284 }
285 if(height!=""){
286 if(hw) {
287 heightwidth += ", ";
288 }
289 hw = true;
290 height = height.substring(0,(height.length()-2)) + "." +height.substring(height.length()-2);
291 heightwidth +="height=" + height + "\\textheight";
292 }
293 if(hw) {
294 ret.append("[" + heightwidth + "]");
295 }
296 ret.append("{"+imageName +"})");
297
298 write(ret.toString());
299 }// End of image()
300
301
302 public void cite(YAMContext context, Vector args) {
303 // The call to super is necessary beacause the test of the arguments is done there
304 super.cite(context, args);
305
306 StringBuffer result = new StringBuffer("%output(~\\cite{");
307 for(int i = 0; i < args.size(); i++){
308 result.append(args.get(i));
309 // Separates multiple citations with comma.
310 // The following code doesn't append a comma for the last citation
311 if(i < (args.size() - 1)) result.append(", ");
312 }
313 result.append("})");
314
315 write(result.toString());
316 }// End of cite()
317
318
319 public void box(YAMContext context, Vector args) {
320 // The call to super is necessary beacause the test of the arguments is done there
321 super.box(context, args);
322
323 String tml = (String)args.elementAt(0);
324 StringBuffer ret = new StringBuffer("%output(\\begin{tabular}{|p{2cm}|}\n \\hline \\\\ \n");
325 ret.append(tml.toString());
326 ret.append("\\\\ \\hline \n \\end{tabular})");
327 }// End of box()
328
329
330
331 public void eps(YAMContext context, Vector args) {
332 // The call to super is necessary beacause the test of the arguments is done there
333 super.eps(context, args);
334
335 // The call to super is necessary beacause the test of the arguments is done there
336 super.eps(context, args);
337
338 String imageName = (String)args.elementAt(0);
339 String scalex = "1.0";
340 String scaley = "1.0";
341 if ( args.size()>1 ) {
342 if ( args.size()!=3 ) {
343 System.err.println("Missing EPS arg(s); line "+context.getLine());
344 return;
345 }
346 scalex = (String)args.elementAt(1);
347 scaley = (String)args.elementAt(2);
348 }
349
350 write("\n[Image "+imageName+"]\n");
351 }// End of eps()
352
353
354 public void footnote(YAMContext context, Vector args) {
355 // The call to super is necessary beacause the test of the arguments is done there
356 super.footnote(context, args);
357
358 String tml = (String)args.elementAt(0);
359 String ret = "%output(\\footnote{" +
360 (tml.toString()).replaceAll("_","\\\\_") + "})";
361 write(ret);
362 }// End of footnote()
363
364
365 public void notes(YAMContext context, Vector args) {
366 // The call to super is necessary beacause the test of the arguments is done there
367 super.notes(context, args);
368
369 String notes = (String) args.elementAt(0);
370 String output = "% ";
371 int i = 0, j = 0;
372 for(i = 0; i < notes.length(); ++i) {
373 if(notes.charAt(i) == '\n') {
374 output += notes.subSequence(j, i+1) + "% ";
375 j = i+1;
376 }
377
378 }
379 i = notes.length();
380 output += notes.subSequence(j, i);
381
382 write(output);
383
384 }// End of notes()
385
386 public void syndiag(YAMContext context, Vector args) {
387 super.syndiag(context, args);
388
389 String grammarSpec = (String)args.elementAt(0);
390 // just return grammar spec for anything but lout for now
391 write("\n<<\n"+grammarSpec+">>\n");
392
393 }// End of syndiag()
394
395 public void tree(YAMContext context, Vector args) {
396 // The call to super is necessary beacause the test of the arguments is done there
397 super.tree(context, args);
398
399 String treeSpec = (String)args.elementAt(0);
400 // just return tree spec for anything but lout for now
401 write(treeSpec);
402
403 }// End of tree()
404
405
406 }//End of LaTeXTarget
|