| 1 | package be.nikiroo.fanfix.output; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.FileWriter; |
| 5 | import java.io.IOException; |
| 6 | import java.net.URL; |
| 7 | |
| 8 | import be.nikiroo.fanfix.Instance; |
| 9 | import be.nikiroo.fanfix.bundles.StringId; |
| 10 | import be.nikiroo.fanfix.data.Chapter; |
| 11 | import be.nikiroo.fanfix.data.MetaData; |
| 12 | import be.nikiroo.fanfix.data.Paragraph; |
| 13 | import be.nikiroo.fanfix.data.Story; |
| 14 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
| 15 | |
| 16 | class 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 | } |