001 /*
002 [Adapted from BSD licence]
003 Copyright (c) 2002 Terence Parr
004 All rights reserved.
005
006 Redistribution and use in source and binary forms, with or without
007 modification, are permitted provided that the following conditions
008 are met:
009 1. Redistributions of source code must retain the above copyright
010 notice, this list of conditions and the following disclaimer.
011 2. Redistributions in binary form must reproduce the above copyright
012 notice, this list of conditions and the following disclaimer in the
013 documentation and/or other materials provided with the distribution.
014 3. The name of the author may not be used to endorse or promote products
015 derived from this software without specific prior written permission.
016
017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package gate.wiki.antlr;
029
030 import java.io.*;
031 import java.util.Vector;
032 import java.util.Date;
033
034 public class LoutTarget extends NOPTarget{
035
036 protected boolean generateSlides = false;
037
038 public LoutTarget() {
039 this(new BufferedWriter(new OutputStreamWriter(System.out)));
040 }
041
042 public LoutTarget(String args) {
043 this();
044 if ( args!=null ) {
045 if ( args.equals("slides") ) {
046 generateSlides = true;
047 }
048 }
049 }
050
051 public LoutTarget(Writer out) {
052 this.out = out;
053 }
054
055 public String getTargetLanguage() {
056 return "Lout";
057 }
058
059 protected String extractLastCommand(String text) {
060 if ( text==null ) {
061 return null;
062 }
063 int i = text.lastIndexOf("@");
064 // System.out.println("index of @ in ["+text+"]=="+i);
065 if ( i<0 ) {
066 return null;
067 }
068 int begin = i;
069 i++;
070 int end = 0;
071 while ( i<text.length() &&
072 Character.isLetterOrDigit(text.charAt(i)))
073 {
074 i++;
075 }
076 end = i;
077 return text.substring(begin, end);
078 }
079
080 public void begin() {
081 writeln("@SysInclude{tbl}");
082 writeln("@SysInclude{diag}");
083 if ( generateSlides ) {
084 writeln("@SysInclude{slides}");
085 writeln("@OverheadTransparencies");
086 writeln("@InitialFont {Helvetica Base 16p}");
087 //writeln("@PageOrientation {Landscape}");
088 }
089 else {
090 writeln("@SysInclude{doc}");
091 writeln("@Doc @Text @Begin");
092 }
093 }
094
095 /** insert tail.hYAM if found */
096 public void end() {
097 if ( !generateSlides ) {
098 writeln("@End @Text");
099 }
100 try {
101 out.flush();
102 }
103 catch (IOException ioe) {
104 System.err.println("Problem writing Lout output");
105 ioe.printStackTrace(System.err);
106 }
107 }
108
109 public void text(String t) {
110 t = t.replaceAll("\"", "\"\\\"\"");
111 t = t.replaceAll("/", "\"/\"");
112 write(t);
113 }
114
115 public void bold(String t) {
116 write("@B{" + t + "}");
117 }
118
119 public void italic(String t) {
120 write("@I{" + t + "}");
121 }
122
123 public void tt(String t) {
124 write("@F{" + t + "}");
125 }
126
127 public void beginListItem(int level) {
128 write("@ListItem {");
129 }
130
131 public void endListItem(int level) {
132 writeln("}");
133 }
134
135 public void begin_ul(int level) {
136 writeln("@BulletList");
137 }
138
139 public void end_ul(int level) {
140 writeln("\n@EndList");
141 }
142
143 public void begin_ol(int level) {
144 writeln("@NumberedList");
145 }
146
147 public void end_ol(int level) {
148 writeln("\n@EndList");
149 }
150
151 public void paragraph() {
152 String last = getLastOutputCommand();
153 // System.out.println("last cmd=["+last+"]");
154 if ( last!=null &&
155 !(last.startsWith("@EndList")||
156 last.startsWith("@QuotedDisplay")) )
157 {
158 writeln("\n@LP");
159 }
160 }
161
162 public void linebreak() {
163 write(" @LP\n\n");
164 }
165
166 public void blankline() {
167 write(" @LP @LP\n");
168 }
169
170 public void code(String c) {
171 write("\n@LP @F @Verbatim @Begin");
172 writeln(c);
173 write("@End @Verbatim");
174 }
175
176 public void verbatim(String rawOutput) {
177 write(rawOutput);
178 }
179
180 public void blockquote(String q) {
181 writeln("@QuotedDisplay { @I {");
182 write("``"+q+"''");
183 writeln("}}\n");
184 }
185
186 public void link(String url, String title) {
187 if (title == null) {
188 write("@Verbatim{"+url+"}");
189 return;
190 }
191 write("@B{"+title+"} (@Verbatim{"+url+"})");
192 }
193
194 public void title(String title) {
195 if ( generateSlides ) {
196 writeln("@Title{"+title+"}");
197 writeln("//");
198 }
199 else {
200 writeln("@Heading{"+title+"}");
201 }
202 }
203
204 public void beginSectionList(int level) {
205 if ( !generateSlides ) {
206 writeln("@Begin"+nSubs(level)+"Sections");
207 }
208 }
209
210 public void endSectionList(int level) {
211 if ( !generateSlides ) {
212 writeln("@End"+nSubs(level)+"Sections");
213 }
214 }
215
216 public void beginSection(String title, int level) {
217 if ( level==YAMContext.LOWEST_LEVEL && generateSlides ) {
218 writeln("@Overhead");
219 writeln(" @Title{"+title+"}");
220 writeln(" @Format{ @CurveBox @HExpand @VExpand @Body }");
221 writeln("@Begin");
222 }
223 else {
224 writeln("@"+nSubs(level)+"Section");
225 writeln(" @Title{"+title+"}");
226 writeln("@Begin");
227 }
228 }
229
230 public void endSection(int level) {
231 if ( level==YAMContext.LOWEST_LEVEL && generateSlides ) {
232 writeln("@End @Overhead");
233 }
234 else {
235 writeln("@End @"+nSubs(level)+"Section");
236 }
237 }
238
239 public void begin_table() {
240 write("\n<TABLE cellspacing=0 border=0>\n");
241 write("<TR><TD>");
242 }
243
244 public void end_table() {
245 write("</TD></TR>\n");
246 write("</TABLE>\n");
247 }
248
249 public void col() {
250 write("</TD><TD>");
251 }
252
253 public void row() {
254 write("</TD>\n</TR>\n<TR>\n<TD>");
255 }
256
257 public boolean isGeneratingSlides() {
258 return generateSlides;
259 }
260
261 // SUPPORT ROUTINES
262
263 private String nSubs(int n) {
264 String s = "";
265 for (int i=1; i<n; i++) {
266 s += "Sub";
267 }
268 return s;
269 }
270
271
272 /** Former plugins */
273
274 public void image(YAMContext context, Vector args) {
275 super.image(context, args);
276
277 }
278
279 public void cite(YAMContext context, Vector args) {
280 super.cite(context, args);
281 }
282
283 public void box(YAMContext context, Vector args) {
284 super.box(context, args);
285 }
286
287 public void c(YAMContext context, Vector args) {
288 super.c(context, args);
289 }
290
291
292 public void eps(YAMContext context, Vector args) {
293 super.eps(context, args);
294
295
296 }// End of eps()
297
298 public void footnote(YAMContext context, Vector args) {
299 super.footnote(context, args);
300 }
301
302
303
304 public void notes(YAMContext context, Vector args) {
305 super.notes(context, args);
306
307 }// End of notes()
308
309 public void syndiag(YAMContext context, Vector args) {
310 super.syndiag(context, args);
311 }
312
313 public void tree(YAMContext context, Vector args) {
314 super.tree(context, args);
315 }
316
317
318
319 }//End of LoutTarget
|