Commit | Line | Data |
---|---|---|
1ee67095 NR |
1 | package be.nikiroo.fanfix.reader.ui; |
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.Story; | |
10 | import be.nikiroo.fanfix.output.BasicOutput; | |
11 | ||
12 | /** | |
13 | * This class can export a chapter into HTML3 code ready for Java Swing support. | |
14 | * | |
15 | * @author niki | |
16 | */ | |
32ba91e9 | 17 | public class GuiReaderViewerTextOutput { |
1ee67095 NR |
18 | private StringBuilder builder; |
19 | private BasicOutput output; | |
20 | private Story fakeStory; | |
21 | ||
22 | /** | |
32ba91e9 | 23 | * Create a new {@link GuiReaderViewerTextOutput} that will convert a |
1ee67095 NR |
24 | * {@link Chapter} into HTML3 suited for Java Swing. |
25 | */ | |
32ba91e9 | 26 | public GuiReaderViewerTextOutput() { |
1ee67095 NR |
27 | builder = new StringBuilder(); |
28 | fakeStory = new Story(); | |
29 | ||
30 | output = new BasicOutput() { | |
31 | private boolean paraInQuote; | |
32 | ||
33 | @Override | |
34 | protected void writeChapterHeader(Chapter chap) throws IOException { | |
35 | builder.append("<HTML>"); | |
36 | ||
37 | builder.append("<H1>"); | |
38 | builder.append("Chapter "); | |
39 | builder.append(chap.getNumber()); | |
40 | builder.append(": "); | |
41 | builder.append(chap.getName()); | |
42 | builder.append("</H1>"); | |
43 | ||
44 | builder.append("<JUSTIFY>"); | |
45 | for (Paragraph para : chap) { | |
46 | writeParagraph(para); | |
47 | } | |
48 | } | |
49 | ||
50 | @Override | |
51 | protected void writeChapterFooter(Chapter chap) throws IOException { | |
52 | if (paraInQuote) { | |
53 | builder.append("</DIV>"); | |
54 | } | |
55 | paraInQuote = false; | |
56 | ||
57 | builder.append("</JUSTIFY>"); | |
58 | builder.append("</HTML>"); | |
59 | } | |
60 | ||
61 | @Override | |
62 | protected void writeParagraph(Paragraph para) throws IOException { | |
63 | switch (para.getType()) { | |
64 | case NORMAL: | |
65 | builder.append(decorateText(para.getContent())); | |
66 | builder.append("<BR>"); | |
67 | break; | |
68 | case BLANK: | |
69 | builder.append("<BR>"); | |
70 | break; | |
71 | case BREAK: | |
72 | builder.append("<BR>* * *<BR><BR>"); | |
73 | break; | |
74 | case QUOTE: | |
75 | if (!paraInQuote) { | |
76 | builder.append("<DIV>"); | |
77 | } else { | |
78 | builder.append("</DIV>"); | |
79 | } | |
80 | paraInQuote = !paraInQuote; | |
81 | ||
82 | builder.append("<DIV>"); | |
83 | builder.append("– "); | |
84 | builder.append(decorateText(para.getContent())); | |
85 | builder.append("</DIV>"); | |
86 | ||
87 | break; | |
88 | case IMAGE: | |
89 | } | |
90 | } | |
91 | ||
92 | @Override | |
93 | protected String enbold(String word) { | |
94 | return "<B COLOR='BLUE'>" + word + "</B>"; | |
95 | } | |
96 | ||
97 | @Override | |
98 | protected String italize(String word) { | |
99 | return "<I COLOR='GRAY'>" + word + "</I>"; | |
100 | } | |
101 | }; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Convert the chapter into HTML3 code. | |
106 | * | |
107 | * @param chap | |
108 | * the {@link Chapter} to convert. | |
109 | * | |
110 | * @return HTML3 code tested with Java Swing | |
111 | */ | |
112 | public String convert(Chapter chap) { | |
113 | builder.setLength(0); | |
114 | try { | |
115 | fakeStory.setChapters(Arrays.asList(chap)); | |
116 | output.process(fakeStory, null, null); | |
117 | } catch (IOException e) { | |
118 | Instance.getTraceHandler().error(e); | |
119 | } | |
120 | return builder.toString(); | |
121 | } | |
122 | } |