source: trunk/src/org/expeditee/io/TEXWriter.java@ 1529

Last change on this file since 1529 was 1529, checked in by bnemhaus, 4 years ago

When running the "WriteTree tex" action, the presence of the annotation "@flowwalker" is tested for. If it is present then the flow walker is used!

File size: 3.6 KB
Line 
1/**
2 * TEXWriter.java
3 * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package org.expeditee.io;
20
21import java.io.IOException;
22import java.io.Writer;
23import java.util.List;
24
25import org.expeditee.gui.Frame;
26import org.expeditee.items.Item;
27import org.expeditee.items.ItemUtils;
28import org.expeditee.items.Text;
29
30public class TEXWriter extends DefaultTreeWriter {
31
32 // may be needed for sectioning commands
33 private Text _title = null;
34 private boolean useFlowWalker = false;
35
36 public String writeTree(Frame toWrite) throws IOException {
37 try {
38 initialise(toWrite);
39 if (useFlowWalker) {
40 System.err.println("USING FLOW WALKER FOR TEXTWriter");
41 outputTreeFlowWalkerStyle(toWrite);
42 } else {
43 outputTree(toWrite);
44 }
45 } catch (IOException ioe) {
46 _running = false;
47 throw ioe;
48 } catch (Exception e) {
49 e.printStackTrace();
50 }
51 _running = false;
52 return finaliseTree();
53 }
54
55 @Override
56 protected void initialise(Frame start, Writer writer) throws IOException {
57 _format = ".tex";
58 Text annotation = start.getAnnotation("flowwalker");
59 if (annotation != null) {
60 useFlowWalker = true;
61 }
62 super.initialise(start, writer);
63 }
64
65 protected void writeTitle(Text title, List<Item> items) throws IOException {
66 _title = title;
67
68 Text text = ItemUtils.FindTag(items, "@TexSection");
69
70 if (text != null) {
71 String first = text.getFirstLine();
72 int ind = first.indexOf(":");
73
74 if (ind > 0) {
75 String command = first.substring(ind + 1).trim().toLowerCase();
76
77 _writer.write("\\" + command + "{");
78
79 List<String> titleLines = _title.getTextList();
80 for (int i = 0; i < titleLines.size() - 1; i++) {
81 _writer.write(titleLines.get(i));
82 _writer.write(ItemWriter.NEW_LINE);
83 }
84
85 _writer.write(titleLines.get(titleLines.size() - 1));
86
87 _writer.write("}" + ItemWriter.NEW_LINE);
88 }
89 }
90 }
91
92 @Override
93 protected void writeStartLink(Item linker) throws IOException {
94 // only output text items
95 if (!(linker instanceof Text))
96 return;
97
98 Text text = (Text) linker;
99 List<String> toWrite = text.getTextList();
100
101 String first = toWrite.get(0);
102 TexEnvironment te = new TexEnvironment(first);
103
104 if (te.isValid()) {
105 if (te.hasComment())
106 _writer.write("%" + te.getComment() + ItemWriter.NEW_LINE);
107 _writer.write("\\begin{" + te.getName() + "}" + ItemWriter.NEW_LINE);
108 }
109 }
110
111 @Override
112 protected void writeEndLink(Item linker) throws IOException {
113 // only output text items
114 if (!(linker instanceof Text))
115 return;
116
117 Text text = (Text) linker;
118 List<String> toWrite = text.getTextList();
119
120 String first = toWrite.get(0);
121 TexEnvironment te = new TexEnvironment(first);
122
123 if (te.isValid()) {
124 _writer.write("\\end{" + te.getName() + "}" + ItemWriter.NEW_LINE);
125 }
126
127 }
128
129 @Override
130 protected void writeText(Text text) throws IOException {
131 for (String s : text.getTextList()) {
132 _writer.write(s);
133 _writer.write(ItemWriter.NEW_LINE);
134 }
135 }
136
137 @Override
138 protected void writeAnnotationText(Text text) throws IOException {
139
140 }
141}
142
Note: See TracBrowser for help on using the repository browser.