Update nikiroo-utils (fix for e-hentai language)
[nikiroo-utils.git] / src / be / nikiroo / fanfix / output / Text.java
CommitLineData
08fe2e33
NR
1package be.nikiroo.fanfix.output;
2
3d247bc3 3import java.io.BufferedWriter;
08fe2e33 4import java.io.File;
3d247bc3 5import java.io.FileOutputStream;
08fe2e33 6import java.io.IOException;
3d247bc3 7import java.io.OutputStreamWriter;
08fe2e33
NR
8import java.net.URL;
9
10import be.nikiroo.fanfix.Instance;
11import be.nikiroo.fanfix.bundles.StringId;
12import be.nikiroo.fanfix.data.Chapter;
13import be.nikiroo.fanfix.data.MetaData;
14import be.nikiroo.fanfix.data.Paragraph;
08fe2e33 15import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
3d247bc3 16import be.nikiroo.fanfix.data.Story;
08fe2e33
NR
17
18class Text extends BasicOutput {
3d247bc3 19 protected BufferedWriter writer;
08fe2e33
NR
20 protected File targetDir;
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));
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}