Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.output; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
bee7dffe NR |
5 | import java.util.ArrayList; |
6 | import java.util.List; | |
08fe2e33 NR |
7 | |
8 | import be.nikiroo.fanfix.Instance; | |
9 | import be.nikiroo.fanfix.bundles.StringId; | |
10 | import be.nikiroo.fanfix.data.Chapter; | |
11 | import be.nikiroo.fanfix.data.Paragraph; | |
08fe2e33 | 12 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
bee7dffe | 13 | import be.nikiroo.fanfix.data.Story; |
3b2b638f | 14 | import be.nikiroo.utils.Progress; |
08fe2e33 NR |
15 | |
16 | /** | |
17 | * This class is the base class used by the other output classes. It can be used | |
18 | * outside of this package, and have static method that you can use to get | |
19 | * access to the correct support class. | |
20 | * | |
21 | * @author niki | |
22 | */ | |
23 | public abstract class BasicOutput { | |
24 | /** | |
25 | * The supported output types for which we can get a {@link BasicOutput} | |
26 | * object. | |
27 | * | |
28 | * @author niki | |
29 | */ | |
30 | public enum OutputType { | |
31 | /** EPUB files created with this program */ | |
32 | EPUB, | |
33 | /** Pure text file with some rules */ | |
34 | TEXT, | |
35 | /** TEXT but with associated .info file */ | |
36 | INFO_TEXT, | |
37 | /** DEBUG output to console */ | |
38 | SYSOUT, | |
39 | /** ZIP with (PNG) images */ | |
40 | CBZ, | |
41 | /** LaTeX file with "book" template */ | |
2206ef66 NR |
42 | LATEX, |
43 | /** HTML files in a dedicated directory */ | |
44 | HTML, | |
45 | ||
46 | ; | |
47 | ||
211f7ddb | 48 | @Override |
08fe2e33 NR |
49 | public String toString() { |
50 | return super.toString().toLowerCase(); | |
51 | } | |
52 | ||
53 | /** | |
54 | * A description of this output type. | |
55 | * | |
4d205683 NR |
56 | * @param longDesc |
57 | * TRUE for the long description, FALSE for the short one | |
58 | * | |
08fe2e33 NR |
59 | * @return the description |
60 | */ | |
4d205683 NR |
61 | public String getDesc(boolean longDesc) { |
62 | StringId id = longDesc ? StringId.OUTPUT_DESC | |
63 | : StringId.OUTPUT_DESC_SHORT; | |
64 | ||
65 | String desc = Instance.getTrans().getStringX(id, this.name()); | |
08fe2e33 NR |
66 | |
67 | if (desc == null) { | |
4d205683 | 68 | desc = Instance.getTrans().getString(id, this); |
08fe2e33 NR |
69 | } |
70 | ||
b2612f9d NR |
71 | if (desc == null) { |
72 | desc = this.toString(); | |
73 | } | |
74 | ||
08fe2e33 NR |
75 | return desc; |
76 | } | |
77 | ||
2206ef66 NR |
78 | /** |
79 | * The default extension to add to the output files. | |
80 | * | |
10d558d2 NR |
81 | * @param readerTarget |
82 | * the target to point to to read the {@link Story} (for | |
83 | * instance, the main entry point if this {@link Story} is in | |
84 | * a directory bundle) | |
85 | * | |
2206ef66 NR |
86 | * @return the extension |
87 | */ | |
10d558d2 | 88 | public String getDefaultExtension(boolean readerTarget) { |
925298fd | 89 | BasicOutput output = BasicOutput.getOutput(this, false, false); |
2206ef66 | 90 | if (output != null) { |
10d558d2 | 91 | return output.getDefaultExtension(readerTarget); |
2206ef66 NR |
92 | } |
93 | ||
94 | return null; | |
95 | } | |
96 | ||
08fe2e33 | 97 | /** |
0efd25e3 NR |
98 | * Call {@link OutputType#valueOf(String)} after conversion to upper |
99 | * case. | |
08fe2e33 NR |
100 | * |
101 | * @param typeName | |
102 | * the possible type name | |
103 | * | |
104 | * @return NULL or the type | |
105 | */ | |
106 | public static OutputType valueOfUC(String typeName) { | |
107 | return OutputType.valueOf(typeName == null ? null : typeName | |
108 | .toUpperCase()); | |
109 | } | |
110 | ||
111 | /** | |
0efd25e3 | 112 | * Call {@link OutputType#valueOf(String)} after conversion to upper |
e604986c | 113 | * case but return def for NULL and empty instead of raising an |
0efd25e3 | 114 | * exception. |
08fe2e33 NR |
115 | * |
116 | * @param typeName | |
117 | * the possible type name | |
e604986c NR |
118 | * @param def |
119 | * the default value | |
08fe2e33 NR |
120 | * |
121 | * @return NULL or the type | |
122 | */ | |
e604986c | 123 | public static OutputType valueOfNullOkUC(String typeName, OutputType def) { |
a6395bef | 124 | if (typeName == null || typeName.isEmpty()) { |
e604986c | 125 | return def; |
08fe2e33 NR |
126 | } |
127 | ||
128 | return OutputType.valueOfUC(typeName); | |
129 | } | |
130 | ||
131 | /** | |
0efd25e3 | 132 | * Call {@link OutputType#valueOf(String)} after conversion to upper |
e604986c | 133 | * case but return def in case of error instead of raising an exception. |
08fe2e33 NR |
134 | * |
135 | * @param typeName | |
136 | * the possible type name | |
e604986c NR |
137 | * @param def |
138 | * the default value | |
08fe2e33 NR |
139 | * |
140 | * @return NULL or the type | |
141 | */ | |
e604986c | 142 | public static OutputType valueOfAllOkUC(String typeName, OutputType def) { |
08fe2e33 NR |
143 | try { |
144 | return OutputType.valueOfUC(typeName); | |
145 | } catch (Exception e) { | |
e604986c | 146 | return def; |
08fe2e33 NR |
147 | } |
148 | } | |
149 | } | |
150 | ||
151 | /** The creator name (this program, by me!) */ | |
152 | static final String EPUB_CREATOR = "Fanfix (by Niki)"; | |
153 | ||
154 | /** The current best name for an image */ | |
155 | private String imageName; | |
156 | private File targetDir; | |
157 | private String targetName; | |
158 | private OutputType type; | |
159 | private boolean writeCover; | |
160 | private boolean writeInfo; | |
bee7dffe NR |
161 | private Progress storyPg; |
162 | private Progress chapPg; | |
08fe2e33 NR |
163 | |
164 | /** | |
165 | * Process the {@link Story} into the given target. | |
166 | * | |
167 | * @param story | |
168 | * the {@link Story} to export | |
169 | * @param target | |
170 | * the target where to save to (will not necessary be taken as is | |
171 | * by the processor, for instance an extension can be added) | |
bee7dffe NR |
172 | * @param pg |
173 | * the optional progress reporter | |
08fe2e33 NR |
174 | * |
175 | * @return the actual main target saved, which can be slightly different | |
176 | * that the input one | |
177 | * | |
178 | * @throws IOException | |
179 | * in case of I/O error | |
180 | */ | |
bee7dffe NR |
181 | public File process(Story story, String target, Progress pg) |
182 | throws IOException { | |
183 | storyPg = pg; | |
184 | ||
d5a7153c NR |
185 | File targetDir = null; |
186 | String targetName = null; | |
187 | if (target != null) { | |
188 | target = new File(target).getAbsolutePath(); | |
189 | targetDir = new File(target).getParentFile(); | |
190 | targetName = new File(target).getName(); | |
191 | ||
192 | String ext = getDefaultExtension(false); | |
193 | if (ext != null && !ext.isEmpty()) { | |
194 | if (targetName.toLowerCase().endsWith(ext)) { | |
195 | targetName = targetName.substring(0, targetName.length() | |
196 | - ext.length()); | |
197 | } | |
08fe2e33 NR |
198 | } |
199 | } | |
200 | ||
201 | return process(story, targetDir, targetName); | |
202 | } | |
203 | ||
204 | /** | |
205 | * Process the {@link Story} into the given target. | |
206 | * <p> | |
207 | * This method is expected to be overridden in most cases. | |
208 | * | |
209 | * @param story | |
210 | * the {@link Story} to export | |
211 | * @param targetDir | |
212 | * the target dir where to save to | |
213 | * @param targetName | |
214 | * the target filename (will not necessary be taken as is by the | |
215 | * processor, for instance an extension can be added) | |
216 | * | |
bee7dffe | 217 | * |
08fe2e33 NR |
218 | * @return the actual main target saved, which can be slightly different |
219 | * that the input one | |
220 | * | |
221 | * @throws IOException | |
222 | * in case of I/O error | |
223 | */ | |
224 | protected File process(Story story, File targetDir, String targetName) | |
225 | throws IOException { | |
226 | this.targetDir = targetDir; | |
227 | this.targetName = targetName; | |
228 | ||
229 | writeStory(story); | |
230 | ||
231 | return null; | |
232 | } | |
233 | ||
234 | /** | |
235 | * The output type. | |
236 | * | |
237 | * @return the type | |
238 | */ | |
239 | public OutputType getType() { | |
240 | return type; | |
241 | } | |
242 | ||
5083ef84 NR |
243 | /** |
244 | * Enable the creation of a .info file next to the resulting processed file. | |
245 | * | |
246 | * @return TRUE to enable it | |
247 | */ | |
248 | protected boolean isWriteInfo() { | |
249 | return writeInfo; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Enable the creation of a cover file next to the resulting processed file | |
254 | * if possible. | |
255 | * | |
256 | * @return TRUE to enable it | |
257 | */ | |
258 | protected boolean isWriteCover() { | |
259 | return writeCover; | |
260 | } | |
261 | ||
08fe2e33 NR |
262 | /** |
263 | * The output type. | |
264 | * | |
265 | * @param type | |
266 | * the new type | |
0efd25e3 NR |
267 | * @param writeCover |
268 | * TRUE to enable the creation of a cover if possible | |
925298fd NR |
269 | * @param writeInfo |
270 | * TRUE to enable the creation of a .info file | |
08fe2e33 NR |
271 | * |
272 | * @return this | |
273 | */ | |
925298fd NR |
274 | protected BasicOutput setType(OutputType type, boolean writeInfo, |
275 | boolean writeCover) { | |
08fe2e33 | 276 | this.type = type; |
08fe2e33 | 277 | this.writeInfo = writeInfo; |
925298fd | 278 | this.writeCover = writeCover; |
08fe2e33 NR |
279 | |
280 | return this; | |
281 | } | |
282 | ||
283 | /** | |
284 | * The default extension to add to the output files. | |
08fe2e33 | 285 | * |
10d558d2 NR |
286 | * @param readerTarget |
287 | * the target to point to to read the {@link Story} (for | |
288 | * instance, the main entry point if this {@link Story} is in a | |
289 | * directory bundle) | |
290 | * | |
08fe2e33 NR |
291 | * @return the extension |
292 | */ | |
211f7ddb NR |
293 | public String getDefaultExtension( |
294 | @SuppressWarnings("unused") boolean readerTarget) { | |
08fe2e33 NR |
295 | return ""; |
296 | } | |
297 | ||
211f7ddb | 298 | @SuppressWarnings("unused") |
08fe2e33 NR |
299 | protected void writeStoryHeader(Story story) throws IOException { |
300 | } | |
301 | ||
211f7ddb | 302 | @SuppressWarnings("unused") |
08fe2e33 NR |
303 | protected void writeChapterHeader(Chapter chap) throws IOException { |
304 | } | |
305 | ||
211f7ddb | 306 | @SuppressWarnings("unused") |
08fe2e33 NR |
307 | protected void writeParagraphHeader(Paragraph para) throws IOException { |
308 | } | |
309 | ||
211f7ddb | 310 | @SuppressWarnings("unused") |
08fe2e33 NR |
311 | protected void writeStoryFooter(Story story) throws IOException { |
312 | } | |
313 | ||
211f7ddb | 314 | @SuppressWarnings("unused") |
08fe2e33 NR |
315 | protected void writeChapterFooter(Chapter chap) throws IOException { |
316 | } | |
317 | ||
211f7ddb | 318 | @SuppressWarnings("unused") |
08fe2e33 NR |
319 | protected void writeParagraphFooter(Paragraph para) throws IOException { |
320 | } | |
321 | ||
322 | protected void writeStory(Story story) throws IOException { | |
bee7dffe NR |
323 | if (storyPg == null) { |
324 | storyPg = new Progress(0, story.getChapters().size() + 2); | |
325 | } else { | |
326 | storyPg.setMinMax(0, story.getChapters().size() + 2); | |
327 | } | |
328 | ||
08fe2e33 NR |
329 | String chapterNameNum = String.format("%03d", 0); |
330 | String paragraphNumber = String.format("%04d", 0); | |
331 | imageName = paragraphNumber + "_" + chapterNameNum + ".png"; | |
332 | ||
fe999aa4 | 333 | if (story.getMeta() != null) { |
68686a37 | 334 | story.getMeta().setType("" + getType()); |
fe999aa4 | 335 | } |
68686a37 | 336 | |
08fe2e33 NR |
337 | if (writeCover) { |
338 | InfoCover.writeCover(targetDir, targetName, story.getMeta()); | |
339 | } | |
340 | if (writeInfo) { | |
341 | InfoCover.writeInfo(targetDir, targetName, story.getMeta()); | |
342 | } | |
343 | ||
bee7dffe NR |
344 | storyPg.setProgress(1); |
345 | ||
346 | List<Progress> chapPgs = new ArrayList<Progress>(story.getChapters() | |
347 | .size()); | |
08fe2e33 | 348 | for (Chapter chap : story) { |
bee7dffe NR |
349 | chapPg = new Progress(0, chap.getParagraphs().size()); |
350 | storyPg.addProgress(chapPg, 1); | |
351 | chapPgs.add(chapPg); | |
352 | chapPg = null; | |
353 | } | |
354 | ||
355 | writeStoryHeader(story); | |
356 | for (int i = 0; i < story.getChapters().size(); i++) { | |
357 | chapPg = chapPgs.get(i); | |
358 | writeChapter(story.getChapters().get(i)); | |
359 | chapPg.setProgress(chapPg.getMax()); | |
360 | chapPg = null; | |
08fe2e33 NR |
361 | } |
362 | writeStoryFooter(story); | |
bee7dffe NR |
363 | |
364 | storyPg.setProgress(storyPg.getMax()); | |
365 | storyPg = null; | |
08fe2e33 NR |
366 | } |
367 | ||
368 | protected void writeChapter(Chapter chap) throws IOException { | |
369 | String chapterNameNum; | |
370 | if (chap.getName() == null || chap.getName().isEmpty()) { | |
371 | chapterNameNum = String.format("%03d", chap.getNumber()); | |
372 | } else { | |
373 | chapterNameNum = String.format("%03d", chap.getNumber()) + "_" | |
374 | + chap.getName().replace(" ", "_"); | |
375 | } | |
376 | ||
377 | int num = 0; | |
378 | String paragraphNumber = String.format("%04d", num++); | |
16a81ef7 | 379 | imageName = chapterNameNum + "_" + paragraphNumber; |
08fe2e33 NR |
380 | |
381 | writeChapterHeader(chap); | |
bee7dffe | 382 | int i = 1; |
08fe2e33 NR |
383 | for (Paragraph para : chap) { |
384 | paragraphNumber = String.format("%04d", num++); | |
16a81ef7 | 385 | imageName = chapterNameNum + "_" + paragraphNumber; |
08fe2e33 | 386 | writeParagraph(para); |
bee7dffe NR |
387 | if (chapPg != null) { |
388 | chapPg.setProgress(i++); | |
389 | } | |
08fe2e33 NR |
390 | } |
391 | writeChapterFooter(chap); | |
392 | } | |
393 | ||
394 | protected void writeParagraph(Paragraph para) throws IOException { | |
395 | writeParagraphHeader(para); | |
396 | writeTextLine(para.getType(), para.getContent()); | |
397 | writeParagraphFooter(para); | |
398 | } | |
399 | ||
211f7ddb | 400 | @SuppressWarnings("unused") |
08fe2e33 NR |
401 | protected void writeTextLine(ParagraphType type, String line) |
402 | throws IOException { | |
403 | } | |
404 | ||
405 | /** | |
406 | * Return the current best guess for an image name, based upon the current | |
407 | * {@link Chapter} and {@link Paragraph}. | |
408 | * | |
409 | * @param prefix | |
410 | * add the original target name as a prefix | |
411 | * | |
412 | * @return the guessed name | |
413 | */ | |
414 | protected String getCurrentImageBestName(boolean prefix) { | |
415 | if (prefix) { | |
416 | return targetName + "_" + imageName; | |
417 | } | |
418 | ||
419 | return imageName; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Return the given word or sentence as <b>bold</b>. | |
424 | * | |
425 | * @param word | |
426 | * the input | |
427 | * | |
428 | * @return the bold output | |
429 | */ | |
430 | protected String enbold(String word) { | |
431 | return word; | |
432 | } | |
433 | ||
434 | /** | |
435 | * Return the given word or sentence as <i>italic</i>. | |
436 | * | |
437 | * @param word | |
438 | * the input | |
439 | * | |
440 | * @return the italic output | |
441 | */ | |
442 | protected String italize(String word) { | |
443 | return word; | |
444 | } | |
445 | ||
446 | /** | |
447 | * Decorate the given text with <b>bold</b> and <i>italic</i> words, | |
448 | * according to {@link BasicOutput#enbold(String)} and | |
449 | * {@link BasicOutput#italize(String)}. | |
450 | * | |
451 | * @param text | |
452 | * the input | |
453 | * | |
454 | * @return the decorated output | |
455 | */ | |
456 | protected String decorateText(String text) { | |
457 | StringBuilder builder = new StringBuilder(); | |
458 | ||
459 | int bold = -1; | |
460 | int italic = -1; | |
461 | char prev = '\0'; | |
462 | for (char car : text.toCharArray()) { | |
463 | switch (car) { | |
464 | case '*': | |
465 | if (bold >= 0 && prev != ' ') { | |
466 | String data = builder.substring(bold); | |
467 | builder.setLength(bold); | |
468 | builder.append(enbold(data)); | |
469 | bold = -1; | |
470 | } else if (bold < 0 | |
471 | && (prev == ' ' || prev == '\0' || prev == '\n')) { | |
472 | bold = builder.length(); | |
473 | } else { | |
474 | builder.append(car); | |
475 | } | |
476 | ||
477 | break; | |
478 | case '_': | |
479 | if (italic >= 0 && prev != ' ') { | |
480 | String data = builder.substring(italic); | |
481 | builder.setLength(italic); | |
482 | builder.append(enbold(data)); | |
483 | italic = -1; | |
484 | } else if (italic < 0 | |
485 | && (prev == ' ' || prev == '\0' || prev == '\n')) { | |
486 | italic = builder.length(); | |
487 | } else { | |
488 | builder.append(car); | |
489 | } | |
490 | ||
491 | break; | |
492 | default: | |
493 | builder.append(car); | |
494 | break; | |
495 | } | |
496 | ||
497 | prev = car; | |
498 | } | |
499 | ||
500 | if (bold >= 0) { | |
501 | builder.insert(bold, '*'); | |
502 | } | |
503 | ||
504 | if (italic >= 0) { | |
505 | builder.insert(italic, '_'); | |
506 | } | |
507 | ||
508 | return builder.toString(); | |
509 | } | |
510 | ||
511 | /** | |
512 | * Return a {@link BasicOutput} object compatible with the given | |
513 | * {@link OutputType}. | |
514 | * | |
515 | * @param type | |
516 | * the type | |
925298fd NR |
517 | * @param writeCover |
518 | * TRUE to enable the creation of a cover if possible to be saved | |
519 | * next to the main target file | |
520 | * @param writeInfo | |
521 | * TRUE to enable the creation of a .info file to be saved next | |
08fe2e33 NR |
522 | * to the main target file |
523 | * | |
524 | * @return the {@link BasicOutput} | |
525 | */ | |
925298fd NR |
526 | public static BasicOutput getOutput(OutputType type, boolean writeInfo, |
527 | boolean writeCover) { | |
08fe2e33 NR |
528 | if (type != null) { |
529 | switch (type) { | |
530 | case EPUB: | |
925298fd | 531 | return new Epub().setType(type, writeInfo, writeCover); |
08fe2e33 | 532 | case TEXT: |
925298fd | 533 | return new Text().setType(type, writeInfo, true); |
08fe2e33 NR |
534 | case INFO_TEXT: |
535 | return new InfoText().setType(type, true, true); | |
536 | case SYSOUT: | |
537 | return new Sysout().setType(type, false, false); | |
538 | case CBZ: | |
925298fd | 539 | return new Cbz().setType(type, writeInfo, writeCover); |
08fe2e33 | 540 | case LATEX: |
925298fd | 541 | return new LaTeX().setType(type, writeInfo, writeCover); |
2206ef66 | 542 | case HTML: |
925298fd | 543 | return new Html().setType(type, writeInfo, writeCover); |
08fe2e33 NR |
544 | } |
545 | } | |
546 | ||
547 | return null; | |
548 | } | |
549 | } |