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()) { | |
a7d266e6 NR |
65 | writer.write(Instance.getTrans().getString(StringId.BY) + " " |
66 | + author); | |
08fe2e33 NR |
67 | } |
68 | if (date != null && !date.isEmpty()) { | |
69 | writer.write(" ("); | |
70 | writer.write(date); | |
71 | writer.write(")"); | |
72 | } | |
73 | writer.write("\n"); | |
74 | ||
75 | // resume: | |
76 | if (meta != null && meta.getResume() != null) { | |
77 | writeChapter(meta.getResume()); | |
78 | } | |
79 | } | |
80 | ||
81 | @Override | |
82 | protected void writeChapterHeader(Chapter chap) throws IOException { | |
83 | String txt; | |
84 | if (chap.getName() != null && !chap.getName().isEmpty()) { | |
85 | txt = Instance.getTrans().getString(StringId.CHAPTER_NAMED, | |
86 | chap.getNumber(), chap.getName()); | |
87 | } else { | |
88 | txt = Instance.getTrans().getString(StringId.CHAPTER_UNNAMED, | |
89 | chap.getNumber()); | |
90 | } | |
91 | ||
92 | writer.write("\n" + txt + "\n"); | |
93 | for (int i = 0; i < txt.length(); i++) { | |
94 | writer.write("—"); | |
95 | } | |
96 | writer.write("\n\n"); | |
97 | } | |
98 | ||
99 | @Override | |
100 | protected void writeParagraphFooter(Paragraph para) throws IOException { | |
101 | writer.write("\n"); | |
102 | } | |
103 | ||
104 | @Override | |
105 | protected void writeParagraphHeader(Paragraph para) throws IOException { | |
106 | if (para.getType() == ParagraphType.IMAGE) { | |
107 | File file = new File(targetDir, getCurrentImageBestName(true)); | |
77fea1c0 NR |
108 | try { |
109 | Instance.getCache().saveAsImage(para.getContentImage(), file, | |
110 | nextParaIsCover); | |
111 | } catch (IOException e) { | |
112 | Instance.getTraceHandler().error( | |
113 | new IOException("Cannot save an image", e)); | |
114 | } | |
08fe2e33 | 115 | } |
16a81ef7 NR |
116 | |
117 | nextParaIsCover = false; | |
08fe2e33 NR |
118 | } |
119 | ||
120 | @Override | |
121 | protected void writeTextLine(ParagraphType type, String line) | |
122 | throws IOException { | |
123 | switch (type) { | |
124 | case BLANK: | |
125 | break; | |
126 | case BREAK: | |
127 | writer.write("\n* * *\n"); | |
128 | break; | |
129 | case NORMAL: | |
130 | case QUOTE: | |
131 | writer.write(line); | |
132 | break; | |
133 | case IMAGE: | |
134 | writer.write("[" + getCurrentImageBestName(true) + "]"); | |
135 | break; | |
136 | } | |
137 | } | |
138 | } |