doc
[nikiroo-utils.git] / output / Text.java
1 package be.nikiroo.fanfix.output;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.net.URL;
9
10 import be.nikiroo.fanfix.Instance;
11 import be.nikiroo.fanfix.bundles.StringId;
12 import be.nikiroo.fanfix.data.Chapter;
13 import be.nikiroo.fanfix.data.MetaData;
14 import be.nikiroo.fanfix.data.Paragraph;
15 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
16 import be.nikiroo.fanfix.data.Story;
17
18 class Text extends BasicOutput {
19 protected BufferedWriter writer;
20 protected File targetDir;
21
22 @Override
23 public File process(Story story, File targetDir, String targetName)
24 throws IOException {
25 String targetNameOrig = targetName;
26 targetName += getDefaultExtension(false);
27
28 this.targetDir = targetDir;
29
30 File target = new File(targetDir, targetName);
31
32 writer = new BufferedWriter(new OutputStreamWriter(
33 new FileOutputStream(target), "UTF-8"));
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
45 public String getDefaultExtension(boolean readerTarget) {
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()) {
65 writer.write(Instance.getTrans().getString(StringId.BY) + " "
66 + author);
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));
108 Instance.getCache().saveAsImage(new URL(para.getContent()), file);
109 }
110 }
111
112 @Override
113 protected void writeTextLine(ParagraphType type, String line)
114 throws IOException {
115 switch (type) {
116 case BLANK:
117 break;
118 case BREAK:
119 writer.write("\n* * *\n");
120 break;
121 case NORMAL:
122 case QUOTE:
123 writer.write(line);
124 break;
125 case IMAGE:
126 writer.write("[" + getCurrentImageBestName(true) + "]");
127 break;
128 }
129 }
130 }