1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.Writer;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import org.expeditee.gui.Frame;
|
---|
8 | import org.expeditee.items.Item;
|
---|
9 | import org.expeditee.items.ItemUtils;
|
---|
10 | import org.expeditee.items.Text;
|
---|
11 |
|
---|
12 | public class TEXWriter extends DefaultTreeWriter {
|
---|
13 |
|
---|
14 | // may be needed for sectioning commands
|
---|
15 | private Text _title = null;
|
---|
16 |
|
---|
17 | @Override
|
---|
18 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
19 | _format = ".tex";
|
---|
20 | super.initialise(start, writer);
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected void writeTitle(Text title, List<Item> items) throws IOException {
|
---|
24 | _title = title;
|
---|
25 |
|
---|
26 | Text text = ItemUtils.FindTag(items, "@TexSection");
|
---|
27 |
|
---|
28 | if (text != null) {
|
---|
29 | String first = text.getFirstLine();
|
---|
30 | int ind = first.indexOf(":");
|
---|
31 |
|
---|
32 | if (ind > 0) {
|
---|
33 | String command = first.substring(ind + 1).trim().toLowerCase();
|
---|
34 |
|
---|
35 | _writer.write("\\" + command + "{");
|
---|
36 |
|
---|
37 | List<String> titleLines = _title.getTextList();
|
---|
38 | for (int i = 0; i < titleLines.size() - 1; i++) {
|
---|
39 | _writer.write(titleLines.get(i));
|
---|
40 | _writer.write(ItemWriter.NEW_LINE);
|
---|
41 | }
|
---|
42 |
|
---|
43 | _writer.write(titleLines.get(titleLines.size() - 1));
|
---|
44 |
|
---|
45 | _writer.write("}" + ItemWriter.NEW_LINE);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected void writeStartLink(Item linker) throws IOException {
|
---|
52 | // only output text items
|
---|
53 | if (!(linker instanceof Text))
|
---|
54 | return;
|
---|
55 |
|
---|
56 | Text text = (Text) linker;
|
---|
57 | List<String> toWrite = text.getTextList();
|
---|
58 |
|
---|
59 | String first = toWrite.get(0);
|
---|
60 | TexEnvironment te = new TexEnvironment(first);
|
---|
61 |
|
---|
62 | if (te.isValid()) {
|
---|
63 | if (te.hasComment())
|
---|
64 | _writer.write("%" + te.getComment() + ItemWriter.NEW_LINE);
|
---|
65 | _writer.write("\\begin{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | protected void writeEndLink(Item linker) throws IOException {
|
---|
71 | // only output text items
|
---|
72 | if (!(linker instanceof Text))
|
---|
73 | return;
|
---|
74 |
|
---|
75 | Text text = (Text) linker;
|
---|
76 | List<String> toWrite = text.getTextList();
|
---|
77 |
|
---|
78 | String first = toWrite.get(0);
|
---|
79 | TexEnvironment te = new TexEnvironment(first);
|
---|
80 |
|
---|
81 | if (te.isValid()) {
|
---|
82 | _writer.write("\\end{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | protected void writeText(Text text) throws IOException {
|
---|
89 | for (String s : text.getTextList()) {
|
---|
90 | _writer.write(s);
|
---|
91 | _writer.write(ItemWriter.NEW_LINE);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | protected void writeAnnotationText(Text text) throws IOException {
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|