| 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 | |
| 9 | import be.nikiroo.fanfix.Instance; |
| 10 | import be.nikiroo.fanfix.bundles.StringId; |
| 11 | import be.nikiroo.fanfix.data.Chapter; |
| 12 | import be.nikiroo.fanfix.data.MetaData; |
| 13 | import be.nikiroo.fanfix.data.Paragraph; |
| 14 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
| 15 | import be.nikiroo.fanfix.data.Story; |
| 16 | |
| 17 | class 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.getInstance().getTrans().getString(StringId.BY) + " " + author); |
| 66 | } |
| 67 | if (date != null && !date.isEmpty()) { |
| 68 | writer.write(" ("); |
| 69 | writer.write(date); |
| 70 | writer.write(")"); |
| 71 | } |
| 72 | writer.write("\n"); |
| 73 | |
| 74 | // resume: |
| 75 | if (meta != null && meta.getResume() != null) { |
| 76 | writeChapter(meta.getResume()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | protected void writeChapterHeader(Chapter chap) throws IOException { |
| 82 | String txt; |
| 83 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
| 84 | txt = Instance.getInstance().getTrans().getString(StringId.CHAPTER_NAMED, chap.getNumber(), chap.getName()); |
| 85 | } else { |
| 86 | txt = Instance.getInstance().getTrans().getString(StringId.CHAPTER_UNNAMED, 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 | try { |
| 106 | Instance.getInstance().getCache().saveAsImage(para.getContentImage(), file, nextParaIsCover); |
| 107 | } catch (IOException e) { |
| 108 | Instance.getInstance().getTraceHandler().error(new IOException("Cannot save an image", e)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | nextParaIsCover = false; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void writeTextLine(ParagraphType type, String line) |
| 117 | throws IOException { |
| 118 | switch (type) { |
| 119 | case BLANK: |
| 120 | break; |
| 121 | case BREAK: |
| 122 | writer.write("\n* * *\n"); |
| 123 | break; |
| 124 | case NORMAL: |
| 125 | case QUOTE: |
| 126 | writer.write(line); |
| 127 | break; |
| 128 | case IMAGE: |
| 129 | writer.write("[" + getCurrentImageBestName(true) + "]"); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |