3ed52ad6a3b22491397b61aeb31175320dbcdada
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 targetName
+= getDefaultExtension();
39 target
= new File(targetDir
, targetName
);
41 writer
= new BufferedWriter(new OutputStreamWriter(
42 new FileOutputStream(target
), "UTF-8"));
44 super.process(story
, targetDir
, targetNameOrig
);
50 String format
= Instance
.getConfig()
51 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
52 if (story
.getMeta().getCover() != null) {
53 ImageIO
.write(story
.getMeta().getCover(), format
, new File(dir
,
61 public String
getDefaultExtension() {
62 return File
.separator
+ "index.html";
66 protected void writeStoryHeader(Story story
) throws IOException
{
70 Chapter resume
= null;
71 if (story
.getMeta() != null) {
72 MetaData meta
= story
.getMeta();
73 title
= meta
.getTitle();
74 resume
= meta
.getResume();
75 if (meta
.getTags() != null) {
76 for (String tag
: meta
.getTags()) {
77 if (!tags
.isEmpty()) {
83 if (!tags
.isEmpty()) {
84 tags
= "(" + tags
+ ")";
87 author
= meta
.getAuthor();
90 String format
= Instance
.getConfig()
91 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
93 InputStream inStyle
= getClass().getResourceAsStream("html.style.css");
94 if (inStyle
== null) {
95 throw new IOException("Cannot find style.css resource");
98 IOUtils
.write(inStyle
, new File(dir
, "style.css"));
103 writer
.write("<!DOCTYPE html>");
104 writer
.write("\n<html>");
105 writer
.write("\n<head>");
106 writer
.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>");
107 writer
.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
108 writer
.write("\n <link rel='stylesheet' type='text/css' href='style.css'>");
109 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
110 writer
.write("\n</head>");
111 writer
.write("\n<body>\n");
113 writer
.write("\n <div class=\"titlepage\">");
114 writer
.write("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
115 writer
.write("\n <div class=\"type\">" + StringUtils
.xmlEscape(tags
)
117 writer
.write("\n <div class=\"cover\">");
118 writer
.write("\n <img src=\"cover." + format
+ "\"></img>");
119 writer
.write("\n </div>");
120 writer
.write("\n <div class=\"author\">"
121 + StringUtils
.xmlEscape(author
) + "</div>");
122 writer
.write("\n </div>");
124 writer
.write("\n <hr/><br/>");
126 if (resume
!= null) {
127 for (Paragraph para
: resume
) {
128 writeParagraph(para
);
131 writer
.write(" </div>\n");
135 writer
.write(" </div>\n");
140 writer
.write("\n <br/>");
144 protected void writeStoryFooter(Story story
) throws IOException
{
145 writer
.write("</body>\n");
149 protected void writeChapterHeader(Chapter chap
) throws IOException
{
151 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
152 nameOrNumber
= chap
.getName();
154 nameOrNumber
= Integer
.toString(chap
.getNumber());
157 writer
.write("\n <h2>");
158 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
159 + chap
.getNumber() + "</span>:</span> ");
160 writer
.write("\n <span class='chaptitle'>"
161 + StringUtils
.xmlEscape(nameOrNumber
) + "</span>");
162 writer
.write("\n </h2>");
164 writer
.write("\n <div class='chapter_content'>\n");
171 protected void writeChapterFooter(Chapter chap
) throws IOException
{
173 writer
.write(" </div>\n");
177 writer
.write(" </div>\n");
181 writer
.write("\n </div>");
185 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
186 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
187 writer
.write(" <div class='dialogues'>\n");
189 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
190 writer
.write(" </div>\n");
194 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
195 writer
.write(" <div class='normals'>\n");
197 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
198 writer
.write(" </div>\n");
202 switch (para
.getType()) {
204 writer
.write(" <div class='blank'></div>");
207 writer
.write(" <hr/>");
210 writer
.write(" <span class='normal'>");
213 writer
.write(" <div class='dialogue'>— ");
217 writer
.write("<a href='"
218 + StringUtils
.xmlEscapeQuote(para
.getContent()) + "'>"
219 + StringUtils
.xmlEscape(para
.getContent()) + "</a>");
225 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
226 switch (para
.getType()) {
228 writer
.write("</span>\n");
231 writer
.write("</div>\n");
240 protected void writeTextLine(ParagraphType type
, String line
)
245 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
253 protected String
enbold(String word
) {
254 return "<strong>" + word
+ "</strong>";
258 protected String
italize(String word
) {
259 return "<emph>" + word
+ "</emph>";