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 |
|
---|
35 | @Override
|
---|
36 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
37 | _format = ".tex";
|
---|
38 | super.initialise(start, writer);
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected void writeTitle(Text title, List<Item> items) throws IOException {
|
---|
42 | _title = title;
|
---|
43 |
|
---|
44 | Text text = ItemUtils.FindTag(items, "@TexSection");
|
---|
45 |
|
---|
46 | if (text != null) {
|
---|
47 | String first = text.getFirstLine();
|
---|
48 | int ind = first.indexOf(":");
|
---|
49 |
|
---|
50 | if (ind > 0) {
|
---|
51 | String command = first.substring(ind + 1).trim().toLowerCase();
|
---|
52 |
|
---|
53 | _writer.write("\\" + command + "{");
|
---|
54 |
|
---|
55 | List<String> titleLines = _title.getTextList();
|
---|
56 | for (int i = 0; i < titleLines.size() - 1; i++) {
|
---|
57 | _writer.write(titleLines.get(i));
|
---|
58 | _writer.write(ItemWriter.NEW_LINE);
|
---|
59 | }
|
---|
60 |
|
---|
61 | _writer.write(titleLines.get(titleLines.size() - 1));
|
---|
62 |
|
---|
63 | _writer.write("}" + ItemWriter.NEW_LINE);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | protected void writeStartLink(Item linker) throws IOException {
|
---|
70 | // only output text items
|
---|
71 | if (!(linker instanceof Text))
|
---|
72 | return;
|
---|
73 |
|
---|
74 | Text text = (Text) linker;
|
---|
75 | List<String> toWrite = text.getTextList();
|
---|
76 |
|
---|
77 | String first = toWrite.get(0);
|
---|
78 | TexEnvironment te = new TexEnvironment(first);
|
---|
79 |
|
---|
80 | if (te.isValid()) {
|
---|
81 | if (te.hasComment())
|
---|
82 | _writer.write("%" + te.getComment() + ItemWriter.NEW_LINE);
|
---|
83 | _writer.write("\\begin{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected void writeEndLink(Item linker) throws IOException {
|
---|
89 | // only output text items
|
---|
90 | if (!(linker instanceof Text))
|
---|
91 | return;
|
---|
92 |
|
---|
93 | Text text = (Text) linker;
|
---|
94 | List<String> toWrite = text.getTextList();
|
---|
95 |
|
---|
96 | String first = toWrite.get(0);
|
---|
97 | TexEnvironment te = new TexEnvironment(first);
|
---|
98 |
|
---|
99 | if (te.isValid()) {
|
---|
100 | _writer.write("\\end{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | protected void writeText(Text text) throws IOException {
|
---|
107 | for (String s : text.getTextList()) {
|
---|
108 | _writer.write(s);
|
---|
109 | _writer.write(ItemWriter.NEW_LINE);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | protected void writeAnnotationText(Text text) throws IOException {
|
---|
115 |
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|