Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / ui / GuiReaderViewerTextOutput.java
CommitLineData
1ee67095
NR
1package be.nikiroo.fanfix.reader.ui;
2
3import java.io.IOException;
4import java.util.Arrays;
5
6import be.nikiroo.fanfix.Instance;
7import be.nikiroo.fanfix.data.Chapter;
8import be.nikiroo.fanfix.data.Paragraph;
e2da2602 9import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
1ee67095
NR
10import be.nikiroo.fanfix.data.Story;
11import 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 */
32ba91e9 18public class GuiReaderViewerTextOutput {
1ee67095
NR
19 private StringBuilder builder;
20 private BasicOutput output;
21 private Story fakeStory;
22
23 /**
32ba91e9 24 * Create a new {@link GuiReaderViewerTextOutput} that will convert a
1ee67095
NR
25 * {@link Chapter} into HTML3 suited for Java Swing.
26 */
32ba91e9 27 public GuiReaderViewerTextOutput() {
1ee67095
NR
28 builder = new StringBuilder();
29 fakeStory = new Story();
30
31 output = new BasicOutput() {
32 private boolean paraInQuote;
33
34 @Override
35 protected void writeChapterHeader(Chapter chap) throws IOException {
36 builder.append("<HTML>");
37
38 builder.append("<H1>");
39 builder.append("Chapter ");
40 builder.append(chap.getNumber());
41 builder.append(": ");
42 builder.append(chap.getName());
43 builder.append("</H1>");
44
908bbb35 45 builder.append("<DIV align='justify'>");
1ee67095
NR
46 }
47
48 @Override
49 protected void writeChapterFooter(Chapter chap) throws IOException {
50 if (paraInQuote) {
51 builder.append("</DIV>");
52 }
53 paraInQuote = false;
54
908bbb35 55 builder.append("</DIV>");
1ee67095
NR
56 builder.append("</HTML>");
57 }
58
59 @Override
60 protected void writeParagraph(Paragraph para) throws IOException {
e2da2602
NR
61 if ((para.getType() == ParagraphType.QUOTE) == !paraInQuote) {
62 paraInQuote = !paraInQuote;
63 if (paraInQuote) {
64 builder.append("<BR>");
65 builder.append("<DIV>");
66 } else {
67 builder.append("</DIV>");
68 builder.append("<BR>");
69 }
70 }
71
1ee67095
NR
72 switch (para.getType()) {
73 case NORMAL:
e2da2602 74 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
1ee67095
NR
75 builder.append(decorateText(para.getContent()));
76 builder.append("<BR>");
77 break;
78 case BLANK:
e2da2602 79 builder.append("<BR><BR>");
1ee67095
NR
80 break;
81 case BREAK:
e2da2602
NR
82 builder.append("<BR><P COLOR='#7777DD' ALIGN='CENTER'><B>");
83 builder.append("* * *");
84 builder.append("</B></P><BR><BR>");
1ee67095
NR
85 break;
86 case QUOTE:
1ee67095 87 builder.append("<DIV>");
e2da2602
NR
88 builder.append("&nbsp;&nbsp;&nbsp;&nbsp;");
89 builder.append("&mdash;&nbsp;");
1ee67095
NR
90 builder.append(decorateText(para.getContent()));
91 builder.append("</DIV>");
92
93 break;
94 case IMAGE:
95 }
96 }
97
98 @Override
99 protected String enbold(String word) {
e2da2602 100 return "<B COLOR='#7777DD'>" + word + "</B>";
1ee67095
NR
101 }
102
103 @Override
104 protected String italize(String word) {
105 return "<I COLOR='GRAY'>" + word + "</I>";
106 }
107 };
108 }
109
110 /**
111 * Convert the chapter into HTML3 code.
112 *
113 * @param chap
114 * the {@link Chapter} to convert.
115 *
116 * @return HTML3 code tested with Java Swing
117 */
118 public String convert(Chapter chap) {
119 builder.setLength(0);
120 try {
121 fakeStory.setChapters(Arrays.asList(chap));
122 output.process(fakeStory, null, null);
123 } catch (IOException e) {
124 Instance.getTraceHandler().error(e);
125 }
126 return builder.toString();
127 }
128}