Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.output; |
2 | ||
3d247bc3 | 3 | import java.io.BufferedWriter; |
08fe2e33 | 4 | import java.io.File; |
3d247bc3 | 5 | import java.io.FileOutputStream; |
08fe2e33 | 6 | import java.io.IOException; |
3d247bc3 | 7 | import java.io.OutputStreamWriter; |
08fe2e33 NR |
8 | |
9 | import be.nikiroo.fanfix.Instance; | |
10 | import be.nikiroo.fanfix.bundles.StringId; | |
11 | import be.nikiroo.fanfix.data.Chapter; | |
12 | import be.nikiroo.fanfix.data.MetaData; | |
13 | import be.nikiroo.fanfix.data.Paragraph; | |
08fe2e33 | 14 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
3d247bc3 | 15 | import be.nikiroo.fanfix.data.Story; |
08fe2e33 NR |
16 | |
17 | class Text extends BasicOutput { | |
3d247bc3 | 18 | protected BufferedWriter writer; |
08fe2e33 | 19 | protected File targetDir; |
16a81ef7 | 20 | private boolean nextParaIsCover = true; |
08fe2e33 NR |
21 | |
22 | @Override | |
23 | public File process(Story story, File targetDir, String targetName) | |
24 | throws IOException { | |
25 | String targetNameOrig = targetName; | |
10d558d2 | 26 | targetName += getDefaultExtension(false); |
08fe2e33 NR |
27 | |
28 | this.targetDir = targetDir; | |
29 | ||
30 | File target = new File(targetDir, targetName); | |
31 | ||
3d247bc3 NR |
32 | writer = new BufferedWriter(new OutputStreamWriter( |
33 | new FileOutputStream(target), "UTF-8")); | |
08fe2e33 NR |
34 | try { |
35 | super.process(story, targetDir, targetNameOrig); | |
36 | } finally { | |
37 | writer.close(); | |
38 | writer = null; | |
39 | } | |
40 | ||
41 | return target; | |
42 | } | |
43 | ||
44 | @Override | |
10d558d2 | 45 | public String getDefaultExtension(boolean readerTarget) { |
08fe2e33 NR |
46 | return ".txt"; |
47 | } | |
48 | ||
49 | @Override | |
50 | protected void writeStoryHeader(Story story) throws IOException { | |
51 | String title = ""; | |
52 | String author = null; | |
53 | String date = null; | |
54 | ||
55 | MetaData meta = story.getMeta(); | |
56 | if (meta != null) { | |
57 | title = meta.getTitle() == null ? "" : meta.getTitle(); | |
58 | author = meta.getAuthor(); | |
59 | date = meta.getDate(); | |
60 | } | |
61 | ||
62 | writer.write(title); | |
63 | writer.write("\n"); | |
64 | if (author != null && !author.isEmpty()) { | |
d66deb8d | 65 | writer.write(Instance.getInstance().getTrans().getString(StringId.BY) + " " + author); |
08fe2e33 NR |
66 | } |
67 | if (date != null && !date.isEmpty()) { | |
68 | writer.write(" ("); | |
69 | writer.write(date); | |
70 | writer.write(")"); | |
71 | } | |
72 | writer.write("\n"); | |
73 | ||
74 | // resume: | |
75 | if (meta != null && meta.getResume() != null) { | |
76 | writeChapter(meta.getResume()); | |
77 | } | |
78 | } | |
79 | ||
80 | @Override | |
81 | protected void writeChapterHeader(Chapter chap) throws IOException { | |
82 | String txt; | |
83 | if (chap.getName() != null && !chap.getName().isEmpty()) { | |
d66deb8d | 84 | txt = Instance.getInstance().getTrans().getString(StringId.CHAPTER_NAMED, chap.getNumber(), chap.getName()); |
08fe2e33 | 85 | } else { |
d66deb8d | 86 | txt = Instance.getInstance().getTrans().getString(StringId.CHAPTER_UNNAMED, chap.getNumber()); |
08fe2e33 NR |
87 | } |
88 | ||
89 | writer.write("\n" + txt + "\n"); | |
90 | for (int i = 0; i < txt.length(); i++) { | |
91 | writer.write("—"); | |
92 | } | |
93 | writer.write("\n\n"); | |
94 | } | |
95 | ||
96 | @Override | |
97 | protected void writeParagraphFooter(Paragraph para) throws IOException { | |
98 | writer.write("\n"); | |
99 | } | |
100 | ||
101 | @Override | |
102 | protected void writeParagraphHeader(Paragraph para) throws IOException { | |
103 | if (para.getType() == ParagraphType.IMAGE) { | |
104 | File file = new File(targetDir, getCurrentImageBestName(true)); | |
77fea1c0 | 105 | try { |
d66deb8d | 106 | Instance.getInstance().getCache().saveAsImage(para.getContentImage(), file, nextParaIsCover); |
77fea1c0 | 107 | } catch (IOException e) { |
d66deb8d | 108 | Instance.getInstance().getTraceHandler().error(new IOException("Cannot save an image", e)); |
77fea1c0 | 109 | } |
08fe2e33 | 110 | } |
16a81ef7 NR |
111 | |
112 | nextParaIsCover = false; | |
08fe2e33 NR |
113 | } |
114 | ||
115 | @Override | |
116 | protected void writeTextLine(ParagraphType type, String line) | |
117 | throws IOException { | |
118 | switch (type) { | |
119 | case BLANK: | |
120 | break; | |
121 | case BREAK: | |
122 | writer.write("\n* * *\n"); | |
123 | break; | |
124 | case NORMAL: | |
125 | case QUOTE: | |
126 | writer.write(line); | |
127 | break; | |
128 | case IMAGE: | |
129 | writer.write("[" + getCurrentImageBestName(true) + "]"); | |
130 | break; | |
131 | } | |
132 | } | |
133 | } |