[919] | 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 |
|
---|
[4] | 19 | package org.expeditee.io;
|
---|
| 20 |
|
---|
| 21 | import java.io.IOException;
|
---|
[306] | 22 | import java.io.Writer;
|
---|
[4] | 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;
|
---|
[1529] | 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 | }
|
---|
[4] | 54 |
|
---|
| 55 | @Override
|
---|
[306] | 56 | protected void initialise(Frame start, Writer writer) throws IOException {
|
---|
[4] | 57 | _format = ".tex";
|
---|
[1529] | 58 | Text annotation = start.getAnnotation("flowwalker");
|
---|
| 59 | if (annotation != null) {
|
---|
| 60 | useFlowWalker = true;
|
---|
| 61 | }
|
---|
[306] | 62 | super.initialise(start, writer);
|
---|
[4] | 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 |
|
---|
[80] | 79 | List<String> titleLines = _title.getTextList();
|
---|
[4] | 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;
|
---|
[80] | 99 | List<String> toWrite = text.getTextList();
|
---|
[4] | 100 |
|
---|
| 101 | String first = toWrite.get(0);
|
---|
[17] | 102 | TexEnvironment te = new TexEnvironment(first);
|
---|
[4] | 103 |
|
---|
[17] | 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);
|
---|
[4] | 108 | }
|
---|
| 109 | }
|
---|
[17] | 110 |
|
---|
[4] | 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;
|
---|
[80] | 118 | List<String> toWrite = text.getTextList();
|
---|
[4] | 119 |
|
---|
| 120 | String first = toWrite.get(0);
|
---|
[17] | 121 | TexEnvironment te = new TexEnvironment(first);
|
---|
[4] | 122 |
|
---|
[17] | 123 | if (te.isValid()) {
|
---|
| 124 | _writer.write("\\end{" + te.getName() + "}" + ItemWriter.NEW_LINE);
|
---|
[4] | 125 | }
|
---|
| 126 |
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | @Override
|
---|
[1529] | 130 | protected void writeText(Text text) throws IOException {
|
---|
[80] | 131 | for (String s : text.getTextList()) {
|
---|
[4] | 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 | }
|
---|
[17] | 142 |
|
---|