| 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.InputStream; |
| 8 | import java.io.OutputStreamWriter; |
| 9 | |
| 10 | import javax.imageio.ImageIO; |
| 11 | |
| 12 | import be.nikiroo.fanfix.Instance; |
| 13 | import be.nikiroo.fanfix.bundles.Config; |
| 14 | import be.nikiroo.fanfix.data.Chapter; |
| 15 | import be.nikiroo.fanfix.data.MetaData; |
| 16 | import be.nikiroo.fanfix.data.Paragraph; |
| 17 | import be.nikiroo.fanfix.data.Paragraph.ParagraphType; |
| 18 | import be.nikiroo.fanfix.data.Story; |
| 19 | import be.nikiroo.utils.IOUtils; |
| 20 | import be.nikiroo.utils.StringUtils; |
| 21 | |
| 22 | class Html extends BasicOutput { |
| 23 | private File dir; |
| 24 | protected BufferedWriter writer; |
| 25 | private boolean inDialogue = false; |
| 26 | private boolean inNormal = false; |
| 27 | |
| 28 | @Override |
| 29 | public File process(Story story, File targetDir, String targetName) |
| 30 | throws IOException { |
| 31 | String targetNameOrig = targetName; |
| 32 | |
| 33 | File target = new File(targetDir, targetName); |
| 34 | target.mkdir(); |
| 35 | dir = target; |
| 36 | |
| 37 | // write a copy of the originals inside |
| 38 | InfoCover.writeInfo(dir, targetName, story.getMeta()); |
| 39 | InfoCover.writeCover(dir, targetName, story.getMeta()); |
| 40 | new InfoText().process(story, dir, targetNameOrig); |
| 41 | |
| 42 | target = new File(targetDir, targetName + getDefaultExtension(true)); |
| 43 | |
| 44 | writer = new BufferedWriter(new OutputStreamWriter( |
| 45 | new FileOutputStream(target), "UTF-8")); |
| 46 | try { |
| 47 | super.process(story, targetDir, targetNameOrig); |
| 48 | } finally { |
| 49 | writer.close(); |
| 50 | writer = null; |
| 51 | } |
| 52 | |
| 53 | String format = Instance.getConfig() |
| 54 | .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); |
| 55 | if (story.getMeta().getCover() != null) { |
| 56 | ImageIO.write(story.getMeta().getCover(), format, new File(dir, |
| 57 | "cover." + format)); |
| 58 | } |
| 59 | |
| 60 | return target; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String getDefaultExtension(boolean readerTarget) { |
| 65 | if (readerTarget) { |
| 66 | return File.separator + "index.html"; |
| 67 | } |
| 68 | |
| 69 | return ""; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void writeStoryHeader(Story story) throws IOException { |
| 74 | String title = ""; |
| 75 | String tags = ""; |
| 76 | String author = ""; |
| 77 | Chapter resume = null; |
| 78 | if (story.getMeta() != null) { |
| 79 | MetaData meta = story.getMeta(); |
| 80 | title = meta.getTitle(); |
| 81 | resume = meta.getResume(); |
| 82 | if (meta.getTags() != null) { |
| 83 | for (String tag : meta.getTags()) { |
| 84 | if (!tags.isEmpty()) { |
| 85 | tags += ", "; |
| 86 | } |
| 87 | tags += tag; |
| 88 | } |
| 89 | |
| 90 | if (!tags.isEmpty()) { |
| 91 | tags = "(" + tags + ")"; |
| 92 | } |
| 93 | } |
| 94 | author = meta.getAuthor(); |
| 95 | } |
| 96 | |
| 97 | String format = Instance.getConfig() |
| 98 | .getString(Config.IMAGE_FORMAT_COVER).toLowerCase(); |
| 99 | |
| 100 | InputStream inStyle = getClass().getResourceAsStream("html.style.css"); |
| 101 | if (inStyle == null) { |
| 102 | throw new IOException("Cannot find style.css resource"); |
| 103 | } |
| 104 | try { |
| 105 | IOUtils.write(inStyle, new File(dir, "style.css")); |
| 106 | } finally { |
| 107 | inStyle.close(); |
| 108 | } |
| 109 | |
| 110 | writer.write("<!DOCTYPE html>"); |
| 111 | writer.write("\n<html>"); |
| 112 | writer.write("\n<head>"); |
| 113 | writer.write("\n <meta http-equiv='content-type' content='text/html; charset=utf-8'>"); |
| 114 | writer.write("\n <meta name='viewport' content='width=device-width, initial-scale=1.0'>"); |
| 115 | writer.write("\n <link rel='stylesheet' type='text/css' href='style.css'>"); |
| 116 | writer.write("\n <title>" + StringUtils.xmlEscape(title) + "</title>"); |
| 117 | writer.write("\n</head>"); |
| 118 | writer.write("\n<body>\n"); |
| 119 | |
| 120 | writer.write("\n <div class=\"titlepage\">"); |
| 121 | writer.write("\n <h1>" + StringUtils.xmlEscape(title) + "</h1>"); |
| 122 | writer.write("\n <div class=\"type\">" + StringUtils.xmlEscape(tags) |
| 123 | + "</div>"); |
| 124 | writer.write("\n <div class=\"cover\">"); |
| 125 | writer.write("\n <img src=\"cover." + format + "\"></img>"); |
| 126 | writer.write("\n </div>"); |
| 127 | writer.write("\n <div class=\"author\">" |
| 128 | + StringUtils.xmlEscape(author) + "</div>"); |
| 129 | writer.write("\n </div>"); |
| 130 | |
| 131 | writer.write("\n <hr/><br/>"); |
| 132 | |
| 133 | if (resume != null) { |
| 134 | for (Paragraph para : resume) { |
| 135 | writeParagraph(para); |
| 136 | } |
| 137 | if (inDialogue) { |
| 138 | writer.write(" </div>\n"); |
| 139 | inDialogue = false; |
| 140 | } |
| 141 | if (inNormal) { |
| 142 | writer.write(" </div>\n"); |
| 143 | inNormal = false; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | writer.write("\n <br/>"); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | protected void writeStoryFooter(Story story) throws IOException { |
| 152 | writer.write("</body>\n"); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void writeChapterHeader(Chapter chap) throws IOException { |
| 157 | String nameOrNumber; |
| 158 | if (chap.getName() != null && !chap.getName().isEmpty()) { |
| 159 | nameOrNumber = chap.getName(); |
| 160 | } else { |
| 161 | nameOrNumber = Integer.toString(chap.getNumber()); |
| 162 | } |
| 163 | |
| 164 | writer.write("\n <h2>"); |
| 165 | writer.write("\n <span class='chap'>Chapter <span class='chapnumber'>" |
| 166 | + chap.getNumber() + "</span>:</span> "); |
| 167 | writer.write("\n <span class='chaptitle'>" |
| 168 | + StringUtils.xmlEscape(nameOrNumber) + "</span>"); |
| 169 | writer.write("\n </h2>"); |
| 170 | writer.write("\n "); |
| 171 | writer.write("\n <div class='chapter_content'>\n"); |
| 172 | |
| 173 | inDialogue = false; |
| 174 | inNormal = false; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | protected void writeChapterFooter(Chapter chap) throws IOException { |
| 179 | if (inDialogue) { |
| 180 | writer.write(" </div>\n"); |
| 181 | inDialogue = false; |
| 182 | } |
| 183 | if (inNormal) { |
| 184 | writer.write(" </div>\n"); |
| 185 | inNormal = false; |
| 186 | } |
| 187 | |
| 188 | writer.write("\n </div>"); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | protected void writeParagraphHeader(Paragraph para) throws IOException { |
| 193 | if (para.getType() == ParagraphType.QUOTE && !inDialogue) { |
| 194 | writer.write(" <div class='dialogues'>\n"); |
| 195 | inDialogue = true; |
| 196 | } else if (para.getType() != ParagraphType.QUOTE && inDialogue) { |
| 197 | writer.write(" </div>\n"); |
| 198 | inDialogue = false; |
| 199 | } |
| 200 | |
| 201 | if (para.getType() == ParagraphType.NORMAL && !inNormal) { |
| 202 | writer.write(" <div class='normals'>\n"); |
| 203 | inNormal = true; |
| 204 | } else if (para.getType() != ParagraphType.NORMAL && inNormal) { |
| 205 | writer.write(" </div>\n"); |
| 206 | inNormal = false; |
| 207 | } |
| 208 | |
| 209 | switch (para.getType()) { |
| 210 | case BLANK: |
| 211 | writer.write(" <div class='blank'></div>"); |
| 212 | break; |
| 213 | case BREAK: |
| 214 | writer.write(" <hr class='break'/>"); |
| 215 | break; |
| 216 | case NORMAL: |
| 217 | writer.write(" <span class='normal'>"); |
| 218 | break; |
| 219 | case QUOTE: |
| 220 | writer.write(" <div class='dialogue'>— "); |
| 221 | break; |
| 222 | case IMAGE: |
| 223 | // TODO |
| 224 | writer.write("<a href='" |
| 225 | + StringUtils.xmlEscapeQuote(para.getContent()) + "'>" |
| 226 | + StringUtils.xmlEscape(para.getContent()) + "</a>"); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | protected void writeParagraphFooter(Paragraph para) throws IOException { |
| 233 | switch (para.getType()) { |
| 234 | case NORMAL: |
| 235 | writer.write("</span>\n"); |
| 236 | break; |
| 237 | case QUOTE: |
| 238 | writer.write("</div>\n"); |
| 239 | break; |
| 240 | default: |
| 241 | writer.write("\n"); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | protected void writeTextLine(ParagraphType type, String line) |
| 248 | throws IOException { |
| 249 | switch (type) { |
| 250 | case QUOTE: |
| 251 | case NORMAL: |
| 252 | writer.write(decorateText(StringUtils.xmlEscape(line))); |
| 253 | break; |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | protected String enbold(String word) { |
| 261 | return "<strong>" + word + "</strong>"; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | protected String italize(String word) { |
| 266 | return "<emph>" + word + "</emph>"; |
| 267 | } |
| 268 | } |