1 package be
.nikiroo
.fanfix
.output
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileOutputStream
;
6 import java
.io
.IOException
;
7 import java
.io
.InputStream
;
8 import java
.io
.OutputStreamWriter
;
10 import javax
.imageio
.ImageIO
;
12 import be
.nikiroo
.fanfix
.Instance
;
13 import be
.nikiroo
.fanfix
.bundles
.Config
;
14 import be
.nikiroo
.fanfix
.data
.Chapter
;
15 import be
.nikiroo
.fanfix
.data
.MetaData
;
16 import be
.nikiroo
.fanfix
.data
.Paragraph
;
17 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
18 import be
.nikiroo
.fanfix
.data
.Story
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.StringUtils
;
22 class Html
extends BasicOutput
{
24 protected BufferedWriter writer
;
25 private boolean inDialogue
= false;
26 private boolean inNormal
= false;
29 public File
process(Story story
, File targetDir
, String targetName
)
31 String targetNameOrig
= targetName
;
33 File target
= new File(targetDir
, targetName
);
37 // write a copy of the originals inside
38 InfoCover
.writeInfo(dir
, targetName
, story
.getMeta());
39 InfoCover
.writeCover(dir
, targetName
, story
.getMeta());
40 new InfoText().process(story
, dir
, targetNameOrig
);
42 target
= new File(targetDir
, targetName
+ getDefaultExtension(true));
44 writer
= new BufferedWriter(new OutputStreamWriter(
45 new FileOutputStream(target
), "UTF-8"));
47 super.process(story
, targetDir
, targetNameOrig
);
53 String format
= Instance
.getConfig()
54 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
55 if (story
.getMeta().getCover() != null) {
56 ImageIO
.write(story
.getMeta().getCover(), format
, new File(dir
,
64 public String
getDefaultExtension(boolean readerTarget
) {
66 return File
.separator
+ "index.html";
73 protected void writeStoryHeader(Story story
) throws IOException
{
77 Chapter resume
= null;
78 if (story
.getMeta() != null) {
79 MetaData meta
= story
.getMeta();
80 title
= meta
.getTitle();
81 resume
= meta
.getResume();
82 if (meta
.getTags() != null) {
83 for (String tag
: meta
.getTags()) {
84 if (!tags
.isEmpty()) {
90 if (!tags
.isEmpty()) {
91 tags
= "(" + tags
+ ")";
94 author
= meta
.getAuthor();
97 String format
= Instance
.getConfig()
98 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
100 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
101 if (inStyle
== null) {
102 throw new IOException("Cannot find style.css resource");
105 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
110 writer
.write("<!DOCTYPE html>");
111 writer
.write("\n<html>");
112 writer
.write("\n<head>");
113 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
114 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
115 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
116 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
117 writer
.write("\n</head>");
118 writer
.write("\n<body>\n");
120 writer
.write("\n <div class=\"titlepage\">");
121 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
122 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
124 writer
.write("\n <div class=\"cover\">");
125 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
126 writer
.write("\n </div>");
127 writer
.write("\n <div class=\"author\">"
128 + StringUtils
.xmlEscape(author
) + "</div>");
129 writer
.write("\n </div>");
131 writer
.write("\n <hr/><br/>");
133 if (resume
!= null) {
134 for (Paragraph para
: resume
) {
135 writeParagraph(para
);
138 writer
.write(" </div>\n");
142 writer
.write(" </div>\n");
147 writer
.write("\n <br/>");
151 protected void writeStoryFooter(Story story
) throws IOException
{
152 writer
.write("</body>\n");
156 protected void writeChapterHeader(Chapter chap
) throws IOException
{
158 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
159 nameOrNumber
= chap
.getName();
161 nameOrNumber
= Integer
.toString(chap
.getNumber());
164 writer
.write("\n <h2>");
165 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
166 + chap
.getNumber() + "</span>:</span> ");
167 writer
.write("\n <span class='chaptitle'>"
168 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
169 writer
.write("\n </h2>");
171 writer
.write("\n <div class='chapter_content'>\n");
178 protected void writeChapterFooter(Chapter chap
) throws IOException
{
180 writer
.write(" </div>\n");
184 writer
.write(" </div>\n");
188 writer
.write("\n </div>");
192 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
193 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
194 writer
.write(" <div class='dialogues'>\n");
196 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
197 writer
.write(" </div>\n");
201 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
202 writer
.write(" <div class='normals'>\n");
204 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
205 writer
.write(" </div>\n");
209 switch (para
.getType()) {
211 writer
.write(" <div class='blank'></div>");
214 writer
.write(" <hr class='break'/>");
217 writer
.write(" <span class='normal'>");
220 writer
.write(" <div class='dialogue'>— ");
224 writer
.write("<a href='"
225 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
226 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
232 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
233 switch (para
.getType()) {
235 writer
.write("</span>\n");
238 writer
.write("</div>\n");
247 protected void writeTextLine(ParagraphType type
, String line
)
252 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
260 protected String
enbold(String word
) {
261 return "<strong>" + word
+ "</strong>";
265 protected String
italize(String word
) {
266 return "<emph>" + word
+ "</emph>";