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 target
= new File(targetDir
, targetName
+ getDefaultExtension(true));
39 writer
= new BufferedWriter(new OutputStreamWriter(
40 new FileOutputStream(target
), "UTF-8"));
42 super.process(story
, targetDir
, targetNameOrig
);
48 String format
= Instance
.getConfig()
49 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
50 if (story
.getMeta().getCover() != null) {
51 ImageIO
.write(story
.getMeta().getCover(), format
, new File(dir
,
59 public String
getDefaultExtension(boolean readerTarget
) {
61 return File
.separator
+ "index.html";
68 protected void writeStoryHeader(Story story
) throws IOException
{
72 Chapter resume
= null;
73 if (story
.getMeta() != null) {
74 MetaData meta
= story
.getMeta();
75 title
= meta
.getTitle();
76 resume
= meta
.getResume();
77 if (meta
.getTags() != null) {
78 for (String tag
: meta
.getTags()) {
79 if (!tags
.isEmpty()) {
85 if (!tags
.isEmpty()) {
86 tags
= "(" + tags
+ ")";
89 author
= meta
.getAuthor();
92 String format
= Instance
.getConfig()
93 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
95 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
96 if (inStyle
== null) {
97 throw new IOException("Cannot find style.css resource");
100 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
105 writer
.write("<!DOCTYPE html>");
106 writer
.write("\n<html>");
107 writer
.write("\n<head>");
108 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
109 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
110 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
111 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
112 writer
.write("\n</head>");
113 writer
.write("\n<body>\n");
115 writer
.write("\n <div class=\"titlepage\">");
116 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
117 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
119 writer
.write("\n <div class=\"cover\">");
120 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
121 writer
.write("\n </div>");
122 writer
.write("\n <div class=\"author\">"
123 + StringUtils
.xmlEscape(author
) + "</div>");
124 writer
.write("\n </div>");
126 writer
.write("\n <hr/><br/>");
128 if (resume
!= null) {
129 for (Paragraph para
: resume
) {
130 writeParagraph(para
);
133 writer
.write(" </div>\n");
137 writer
.write(" </div>\n");
142 writer
.write("\n <br/>");
146 protected void writeStoryFooter(Story story
) throws IOException
{
147 writer
.write("</body>\n");
151 protected void writeChapterHeader(Chapter chap
) throws IOException
{
153 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
154 nameOrNumber
= chap
.getName();
156 nameOrNumber
= Integer
.toString(chap
.getNumber());
159 writer
.write("\n <h2>");
160 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
161 + chap
.getNumber() + "</span>:</span> ");
162 writer
.write("\n <span class='chaptitle'>"
163 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
164 writer
.write("\n </h2>");
166 writer
.write("\n <div class='chapter_content'>\n");
173 protected void writeChapterFooter(Chapter chap
) throws IOException
{
175 writer
.write(" </div>\n");
179 writer
.write(" </div>\n");
183 writer
.write("\n </div>");
187 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
188 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
189 writer
.write(" <div class='dialogues'>\n");
191 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
192 writer
.write(" </div>\n");
196 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
197 writer
.write(" <div class='normals'>\n");
199 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
200 writer
.write(" </div>\n");
204 switch (para
.getType()) {
206 writer
.write(" <div class='blank'></div>");
209 writer
.write(" <hr/>");
212 writer
.write(" <span class='normal'>");
215 writer
.write(" <div class='dialogue'>— ");
219 writer
.write("<a href='"
220 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
221 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
227 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
228 switch (para
.getType()) {
230 writer
.write("</span>\n");
233 writer
.write("</div>\n");
242 protected void writeTextLine(ParagraphType type
, String line
)
247 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
255 protected String
enbold(String word
) {
256 return "<strong>" + word
+ "</strong>";
260 protected String
italize(String word
) {
261 return "<emph>" + word
+ "</emph>";