1 package be
.nikiroo
.fanfix
.output
;
4 import java
.io
.FileWriter
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
8 import javax
.imageio
.ImageIO
;
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 FileWriter 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 targetName
= new File(targetName
, "index").getPath();
36 targetName
+= getDefaultExtension();
38 target
= new File(targetDir
, targetName
);
40 writer
= new FileWriter(target
);
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() {
64 protected void writeStoryHeader(Story story
) throws IOException
{
68 Chapter resume
= null;
69 if (story
.getMeta() != null) {
70 MetaData meta
= story
.getMeta();
71 title
= meta
.getTitle();
72 resume
= meta
.getResume();
73 if (meta
.getTags() != null) {
74 for (String tag
: meta
.getTags()) {
75 if (!tags
.isEmpty()) {
81 if (!tags
.isEmpty()) {
82 tags
= "(" + tags
+ ")";
85 author
= meta
.getAuthor();
88 String format
= Instance
.getConfig()
89 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
91 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
92 if (inStyle
== null) {
93 throw new IOException("Cannot find style.css resource");
96 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
101 writer
.write("<!DOCTYPE html>");
102 writer
.write("\n<html>");
103 writer
.write("\n<head>");
104 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
105 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
106 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
107 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
108 writer
.write("\n</head>");
109 writer
.write("\n<body>\n");
111 writer
.write("\n <div class=\"titlepage\">");
112 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
113 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
115 writer
.write("\n <div class=\"cover\">");
116 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
117 writer
.write("\n </div>");
118 writer
.write("\n <div class=\"author\">"
119 + StringUtils
.xmlEscape(author
) + "</div>");
120 writer
.write("\n </div>");
122 writer
.write("\n <hr/><br/>");
124 if (resume
!= null) {
125 for (Paragraph para
: resume
) {
126 writeParagraph(para
);
129 writer
.write(" </div>\n");
133 writer
.write(" </div>\n");
138 writer
.write("\n <br/>");
142 protected void writeStoryFooter(Story story
) throws IOException
{
143 writer
.write("</body>\n");
147 protected void writeChapterHeader(Chapter chap
) throws IOException
{
149 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
150 nameOrNumber
= chap
.getName();
152 nameOrNumber
= Integer
.toString(chap
.getNumber());
155 writer
.write("\n <h2>");
156 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
157 + chap
.getNumber() + "</span>:</span> ");
158 writer
.write("\n <span class='chaptitle'>"
159 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
160 writer
.write("\n </h2>");
162 writer
.write("\n <div class='chapter_content'>\n");
169 protected void writeChapterFooter(Chapter chap
) throws IOException
{
171 writer
.write(" </div>\n");
175 writer
.write(" </div>\n");
179 writer
.write("\n </div>");
183 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
184 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
185 writer
.write(" <div class='dialogues'>\n");
187 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
188 writer
.write(" </div>\n");
192 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
193 writer
.write(" <div class='normals'>\n");
195 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
196 writer
.write(" </div>\n");
200 switch (para
.getType()) {
202 writer
.write(" <div class='blank'></div>");
205 writer
.write(" <hr/>");
208 writer
.write(" <span class='normal'>");
211 writer
.write(" <div class='dialogue'>— ");
215 writer
.write("<a href='"
216 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
217 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
223 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
224 switch (para
.getType()) {
226 writer
.write("</span>\n");
229 writer
.write("</div>\n");
238 protected void writeTextLine(ParagraphType type
, String line
)
243 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
251 protected String
enbold(String word
) {
252 return "<strong>" + word
+ "</strong>";
256 protected String
italize(String word
) {
257 return "<emph>" + word
+ "</emph>";