gui: change name, author
[fanfix.git] / src / be / nikiroo / fanfix / output / Text.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.output;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStreamWriter;
8
9import be.nikiroo.fanfix.Instance;
10import be.nikiroo.fanfix.bundles.StringId;
11import be.nikiroo.fanfix.data.Chapter;
12import be.nikiroo.fanfix.data.MetaData;
13import be.nikiroo.fanfix.data.Paragraph;
14import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
15import be.nikiroo.fanfix.data.Story;
16
17class Text extends BasicOutput {
18 protected BufferedWriter writer;
19 protected File targetDir;
20 private boolean nextParaIsCover = true;
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 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 }
115 }
116
117 nextParaIsCover = false;
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}