1 package be
.nikiroo
.fanfix
.reader
;
3 import java
.io
.IOException
;
4 import java
.util
.Arrays
;
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
;
14 * This class can export a chapter into HTML3 code ready for Java Swing support.
18 public class TextOutput
{
19 private StringBuilder builder
;
20 private BasicOutput output
;
21 private Story fakeStory
;
22 private boolean chapterName
;
25 * Create a new {@link TextOutput} that will convert a {@link Chapter} into
26 * HTML3 suited for Java Swing.
29 * TRUE if you want a standalone document (with an <HTML> tag)
31 public TextOutput(final boolean standalone
) {
32 builder
= new StringBuilder();
33 fakeStory
= new Story();
35 output
= new BasicOutput() {
36 private boolean paraInQuote
;
39 protected void writeChapterHeader(Chapter chap
) throws IOException
{
41 builder
.append("<HTML style='line-height: 5px;'>");
45 builder
.append("<H1>");
46 builder
.append("Chapter ");
47 builder
.append(chap
.getNumber());
48 if (chap
.getName() != null
49 && !chap
.getName().trim().isEmpty()) {
51 builder
.append(chap
.getName());
53 builder
.append("</H1>");
56 builder
.append("<DIV align='justify'>");
60 protected void writeChapterFooter(Chapter chap
) throws IOException
{
62 builder
.append("</DIV>");
66 builder
.append("</DIV>");
69 builder
.append("</HTML>");
74 protected void writeParagraph(Paragraph para
) throws IOException
{
75 if ((para
.getType() == ParagraphType
.QUOTE
) == !paraInQuote
) {
76 paraInQuote
= !paraInQuote
;
78 builder
.append("<BR>");
79 builder
.append("<DIV>");
81 builder
.append("</DIV>");
82 builder
.append("<BR>");
86 switch (para
.getType()) {
88 builder
.append(" ");
89 builder
.append(decorateText(para
.getContent()));
90 builder
.append("<BR>");
93 builder
.append("<FONT SIZE='1'><BR></FONT>");
97 builder
.append("<P COLOR='#AAAAAA' ALIGN='CENTER'>");
98 builder
.append("<FONT SIZE='5'>* * *</FONT>");
99 builder
.append("</P>");
100 builder
.append("<BR>");
103 builder
.append("<DIV>");
104 builder
.append(" ");
105 builder
.append("— ");
106 builder
.append(decorateText(para
.getContent()));
107 builder
.append("</DIV>");
115 protected String
enbold(String word
) {
116 // Used to be COLOR='#7777DD'
117 return "<B>" + word
+ "</B>";
121 protected String
italize(String word
) {
122 return "<I COLOR='GRAY'>" + word
+ "</I>";
128 * Convert the chapter into HTML3 code.
131 * the {@link Chapter} to convert
133 * display the chapter name
135 * @return HTML3 code tested with Java Swing
137 public String
convert(Chapter chap
, boolean chapterName
) {
138 this.chapterName
= chapterName
;
139 builder
.setLength(0);
141 fakeStory
.setChapters(Arrays
.asList(chap
));
142 output
.process(fakeStory
, null, null);
143 } catch (IOException e
) {
144 Instance
.getInstance().getTraceHandler().error(e
);
146 return builder
.toString();