1 | package org.expeditee.io;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.List;
|
---|
5 |
|
---|
6 | import org.expeditee.gui.Frame;
|
---|
7 | import org.expeditee.items.Item;
|
---|
8 | import org.expeditee.items.Picture;
|
---|
9 | import org.expeditee.items.Text;
|
---|
10 |
|
---|
11 | public class HTMLWriter extends DefaultTreeWriter {
|
---|
12 |
|
---|
13 | private int _indent = 0;
|
---|
14 |
|
---|
15 | private static final String INDENT = " ";
|
---|
16 |
|
---|
17 | @Override
|
---|
18 | protected void initialise(Frame start) throws IOException {
|
---|
19 | _format = ".html";
|
---|
20 | super.initialise(start);
|
---|
21 | }
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | protected void writeStartLink(Item linker) throws IOException {
|
---|
25 | // only output text items
|
---|
26 | if (!(linker instanceof Text)) {
|
---|
27 | _indent++;
|
---|
28 | return;
|
---|
29 | }
|
---|
30 |
|
---|
31 | Text text = (Text) linker;
|
---|
32 | List<String> toWrite = text.getText();
|
---|
33 |
|
---|
34 | String first = toWrite.get(0);
|
---|
35 | int ind = first.indexOf(":");
|
---|
36 |
|
---|
37 | if (ind > 0) {
|
---|
38 | String tag = first.substring(0, ind);
|
---|
39 | String remain = first.substring(ind + 1);
|
---|
40 |
|
---|
41 | if (remain.length() > 0) {
|
---|
42 | // indenting for comments
|
---|
43 | indent();
|
---|
44 | _writer.write("<!-- " + remain);
|
---|
45 |
|
---|
46 | for (int i = 1; i < toWrite.size(); i++)
|
---|
47 | _writer.write(ItemWriter.NEW_LINE + toWrite.get(i));
|
---|
48 |
|
---|
49 | _writer.write(" -->" + ItemWriter.NEW_LINE);
|
---|
50 | }
|
---|
51 |
|
---|
52 | // indenting for tag
|
---|
53 | indent();
|
---|
54 | _writer.write("<" + tag + ">" + ItemWriter.NEW_LINE);
|
---|
55 | }
|
---|
56 |
|
---|
57 | _indent++;
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | protected void writeEndLink(Item linker) throws IOException {
|
---|
62 | _indent--;
|
---|
63 |
|
---|
64 | // only output text items
|
---|
65 | if (!(linker instanceof Text))
|
---|
66 | return;
|
---|
67 |
|
---|
68 | Text text = (Text) linker;
|
---|
69 | List<String> toWrite = text.getText();
|
---|
70 |
|
---|
71 | String first = toWrite.get(0);
|
---|
72 | int ind = first.indexOf(":");
|
---|
73 |
|
---|
74 | if (ind > 0) {
|
---|
75 | String tag = first.substring(0, ind);
|
---|
76 |
|
---|
77 | // indenting for tag
|
---|
78 | indent();
|
---|
79 | _writer.write("</" + tag + ">" + ItemWriter.NEW_LINE);
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | protected void writeText(Text text) throws IOException {
|
---|
86 | for (String s : text.getText()) {
|
---|
87 |
|
---|
88 | indent();
|
---|
89 | // check if the first word is proceeded by a :
|
---|
90 | int firstColonIndex = s.indexOf(":");
|
---|
91 | int firstSpaceIndex = s.indexOf(" ");
|
---|
92 | if (firstColonIndex > 0 && firstColonIndex < firstSpaceIndex) {
|
---|
93 | _writer.write("<" + s.substring(0, firstColonIndex) + ">");
|
---|
94 | if (firstColonIndex < s.length())
|
---|
95 | _writer.write(s.substring(firstColonIndex + 1));
|
---|
96 | _writer.write("</" + s.substring(0, firstColonIndex) + ">");
|
---|
97 | } else
|
---|
98 | _writer.write(s);
|
---|
99 | _writer.write(ItemWriter.NEW_LINE);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private void indent() throws IOException {
|
---|
104 | for (int i = 0; i < _indent; i++)
|
---|
105 | _writer.write(INDENT);
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | protected void writePicture(Picture pic) throws IOException {
|
---|
110 | Text source = pic.getText();
|
---|
111 |
|
---|
112 | String line = source.getFirstLine();
|
---|
113 | line = line.substring(line.indexOf(":") + 1).trim();
|
---|
114 |
|
---|
115 | indent();
|
---|
116 |
|
---|
117 | _writer.write("<img src = '../images/" + line + "'>");
|
---|
118 | _writer.write(ItemWriter.NEW_LINE);
|
---|
119 | }
|
---|
120 | }
|
---|