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 be
.nikiroo
.fanfix
.Instance
;
11 import be
.nikiroo
.fanfix
.bundles
.Config
;
12 import be
.nikiroo
.fanfix
.data
.Chapter
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Paragraph
;
15 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
16 import be
.nikiroo
.fanfix
.data
.Story
;
17 import be
.nikiroo
.utils
.IOUtils
;
18 import be
.nikiroo
.utils
.StringUtils
;
20 class Html
extends BasicOutput
{
22 protected BufferedWriter writer
;
23 private boolean inDialogue
= false;
24 private boolean inNormal
= false;
27 public File
process(Story story
, File targetDir
, String targetName
)
29 String targetNameOrig
= targetName
;
31 File target
= new File(targetDir
, targetName
);
35 target
= new File(targetDir
, targetName
+ getDefaultExtension(true));
37 writer
= new BufferedWriter(new OutputStreamWriter(
38 new FileOutputStream(target
), "UTF-8"));
40 super.process(story
, targetDir
, targetNameOrig
);
46 // write a copy of the originals inside
47 InfoCover
.writeInfo(dir
, targetName
, story
.getMeta());
48 InfoCover
.writeCover(dir
, targetName
, story
.getMeta());
49 BasicOutput
.getOutput(OutputType
.TEXT
, isWriteInfo(), isWriteCover())
50 .process(story
, dir
, targetNameOrig
);
52 if (story
.getMeta().getCover() != null) {
53 Instance
.getCache().saveAsImage(story
.getMeta().getCover(),
54 new File(dir
, "cover"), true);
61 public String
getDefaultExtension(boolean readerTarget
) {
63 return File
.separator
+ "index.html";
70 protected void writeStoryHeader(Story story
) throws IOException
{
74 Chapter resume
= null;
75 if (story
.getMeta() != null) {
76 MetaData meta
= story
.getMeta();
77 title
= meta
.getTitle();
78 resume
= meta
.getResume();
79 if (meta
.getTags() != null) {
80 for (String tag
: meta
.getTags()) {
81 if (!tags
.isEmpty()) {
87 if (!tags
.isEmpty()) {
88 tags
= "(" + tags
+ ")";
91 author
= meta
.getAuthor();
94 String format
= Instance
.getConfig()
95 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
97 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
98 if (inStyle
== null) {
99 throw new IOException("Cannot find style.css resource");
102 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
107 writer
.write("<!DOCTYPE html>");
108 writer
.write("\n<html>");
109 writer
.write("\n<head>");
110 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
111 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
112 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
113 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
114 writer
.write("\n</head>");
115 writer
.write("\n<body>\n");
117 writer
.write("\n <div class=\"titlepage\">");
118 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
119 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
121 writer
.write("\n <div class=\"cover\">");
122 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
123 writer
.write("\n </div>");
124 writer
.write("\n <div class=\"author\">"
125 + StringUtils
.xmlEscape(author
) + "</div>");
126 writer
.write("\n </div>");
128 writer
.write("\n <hr/><br/>");
130 if (resume
!= null) {
131 for (Paragraph para
: resume
) {
132 writeParagraph(para
);
135 writer
.write(" </div>\n");
139 writer
.write(" </div>\n");
144 writer
.write("\n <br/>");
148 protected void writeStoryFooter(Story story
) throws IOException
{
149 writer
.write("</body>\n");
153 protected void writeChapterHeader(Chapter chap
) throws IOException
{
155 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
156 nameOrNumber
= chap
.getName();
158 nameOrNumber
= Integer
.toString(chap
.getNumber());
161 writer
.write("\n <h2>");
162 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
163 + chap
.getNumber() + "</span>:</span> ");
164 writer
.write("\n <span class='chaptitle'>"
165 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
166 writer
.write("\n </h2>");
168 writer
.write("\n <div class='chapter_content'>\n");
175 protected void writeChapterFooter(Chapter chap
) throws IOException
{
177 writer
.write(" </div>\n");
181 writer
.write(" </div>\n");
185 writer
.write("\n </div>");
189 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
190 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
191 writer
.write(" <div class='dialogues'>\n");
193 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
194 writer
.write(" </div>\n");
198 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
199 writer
.write(" <div class='normals'>\n");
201 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
202 writer
.write(" </div>\n");
206 switch (para
.getType()) {
208 writer
.write(" <div class='blank'></div>");
211 writer
.write(" <hr class='break'/>");
214 writer
.write(" <span class='normal'>");
217 writer
.write(" <div class='dialogue'>— ");
221 writer
.write("<a href='"
222 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
223 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
229 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
230 switch (para
.getType()) {
232 writer
.write("</span>\n");
235 writer
.write("</div>\n");
244 protected void writeTextLine(ParagraphType type
, String line
)
249 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
257 protected String
enbold(String word
) {
258 return "<strong>" + word
+ "</strong>";
262 protected String
italize(String word
) {
263 return "<emph>" + word
+ "</emph>";