JsonIO: progress can have a key 'meta'
[fanfix.git] / src / be / nikiroo / fanfix / reader / TextOutput.java
1 package be.nikiroo.fanfix.reader;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 import be.nikiroo.fanfix.Instance;
7 import be.nikiroo.fanfix.data.Chapter;
8 import be.nikiroo.fanfix.data.Paragraph;
9 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
10 import be.nikiroo.fanfix.data.Story;
11 import be.nikiroo.fanfix.output.BasicOutput;
12
13 /**
14 * This class can export a chapter into HTML3 code ready for Java Swing support.
15 *
16 * @author niki
17 */
18 public class TextOutput {
19 private StringBuilder builder;
20 private BasicOutput output;
21 private Story fakeStory;
22 private boolean chapterName;
23
24 /**
25 * Create a new {@link TextOutput} that will convert a {@link Chapter} into
26 * HTML3 suited for Java Swing.
27 *
28 * @param standalone
29 * TRUE if you want a standalone document (with an <HTML> tag)
30 */
31 public TextOutput(final boolean standalone) {
32 builder = new StringBuilder();
33 fakeStory = new Story();
34
35 output = new BasicOutput() {
36 private boolean paraInQuote;
37
38 @Override
39 protected void writeChapterHeader(Chapter chap) throws IOException {
40 if (standalone) {
41 builder.append("<HTML style='line-height: 5px;'>");
42 }
43
44 if (chapterName) {
45 builder.append("<H1>");
46 builder.append("Chapter ");
47 builder.append(chap.getNumber());
48 if (chap.getName() != null
49 && !chap.getName().trim().isEmpty()) {
50 builder.append(": ");
51 builder.append(chap.getName());
52 }
53 builder.append("</H1>");
54 }
55
56 builder.append("<DIV align='justify'>");
57 }
58
59 @Override
60 protected void writeChapterFooter(Chapter chap) throws IOException {
61 if (paraInQuote) {
62 builder.append("</DIV>");
63 }
64 paraInQuote = false;
65
66 builder.append("</DIV>");
67
68 if (standalone) {
69 builder.append("</HTML>");
70 }
71 }
72
73 @Override
74 protected void writeParagraph(Paragraph para) throws IOException {
75 if ((para.getType() == ParagraphType.QUOTE) == !paraInQuote) {
76 paraInQuote = !paraInQuote;
77 if (paraInQuote) {
78 builder.append("<BR>");
79 builder.append("<DIV>");
80 } else {
81 builder.append("</DIV>");
82 builder.append("<BR>");
83 }
84 }
85
86 switch (para.getType()) {
87 case NORMAL:
88 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
89 builder.append(decorateText(para.getContent()));
90 builder.append("<BR>");
91 break;
92 case BLANK:
93 builder.append("<FONT SIZE='1'><BR></FONT>");
94 break;
95 case BREAK:
96 // Used to be 7777DD
97 builder.append("<P COLOR='#AAAAAA' ALIGN='CENTER'>");
98 builder.append("<FONT SIZE='5'>* * *</FONT>");
99 builder.append("</P>");
100 builder.append("<BR>");
101 break;
102 case QUOTE:
103 builder.append("<DIV>");
104 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
105 builder.append("&mdash;&nbsp;");
106 builder.append(decorateText(para.getContent()));
107 builder.append("</DIV>");
108
109 break;
110 case IMAGE:
111 }
112 }
113
114 @Override
115 protected String enbold(String word) {
116 // Used to be COLOR='#7777DD'
117 return "<B>" + word + "</B>";
118 }
119
120 @Override
121 protected String italize(String word) {
122 return "<I COLOR='GRAY'>" + word + "</I>";
123 }
124 };
125 }
126
127 /**
128 * Convert the chapter into HTML3 code.
129 *
130 * @param chap
131 * the {@link Chapter} to convert
132 * @param chapterName
133 * display the chapter name
134 *
135 * @return HTML3 code tested with Java Swing
136 */
137 public String convert(Chapter chap, boolean chapterName) {
138 this.chapterName = chapterName;
139 builder.setLength(0);
140 try {
141 fakeStory.setChapters(Arrays.asList(chap));
142 output.process(fakeStory, null, null);
143 } catch (IOException e) {
144 Instance.getInstance().getTraceHandler().error(e);
145 }
146 return builder.toString();
147 }
148 }