Commit | Line | Data |
---|---|---|
2206ef66 NR |
1 | package be.nikiroo.fanfix.output; |
2 | ||
3d247bc3 | 3 | import java.io.BufferedWriter; |
a6395bef | 4 | import java.io.File; |
3d247bc3 | 5 | import java.io.FileOutputStream; |
a6395bef | 6 | import java.io.IOException; |
333f0e7b | 7 | import java.io.InputStream; |
3d247bc3 | 8 | import java.io.OutputStreamWriter; |
333f0e7b | 9 | |
a6395bef | 10 | import be.nikiroo.fanfix.Instance; |
333f0e7b | 11 | import be.nikiroo.fanfix.bundles.Config; |
a6395bef | 12 | import be.nikiroo.fanfix.data.Chapter; |
333f0e7b | 13 | import be.nikiroo.fanfix.data.MetaData; |
a6395bef NR |
14 | import be.nikiroo.fanfix.data.Paragraph; |
15 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
16 | import be.nikiroo.fanfix.data.Story; | |
333f0e7b | 17 | import be.nikiroo.utils.IOUtils; |
a6395bef NR |
18 | import be.nikiroo.utils.StringUtils; |
19 | ||
20 | class Html extends BasicOutput { | |
333f0e7b | 21 | private File dir; |
3d247bc3 | 22 | protected BufferedWriter writer; |
a6395bef NR |
23 | private boolean inDialogue = false; |
24 | private boolean inNormal = false; | |
25 | ||
26 | @Override | |
27 | public File process(Story story, File targetDir, String targetName) | |
28 | throws IOException { | |
333f0e7b NR |
29 | String targetNameOrig = targetName; |
30 | ||
a6395bef NR |
31 | File target = new File(targetDir, targetName); |
32 | target.mkdir(); | |
333f0e7b | 33 | dir = target; |
a6395bef | 34 | |
10d558d2 | 35 | target = new File(targetDir, targetName + getDefaultExtension(true)); |
373da363 | 36 | |
3d247bc3 NR |
37 | writer = new BufferedWriter(new OutputStreamWriter( |
38 | new FileOutputStream(target), "UTF-8")); | |
a6395bef NR |
39 | try { |
40 | super.process(story, targetDir, targetNameOrig); | |
41 | } finally { | |
42 | writer.close(); | |
43 | writer = null; | |
44 | } | |
45 | ||
276f95c6 NR |
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); | |
51 | ||
333f0e7b | 52 | if (story.getMeta().getCover() != null) { |
d66deb8d | 53 | Instance.getInstance().getCache().saveAsImage(story.getMeta().getCover(), new File(dir, "cover"), true); |
333f0e7b NR |
54 | } |
55 | ||
a6395bef NR |
56 | return target; |
57 | } | |
58 | ||
59 | @Override | |
10d558d2 NR |
60 | public String getDefaultExtension(boolean readerTarget) { |
61 | if (readerTarget) { | |
62 | return File.separator + "index.html"; | |
10d558d2 | 63 | } |
211f7ddb NR |
64 | |
65 | return ""; | |
a6395bef NR |
66 | } |
67 | ||
68 | @Override | |
69 | protected void writeStoryHeader(Story story) throws IOException { | |
70 | String title = ""; | |
333f0e7b NR |
71 | String tags = ""; |
72 | String author = ""; | |
73 | Chapter resume = null; | |
a6395bef | 74 | if (story.getMeta() != null) { |
333f0e7b NR |
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()) { | |
81 | tags += ", "; | |
82 | } | |
83 | tags += tag; | |
84 | } | |
85 | ||
86 | if (!tags.isEmpty()) { | |
87 | tags = "(" + tags + ")"; | |
88 | } | |
89 | } | |
90 | author = meta.getAuthor(); | |
91 | } | |
92 | ||
d66deb8d | 93 | String format = Instance.getInstance().getConfig() |
13fdb89a | 94 | .getString(Config.FILE_FORMAT_IMAGE_FORMAT_COVER).toLowerCase(); |
333f0e7b NR |
95 | |
96 | InputStream inStyle = getClass().getResourceAsStream("html.style.css"); | |
97 | if (inStyle == null) { | |
98 | throw new IOException("Cannot find style.css resource"); | |
99 | } | |
100 | try { | |
101 | IOUtils.write(inStyle, new File(dir, "style.css")); | |
102 | } finally { | |
103 | inStyle.close(); | |
a6395bef NR |
104 | } |
105 | ||
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'>"); | |
333f0e7b | 111 | writer.write("\n <link rel='stylesheet' type='text/css' href='style.css'>"); |
a6395bef NR |
112 | writer.write("\n <title>" + StringUtils.xmlEscape(title) + "</title>"); |
113 | writer.write("\n</head>"); | |
114 | writer.write("\n<body>\n"); | |
115 | ||
333f0e7b NR |
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) | |
119 | + "</div>"); | |
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>"); | |
126 | ||
127 | writer.write("\n <hr/><br/>"); | |
128 | ||
129 | if (resume != null) { | |
130 | for (Paragraph para : resume) { | |
131 | writeParagraph(para); | |
132 | } | |
133 | if (inDialogue) { | |
134 | writer.write(" </div>\n"); | |
135 | inDialogue = false; | |
136 | } | |
137 | if (inNormal) { | |
138 | writer.write(" </div>\n"); | |
139 | inNormal = false; | |
140 | } | |
141 | } | |
142 | ||
143 | writer.write("\n <br/>"); | |
a6395bef NR |
144 | } |
145 | ||
146 | @Override | |
147 | protected void writeStoryFooter(Story story) throws IOException { | |
148 | writer.write("</body>\n"); | |
149 | } | |
150 | ||
151 | @Override | |
152 | protected void writeChapterHeader(Chapter chap) throws IOException { | |
333f0e7b | 153 | String nameOrNumber; |
a6395bef | 154 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
333f0e7b | 155 | nameOrNumber = chap.getName(); |
a6395bef | 156 | } else { |
333f0e7b | 157 | nameOrNumber = Integer.toString(chap.getNumber()); |
a6395bef NR |
158 | } |
159 | ||
333f0e7b NR |
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>"); | |
166 | writer.write("\n "); | |
167 | writer.write("\n <div class='chapter_content'>\n"); | |
a6395bef NR |
168 | |
169 | inDialogue = false; | |
170 | inNormal = false; | |
171 | } | |
172 | ||
333f0e7b NR |
173 | @Override |
174 | protected void writeChapterFooter(Chapter chap) throws IOException { | |
175 | if (inDialogue) { | |
176 | writer.write(" </div>\n"); | |
177 | inDialogue = false; | |
178 | } | |
179 | if (inNormal) { | |
180 | writer.write(" </div>\n"); | |
181 | inNormal = false; | |
182 | } | |
183 | ||
184 | writer.write("\n </div>"); | |
185 | } | |
186 | ||
a6395bef NR |
187 | @Override |
188 | protected void writeParagraphHeader(Paragraph para) throws IOException { | |
189 | if (para.getType() == ParagraphType.QUOTE && !inDialogue) { | |
190 | writer.write(" <div class='dialogues'>\n"); | |
191 | inDialogue = true; | |
192 | } else if (para.getType() != ParagraphType.QUOTE && inDialogue) { | |
193 | writer.write(" </div>\n"); | |
194 | inDialogue = false; | |
195 | } | |
196 | ||
197 | if (para.getType() == ParagraphType.NORMAL && !inNormal) { | |
198 | writer.write(" <div class='normals'>\n"); | |
199 | inNormal = true; | |
200 | } else if (para.getType() != ParagraphType.NORMAL && inNormal) { | |
201 | writer.write(" </div>\n"); | |
202 | inNormal = false; | |
203 | } | |
204 | ||
205 | switch (para.getType()) { | |
206 | case BLANK: | |
207 | writer.write(" <div class='blank'></div>"); | |
208 | break; | |
209 | case BREAK: | |
f977d05b | 210 | writer.write(" <hr class='break'/>"); |
a6395bef NR |
211 | break; |
212 | case NORMAL: | |
213 | writer.write(" <span class='normal'>"); | |
214 | break; | |
215 | case QUOTE: | |
216 | writer.write(" <div class='dialogue'>— "); | |
217 | break; | |
218 | case IMAGE: | |
219 | // TODO | |
220 | writer.write("<a href='" | |
221 | + StringUtils.xmlEscapeQuote(para.getContent()) + "'>" | |
222 | + StringUtils.xmlEscape(para.getContent()) + "</a>"); | |
223 | break; | |
224 | } | |
225 | } | |
226 | ||
227 | @Override | |
228 | protected void writeParagraphFooter(Paragraph para) throws IOException { | |
229 | switch (para.getType()) { | |
230 | case NORMAL: | |
231 | writer.write("</span>\n"); | |
232 | break; | |
233 | case QUOTE: | |
234 | writer.write("</div>\n"); | |
235 | break; | |
236 | default: | |
237 | writer.write("\n"); | |
238 | break; | |
239 | } | |
240 | } | |
241 | ||
242 | @Override | |
243 | protected void writeTextLine(ParagraphType type, String line) | |
244 | throws IOException { | |
245 | switch (type) { | |
246 | case QUOTE: | |
247 | case NORMAL: | |
248 | writer.write(decorateText(StringUtils.xmlEscape(line))); | |
249 | break; | |
250 | default: | |
251 | break; | |
252 | } | |
253 | } | |
254 | ||
255 | @Override | |
256 | protected String enbold(String word) { | |
257 | return "<strong>" + word + "</strong>"; | |
258 | } | |
259 | ||
260 | @Override | |
261 | protected String italize(String word) { | |
262 | return "<emph>" + word + "</emph>"; | |
263 | } | |
2206ef66 | 264 | } |