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
.getInstance().getCache().saveAsImage(story
.getMeta().getCover(), new File(dir
, "cover"), true);
60 public String
getDefaultExtension(boolean readerTarget
) {
62 return File
.separator
+ "index.html";
69 protected void writeStoryHeader(Story story
) throws IOException
{
73 Chapter resume
= null;
74 if (story
.getMeta() != null) {
75 MetaData meta
= story
.getMeta();
76 title
= meta
.getTitle();
77 resume
= meta
.getResume();
78 if (meta
.getTags() != null) {
79 for (String tag
: meta
.getTags()) {
80 if (!tags
.isEmpty()) {
86 if (!tags
.isEmpty()) {
87 tags
= "(" + tags
+ ")";
90 author
= meta
.getAuthor();
93 String format
= Instance
.getInstance().getConfig()
94 .getString(Config
.FILE_FORMAT_IMAGE_FORMAT_COVER
).toLowerCase();
96 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
97 if (inStyle
== null) {
98 throw new IOException("Cannot find style.css resource");
101 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
106 writer
.write("<!DOCTYPE html>");
107 writer
.write("\n<html>");
108 writer
.write("\n<head>");
109 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
110 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
111 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
112 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
113 writer
.write("\n</head>");
114 writer
.write("\n<body>\n");
116 writer
.write("\n <div class=\"titlepage\">");
117 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
118 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
120 writer
.write("\n <div class=\"cover\">");
121 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
122 writer
.write("\n </div>");
123 writer
.write("\n <div class=\"author\">"
124 + StringUtils
.xmlEscape(author
) + "</div>");
125 writer
.write("\n </div>");
127 writer
.write("\n <hr/><br/>");
129 if (resume
!= null) {
130 for (Paragraph para
: resume
) {
131 writeParagraph(para
);
134 writer
.write(" </div>\n");
138 writer
.write(" </div>\n");
143 writer
.write("\n <br/>");
147 protected void writeStoryFooter(Story story
) throws IOException
{
148 writer
.write("</body>\n");
152 protected void writeChapterHeader(Chapter chap
) throws IOException
{
154 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
155 nameOrNumber
= chap
.getName();
157 nameOrNumber
= Integer
.toString(chap
.getNumber());
160 writer
.write("\n <h2>");
161 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
162 + chap
.getNumber() + "</span>:</span> ");
163 writer
.write("\n <span class='chaptitle'>"
164 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
165 writer
.write("\n </h2>");
167 writer
.write("\n <div class='chapter_content'>\n");
174 protected void writeChapterFooter(Chapter chap
) throws IOException
{
176 writer
.write(" </div>\n");
180 writer
.write(" </div>\n");
184 writer
.write("\n </div>");
188 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
189 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
190 writer
.write(" <div class='dialogues'>\n");
192 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
193 writer
.write(" </div>\n");
197 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
198 writer
.write(" <div class='normals'>\n");
200 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
201 writer
.write(" </div>\n");
205 switch (para
.getType()) {
207 writer
.write(" <div class='blank'></div>");
210 writer
.write(" <hr class='break'/>");
213 writer
.write(" <span class='normal'>");
216 writer
.write(" <div class='dialogue'>— ");
219 // TODO check if images work OK
220 writer
.write("<a href='"
221 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
222 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
228 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
229 switch (para
.getType()) {
231 writer
.write("</span>\n");
234 writer
.write("</div>\n");
243 protected void writeTextLine(ParagraphType type
, String line
)
248 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
256 protected String
enbold(String word
) {
257 return "<strong>" + word
+ "</strong>";
261 protected String
italize(String word
) {
262 return "<emph>" + word
+ "</emph>";