Fixes, new version number: 0.9.2
[nikiroo-utils.git] / src / be / nikiroo / fanfix / output / Text.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.output;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.net.URL;
7
8import be.nikiroo.fanfix.Instance;
9import be.nikiroo.fanfix.bundles.StringId;
10import be.nikiroo.fanfix.data.Chapter;
11import be.nikiroo.fanfix.data.MetaData;
12import be.nikiroo.fanfix.data.Paragraph;
13import be.nikiroo.fanfix.data.Story;
14import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
15
16class 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
42 public String getDefaultExtension() {
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()) {
62 writer.write("©" + author);
63 }
64 if (date != null && !date.isEmpty()) {
65 writer.write(" (");
66 writer.write(date);
67 writer.write(")");
68 }
69 writer.write("\n");
70
71 // resume:
72 if (meta != null && meta.getResume() != null) {
73 writeChapter(meta.getResume());
74 }
75 }
76
77 @Override
78 protected void writeChapterHeader(Chapter chap) throws IOException {
79 String txt;
80 if (chap.getName() != null && !chap.getName().isEmpty()) {
81 txt = Instance.getTrans().getString(StringId.CHAPTER_NAMED,
82 chap.getNumber(), chap.getName());
83 } else {
84 txt = Instance.getTrans().getString(StringId.CHAPTER_UNNAMED,
85 chap.getNumber());
86 }
87
88 writer.write("\n" + txt + "\n");
89 for (int i = 0; i < txt.length(); i++) {
90 writer.write("—");
91 }
92 writer.write("\n\n");
93 }
94
95 @Override
96 protected void writeParagraphFooter(Paragraph para) throws IOException {
97 writer.write("\n");
98 }
99
100 @Override
101 protected void writeParagraphHeader(Paragraph para) throws IOException {
102 if (para.getType() == ParagraphType.IMAGE) {
103 File file = new File(targetDir, getCurrentImageBestName(true));
104 Instance.getCache().saveAsImage(new URL(para.getContent()), file);
105 }
106 }
107
108 @Override
109 protected void writeTextLine(ParagraphType type, String line)
110 throws IOException {
111 switch (type) {
112 case BLANK:
113 break;
114 case BREAK:
115 writer.write("\n* * *\n");
116 break;
117 case NORMAL:
118 case QUOTE:
119 writer.write(line);
120 break;
121 case IMAGE:
122 writer.write("[" + getCurrentImageBestName(true) + "]");
123 break;
124 }
125 }
126}