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 | ||
908bbb35 | 44 | builder.append("<DIV align='justify'>"); |
1ee67095 NR |
45 | } |
46 | ||
47 | @Override | |
48 | protected void writeChapterFooter(Chapter chap) throws IOException { | |
49 | if (paraInQuote) { | |
50 | builder.append("</DIV>"); | |
51 | } | |
52 | paraInQuote = false; | |
53 | ||
908bbb35 | 54 | builder.append("</DIV>"); |
1ee67095 NR |
55 | builder.append("</HTML>"); |
56 | } | |
57 | ||
58 | @Override | |
59 | protected void writeParagraph(Paragraph para) throws IOException { | |
60 | switch (para.getType()) { | |
61 | case NORMAL: | |
62 | builder.append(decorateText(para.getContent())); | |
63 | builder.append("<BR>"); | |
64 | break; | |
65 | case BLANK: | |
66 | builder.append("<BR>"); | |
67 | break; | |
68 | case BREAK: | |
69 | builder.append("<BR>* * *<BR><BR>"); | |
70 | break; | |
71 | case QUOTE: | |
72 | if (!paraInQuote) { | |
73 | builder.append("<DIV>"); | |
74 | } else { | |
75 | builder.append("</DIV>"); | |
76 | } | |
77 | paraInQuote = !paraInQuote; | |
78 | ||
79 | builder.append("<DIV>"); | |
80 | builder.append("– "); | |
81 | builder.append(decorateText(para.getContent())); | |
82 | builder.append("</DIV>"); | |
83 | ||
84 | break; | |
85 | case IMAGE: | |
86 | } | |
87 | } | |
88 | ||
89 | @Override | |
90 | protected String enbold(String word) { | |
91 | return "<B COLOR='BLUE'>" + word + "</B>"; | |
92 | } | |
93 | ||
94 | @Override | |
95 | protected String italize(String word) { | |
96 | return "<I COLOR='GRAY'>" + word + "</I>"; | |
97 | } | |
98 | }; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Convert the chapter into HTML3 code. | |
103 | * | |
104 | * @param chap | |
105 | * the {@link Chapter} to convert. | |
106 | * | |
107 | * @return HTML3 code tested with Java Swing | |
108 | */ | |
109 | public String convert(Chapter chap) { | |
110 | builder.setLength(0); | |
111 | try { | |
112 | fakeStory.setChapters(Arrays.asList(chap)); | |
113 | output.process(fakeStory, null, null); | |
114 | } catch (IOException e) { | |
115 | Instance.getTraceHandler().error(e); | |
116 | } | |
117 | return builder.toString(); | |
118 | } | |
119 | } |