Commit | Line | Data |
---|---|---|
08fe2e33 NR |
1 | package be.nikiroo.fanfix.output; |
2 | ||
3 | import java.io.File; | |
4 | import java.io.FileWriter; | |
5 | import java.io.IOException; | |
6 | import java.net.URL; | |
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.MetaData; | |
12 | import be.nikiroo.fanfix.data.Paragraph; | |
13 | import be.nikiroo.fanfix.data.Story; | |
14 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; | |
15 | ||
16 | class Text extends BasicOutput { | |
17 | protected FileWriter writer; | |
18 | protected File targetDir; | |
19 | ||
20 | @Override | |
21 | public File process(Story story, File targetDir, String targetName) | |
22 | throws IOException { | |
23 | String targetNameOrig = targetName; | |
24 | targetName += getDefaultExtension(); | |
25 | ||
26 | this.targetDir = targetDir; | |
27 | ||
28 | File target = new File(targetDir, targetName); | |
29 | ||
30 | writer = new FileWriter(target); | |
31 | try { | |
32 | super.process(story, targetDir, targetNameOrig); | |
33 | } finally { | |
34 | writer.close(); | |
35 | writer = null; | |
36 | } | |
37 | ||
38 | return target; | |
39 | } | |
40 | ||
41 | @Override | |
2206ef66 | 42 | public String getDefaultExtension() { |
08fe2e33 NR |
43 | return ".txt"; |
44 | } | |
45 | ||
46 | @Override | |
47 | protected void writeStoryHeader(Story story) throws IOException { | |
48 | String title = ""; | |
49 | String author = null; | |
50 | String date = null; | |
51 | ||
52 | MetaData meta = story.getMeta(); | |
53 | if (meta != null) { | |
54 | title = meta.getTitle() == null ? "" : meta.getTitle(); | |
55 | author = meta.getAuthor(); | |
56 | date = meta.getDate(); | |
57 | } | |
58 | ||
59 | writer.write(title); | |
60 | writer.write("\n"); | |
61 | if (author != null && !author.isEmpty()) { | |
a7d266e6 NR |
62 | writer.write(Instance.getTrans().getString(StringId.BY) + " " |
63 | + author); | |
08fe2e33 NR |
64 | } |
65 | if (date != null && !date.isEmpty()) { | |
66 | writer.write(" ("); | |
67 | writer.write(date); | |
68 | writer.write(")"); | |
69 | } | |
70 | writer.write("\n"); | |
71 | ||
72 | // resume: | |
73 | if (meta != null && meta.getResume() != null) { | |
74 | writeChapter(meta.getResume()); | |
75 | } | |
76 | } | |
77 | ||
78 | @Override | |
79 | protected void writeChapterHeader(Chapter chap) throws IOException { | |
80 | String txt; | |
81 | if (chap.getName() != null && !chap.getName().isEmpty()) { | |
82 | txt = Instance.getTrans().getString(StringId.CHAPTER_NAMED, | |
83 | chap.getNumber(), chap.getName()); | |
84 | } else { | |
85 | txt = Instance.getTrans().getString(StringId.CHAPTER_UNNAMED, | |
86 | chap.getNumber()); | |
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)); | |
105 | Instance.getCache().saveAsImage(new URL(para.getContent()), file); | |
106 | } | |
107 | } | |
108 | ||
109 | @Override | |
110 | protected void writeTextLine(ParagraphType type, String line) | |
111 | throws IOException { | |
112 | switch (type) { | |
113 | case BLANK: | |
114 | break; | |
115 | case BREAK: | |
116 | writer.write("\n* * *\n"); | |
117 | break; | |
118 | case NORMAL: | |
119 | case QUOTE: | |
120 | writer.write(line); | |
121 | break; | |
122 | case IMAGE: | |
123 | writer.write("[" + getCurrentImageBestName(true) + "]"); | |
124 | break; | |
125 | } | |
126 | } | |
127 | } |