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 |
|
---|
19 | package org.expeditee.io;
|
---|
20 |
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.Writer;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.items.Item;
|
---|
27 | import org.expeditee.items.ItemUtils;
|
---|
28 | import org.expeditee.items.Text;
|
---|
29 |
|
---|
30 | public 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 | outputTreeFlowWalkerStyle(toWrite);
|
---|
41 | } else {
|
---|
42 | outputTree(toWrite);
|
---|
43 | }
|
---|
44 | } catch (IOException ioe) {
|
---|
45 | _running = false;
|
---|
46 | throw ioe;
|
---|
47 | } catch (Exception e) {
|
---|
48 | e.printStackTrace();
|
---|
49 | }
|
---|
50 | _running = false;
|
---|
51 | return finaliseTree();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
56 | _format = ".tex";
|
---|
57 | Text annotation = start.getAnnotation("flowwalker");
|
---|
58 | if (annotation != null) {
|
---|
59 | useFlowWalker = true;
|
---|
60 | }
|
---|
61 | super.initialise(start, writer);
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected void writeTitle(Text title, List<Item> items) throws IOException {
|
---|
65 | _title = title;
|
---|
66 |
|
---|
67 | Text text = ItemUtils.FindTag(items, "@TexSection");
|
---|
68 |
|
---|
69 | if (text != null) {
|
---|
70 | String first = text.getFirstLine();
|
---|
71 | int ind = first.indexOf(":");
|
---|
72 |
|
---|
73 | if (ind > 0) {
|
---|
74 | String command = first.substring(ind + 1).trim().toLowerCase();
|
---|
75 |
|
---|
76 | _writer.write("\\" + command + "{");
|
---|
77 |
|
---|
78 | List<String> titleLines = _title.getTextList();
|
---|
79 | for (int i = 0; i < titleLines.size() - 1; i++) {
|
---|
80 | _writer.write(titleLines.get(i));
|
---|
81 | _writer.write(ItemWriter.NEW_LINE);
|
---|
82 | }
|
---|
83 |
|
---|
84 | _writer.write(titleLines.get(titleLines.size() - 1));
|
---|
85 |
|
---|
86 | _writer.write("}" + ItemWriter.NEW_LINE);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | protected void writeStartLink(Item linker) throws IOException {
|
---|
93 | // only output text items
|
---|
94 | if (!(linker instanceof Text))
|
---|
95 | return;
|
---|
96 |
|
---|
97 | Text text = (Text) linker;
|
---|
98 | List<String> toWrite = text.getTextList();
|
---|
99 |
|
---|
100 | String first = toWrite.get(0);
|
---|
101 | TexEnvironment te = new TexEnvironment(first);
|
---|
102 |
|
---|
103 | if (te.isValid()) {
|
---|
104 | if (te.hasComment())
|
---|
105 | _writer.write("%" + te.getComment() + ItemWriter.NEW_LINE);
|
---|
106 | _writer.write("\\begin{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | protected void writeEndLink(Item linker) throws IOException {
|
---|
112 | // only output text items
|
---|
113 | if (!(linker instanceof Text))
|
---|
114 | return;
|
---|
115 |
|
---|
116 | Text text = (Text) linker;
|
---|
117 | List<String> toWrite = text.getTextList();
|
---|
118 |
|
---|
119 | String first = toWrite.get(0);
|
---|
120 | TexEnvironment te = new TexEnvironment(first);
|
---|
121 |
|
---|
122 | if (te.isValid()) {
|
---|
123 | _writer.write("\\end{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | protected void writeText(Text text) throws IOException {
|
---|
130 | for (String s : text.getTextList()) {
|
---|
131 | _writer.write(s);
|
---|
132 | _writer.write(ItemWriter.NEW_LINE);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | protected void writeAnnotationText(Text text) throws IOException {
|
---|
138 |
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|