1 package be
.nikiroo
.fanfix
.output
;
3 import java
.io
.BufferedWriter
;
5 import java
.io
.FileInputStream
;
6 import java
.io
.FileOutputStream
;
7 import java
.io
.IOException
;
8 import java
.io
.InputStream
;
9 import java
.io
.OutputStream
;
10 import java
.io
.OutputStreamWriter
;
11 import java
.util
.zip
.ZipEntry
;
12 import java
.util
.zip
.ZipOutputStream
;
14 import be
.nikiroo
.fanfix
.Instance
;
15 import be
.nikiroo
.fanfix
.bundles
.Config
;
16 import be
.nikiroo
.fanfix
.bundles
.StringId
;
17 import be
.nikiroo
.fanfix
.data
.Chapter
;
18 import be
.nikiroo
.fanfix
.data
.MetaData
;
19 import be
.nikiroo
.fanfix
.data
.Paragraph
;
20 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
21 import be
.nikiroo
.fanfix
.data
.Story
;
22 import be
.nikiroo
.utils
.IOUtils
;
23 import be
.nikiroo
.utils
.StringUtils
;
25 class Epub
extends BasicOutput
{
27 private BufferedWriter writer
;
28 private boolean inDialogue
= false;
29 private boolean inNormal
= false;
31 private boolean nextParaIsCover
= true;
34 public File
process(Story story
, File targetDir
, String targetName
)
36 String targetNameOrig
= targetName
;
37 targetName
+= getDefaultExtension(false);
39 tmpDir
= Instance
.getTempFiles().createTempDir("fanfic-reader-epub");
42 if (!tmpDir
.mkdir()) {
43 throw new IOException(
44 "Cannot create a temporary directory: no space left on device?");
47 super.process(story
, targetDir
, targetNameOrig
);
52 File data
= new File(tmpDir
, "DATA");
54 BasicOutput
.getOutput(OutputType
.TEXT
, isWriteInfo(),
55 isWriteCover()).process(story
, data
, targetNameOrig
);
56 InfoCover
.writeInfo(data
, targetNameOrig
, story
.getMeta());
57 IOUtils
.writeSmallFile(data
, "version", "3.0");
60 epub
= new File(targetDir
, targetName
);
61 IOUtils
.zip(tmpDir
, epub
, true);
63 OutputStream out
= new FileOutputStream(epub
);
65 ZipOutputStream zip
= new ZipOutputStream(out
);
67 // "mimetype" MUST be the first element and not compressed
68 zip
.setLevel(ZipOutputStream
.STORED
);
69 File mimetype
= new File(tmpDir
, "mimetype");
70 IOUtils
.writeSmallFile(tmpDir
, "mimetype",
71 "application/epub+zip");
72 ZipEntry entry
= new ZipEntry("mimetype");
73 entry
.setExtra(new byte[] {});
74 zip
.putNextEntry(entry
);
75 FileInputStream in
= new FileInputStream(mimetype
);
77 IOUtils
.write(in
, zip
);
81 IOUtils
.deltree(mimetype
);
82 zip
.setLevel(ZipOutputStream
.DEFLATED
);
85 IOUtils
.zip(zip
, "", tmpDir
, true);
93 IOUtils
.deltree(tmpDir
);
101 public String
getDefaultExtension(boolean readerTarget
) {
106 protected void writeStoryHeader(Story story
) throws IOException
{
107 File ops
= new File(tmpDir
, "OPS");
109 File css
= new File(ops
, "css");
111 images
= new File(ops
, "images");
113 File metaInf
= new File(tmpDir
, "META-INF");
117 String containerContent
= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
118 + "<container xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\" version=\"1.0\">\n"
120 + "\t\t<rootfile full-path=\"OPS/epb.opf\" media-type=\"application/oebps-package+xml\"/>\n"
121 + "\t</rootfiles>\n" + "</container>\n";
123 IOUtils
.writeSmallFile(metaInf
, "container.xml", containerContent
);
126 InputStream inStyle
= getClass().getResourceAsStream("epub.style.css");
127 if (inStyle
== null) {
128 throw new IOException("Cannot find style.css resource");
131 IOUtils
.write(inStyle
, new File(css
, "style.css"));
137 if (story
.getMeta() != null && story
.getMeta().getCover() != null) {
138 File file
= new File(images
, "cover");
139 Instance
.getCache().saveAsImage(story
.getMeta().getCover(), file
,
143 // OPS/* except chapters
144 IOUtils
.writeSmallFile(ops
, "epb.ncx", generateNcx(story
));
145 IOUtils
.writeSmallFile(ops
, "epb.opf", generateOpf(story
));
146 IOUtils
.writeSmallFile(ops
, "title.xhtml", generateTitleXml(story
));
149 if (story
.getMeta() != null && story
.getMeta().getResume() != null) {
150 writeChapter(story
.getMeta().getResume());
155 protected void writeChapterHeader(Chapter chap
) throws IOException
{
156 String filename
= String
.format("%s%03d%s", "chapter-",
157 chap
.getNumber(), ".xhtml");
158 writer
= new BufferedWriter(new OutputStreamWriter(
159 new FileOutputStream(new File(tmpDir
+ File
.separator
+ "OPS",
160 filename
)), "UTF-8"));
164 String title
= "Chapter " + chap
.getNumber();
165 String nameOrNum
= Integer
.toString(chap
.getNumber());
166 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
167 title
+= ": " + chap
.getName();
168 nameOrNum
= chap
.getName();
171 writer
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
172 writer
.append("\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
173 writer
.append("\n<html xmlns=\"http://www.w3.org/1999/xhtml\">");
174 writer
.write("\n<head>");
175 writer
.write("\n <title>" + StringUtils
.xmlEscape(title
)
177 writer
.write("\n <link rel='stylesheet' href='css/style.css' type='text/css'/>");
178 writer
.write("\n</head>");
179 writer
.write("\n<body>");
180 writer
.write("\n <h2>");
181 writer
.write("\n <span class='chap'>Chapter <span class='chapnumber'>"
182 + chap
.getNumber() + "</span>:</span> ");
183 writer
.write("\n <span class='chaptitle'>"
184 + StringUtils
.xmlEscape(nameOrNum
) + "</span>");
185 writer
.write("\n </h2>");
187 writer
.write("\n <div class='chapter_content'>\n");
188 } catch (Exception e
) {
190 throw new IOException(e
);
195 protected void writeChapterFooter(Chapter chap
) throws IOException
{
198 writer
.write(" </div>\n");
202 writer
.write(" </div>\n");
205 writer
.write(" </div>\n</body>\n</html>\n");
213 protected void writeParagraphHeader(Paragraph para
) throws IOException
{
214 if (para
.getType() == ParagraphType
.QUOTE
&& !inDialogue
) {
215 writer
.write(" <div class='dialogues'>\n");
217 } else if (para
.getType() != ParagraphType
.QUOTE
&& inDialogue
) {
218 writer
.write(" </div>\n");
222 if (para
.getType() == ParagraphType
.NORMAL
&& !inNormal
) {
223 writer
.write(" <div class='normals'>\n");
225 } else if (para
.getType() != ParagraphType
.NORMAL
&& inNormal
) {
226 writer
.write(" </div>\n");
230 switch (para
.getType()) {
232 writer
.write(" <div class='blank'></div>");
235 writer
.write(" <hr class='break'/>");
238 writer
.write(" <span class='normal'>");
241 writer
.write(" <div class='dialogue'>— ");
244 File file
= new File(images
, getCurrentImageBestName(false));
245 Instance
.getCache().saveAsImage(para
.getContentImage(), file
,
247 writer
.write(" <img alt='page image' class='page-image' src='images/"
248 + getCurrentImageBestName(false) + "'/>");
252 nextParaIsCover
= false;
256 protected void writeParagraphFooter(Paragraph para
) throws IOException
{
257 switch (para
.getType()) {
259 writer
.write("</span>\n");
262 writer
.write("</div>\n");
271 protected void writeTextLine(ParagraphType type
, String line
)
276 writer
.write(decorateText(StringUtils
.xmlEscape(line
)));
284 protected String
enbold(String word
) {
285 return "<strong>" + word
+ "</strong>";
289 protected String
italize(String word
) {
290 return "<emph>" + word
+ "</emph>";
293 private String
generateNcx(Story story
) {
294 StringBuilder builder
= new StringBuilder();
299 if (story
.getMeta() != null) {
300 MetaData meta
= story
.getMeta();
301 uuid
= meta
.getUuid();
302 author
= meta
.getAuthor();
303 title
= meta
.getTitle();
306 builder
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
307 builder
.append("\n<!DOCTYPE ncx");
308 builder
.append("\nPUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">");
309 builder
.append("\n<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">");
310 builder
.append("\n <head>");
311 builder
.append("\n <!--The following four metadata items are required for all");
312 builder
.append("\n NCX documents, including those conforming to the relaxed");
313 builder
.append("\n constraints of OPS 2.0-->");
314 builder
.append("\n <meta name=\"dtb:uid\" content=\""
315 + StringUtils
.xmlEscapeQuote(uuid
) + "\"/>");
316 builder
.append("\n <meta name=\"dtb:depth\" content=\"1\"/>");
317 builder
.append("\n <meta name=\"dtb:totalPageCount\" content=\"0\"/>");
318 builder
.append("\n <meta name=\"dtb:maxPageNumber\" content=\"0\"/>");
319 builder
.append("\n <meta name=\"epub-creator\" content=\""
320 + StringUtils
.xmlEscapeQuote(EPUB_CREATOR
) + "\"/>");
321 builder
.append("\n </head>");
322 builder
.append("\n <docTitle>");
323 builder
.append("\n <text>" + StringUtils
.xmlEscape(title
) + "</text>");
324 builder
.append("\n </docTitle>");
325 builder
.append("\n <docAuthor>");
327 builder
.append("\n <text>" + StringUtils
.xmlEscape(author
) + "</text>");
328 builder
.append("\n </docAuthor>");
329 builder
.append("\n <navMap>");
330 builder
.append("\n <navPoint id=\"navpoint-1\" playOrder=\"1\">");
331 builder
.append("\n <navLabel>");
332 builder
.append("\n <text>Title Page</text>");
333 builder
.append("\n </navLabel>");
334 builder
.append("\n <content src=\"title.xhtml\"/>");
335 builder
.append("\n </navPoint>");
337 int navPoint
= 2; // 1 is above
339 if (story
.getMeta() != null & story
.getMeta().getResume() != null) {
340 Chapter chap
= story
.getMeta().getResume();
341 generateNcx(chap
, builder
, navPoint
++);
344 for (Chapter chap
: story
) {
345 generateNcx(chap
, builder
, navPoint
++);
348 builder
.append("\n </navMap>");
349 builder
.append("\n</ncx>\n");
351 return builder
.toString();
354 private void generateNcx(Chapter chap
, StringBuilder builder
, int navPoint
) {
356 if (chap
.getName() != null && !chap
.getName().isEmpty()) {
357 name
= Instance
.getTrans().getString(StringId
.CHAPTER_NAMED
,
358 chap
.getNumber(), chap
.getName());
360 name
= Instance
.getTrans().getString(StringId
.CHAPTER_UNNAMED
,
364 String nnn
= String
.format("%03d", (navPoint
- 2));
366 builder
.append("\n <navPoint id=\"navpoint-" + navPoint
367 + "\" playOrder=\"" + navPoint
+ "\">");
368 builder
.append("\n <navLabel>");
369 builder
.append("\n <text>" + name
+ "</text>");
370 builder
.append("\n </navLabel>");
371 builder
.append("\n <content src=\"chapter-" + nnn
+ ".xhtml\"/>");
372 builder
.append("\n </navPoint>\n");
375 private String
generateOpf(Story story
) {
376 StringBuilder builder
= new StringBuilder();
382 String publisher
= "";
386 if (story
.getMeta() != null) {
387 MetaData meta
= story
.getMeta();
388 title
= meta
.getTitle();
389 uuid
= meta
.getUuid();
390 author
= meta
.getAuthor();
391 date
= meta
.getDate();
392 publisher
= meta
.getPublisher();
393 subject
= meta
.getSubject();
394 source
= meta
.getSource();
395 lang
= meta
.getLang();
398 builder
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
399 builder
.append("\n<package xmlns=\"http://www.idpf.org/2007/opf\" unique-identifier=\"BookId\" version=\"2.0\">");
400 builder
.append("\n <metadata xmlns:opf=\"http://www.idpf.org/2007/opf\"");
401 builder
.append("\n xmlns:dc=\"http://purl.org/dc/elements/1.1/\">");
402 builder
.append("\n <dc:title>" + StringUtils
.xmlEscape(title
)
404 builder
.append("\n <dc:creator opf:role=\"aut\" opf:file-as=\""
405 + StringUtils
.xmlEscapeQuote(author
) + "\">"
406 + StringUtils
.xmlEscape(author
) + "</dc:creator>");
407 builder
.append("\n <dc:date opf:event=\"original-publication\">"
408 + StringUtils
.xmlEscape(date
) + "</dc:date>");
409 builder
.append("\n <dc:publisher>"
410 + StringUtils
.xmlEscape(publisher
) + "</dc:publisher>");
411 builder
.append("\n <dc:date opf:event=\"epub-publication\"></dc:date>");
412 builder
.append("\n <dc:subject>" + StringUtils
.xmlEscape(subject
)
414 builder
.append("\n <dc:source>" + StringUtils
.xmlEscape(source
)
416 builder
.append("\n <dc:rights>Not for commercial use.</dc:rights>");
417 builder
.append("\n <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"
418 + StringUtils
.xmlEscape(uuid
) + "</dc:identifier>");
419 builder
.append("\n <dc:language>" + StringUtils
.xmlEscape(lang
)
421 builder
.append("\n </metadata>");
422 builder
.append("\n <manifest>");
423 builder
.append("\n <!-- Content Documents -->");
424 builder
.append("\n <item id=\"titlepage\" href=\"title.xhtml\" media-type=\"application/xhtml+xml\"/>");
425 for (int i
= 0; i
<= story
.getChapters().size(); i
++) {
426 String name
= String
.format("%s%03d", "chapter-", i
);
427 builder
.append("\n <item id=\""
428 + StringUtils
.xmlEscapeQuote(name
) + "\" href=\""
429 + StringUtils
.xmlEscapeQuote(name
)
430 + ".xhtml\" media-type=\"application/xhtml+xml\"/>");
433 builder
.append("\n <!-- CSS Style Sheets -->");
434 builder
.append("\n <item id=\"style-css\" href=\"css/style.css\" media-type=\"text/css\"/>");
436 builder
.append("\n <!-- Images -->");
438 if (story
.getMeta() != null && story
.getMeta().getCover() != null) {
439 String format
= Instance
.getConfig()
440 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
441 builder
.append("\n <item id=\"cover\" href=\"images/cover."
442 + format
+ "\" media-type=\"image/png\"/>");
445 builder
.append("\n <!-- NCX -->");
446 builder
.append("\n <item id=\"ncx\" href=\"epb.ncx\" media-type=\"application/x-dtbncx+xml\"/>");
447 builder
.append("\n </manifest>");
448 builder
.append("\n <spine toc=\"ncx\">");
449 builder
.append("\n <itemref idref=\"titlepage\" linear=\"yes\"/>");
450 for (int i
= 0; i
<= story
.getChapters().size(); i
++) {
451 String name
= String
.format("%s%03d", "chapter-", i
);
452 builder
.append("\n <itemref idref=\""
453 + StringUtils
.xmlEscapeQuote(name
) + "\" linear=\"yes\"/>");
455 builder
.append("\n </spine>");
456 builder
.append("\n</package>\n");
458 return builder
.toString();
461 private String
generateTitleXml(Story story
) {
462 StringBuilder builder
= new StringBuilder();
467 if (story
.getMeta() != null) {
468 MetaData meta
= story
.getMeta();
469 title
= meta
.getTitle();
470 if (meta
.getTags() != null) {
471 for (String tag
: meta
.getTags()) {
472 if (!tags
.isEmpty()) {
478 if (!tags
.isEmpty()) {
479 tags
= "(" + tags
+ ")";
482 author
= meta
.getAuthor();
485 String format
= Instance
.getConfig()
486 .getString(Config
.IMAGE_FORMAT_COVER
).toLowerCase();
488 builder
.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
489 builder
.append("\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
490 builder
.append("\n<html xmlns=\"http://www.w3.org/1999/xhtml\">");
491 builder
.append("\n<head>");
492 builder
.append("\n <title>" + StringUtils
.xmlEscape(title
) + "</title>");
493 builder
.append("\n <link rel=\"stylesheet\" href=\"css/style.css\" type=\"text/css\"/>");
494 builder
.append("\n</head>");
495 builder
.append("\n<body>");
496 builder
.append("\n <div class=\"titlepage\">");
497 builder
.append("\n <h1>" + StringUtils
.xmlEscape(title
) + "</h1>");
498 builder
.append("\n <div class=\"type\">"
499 + StringUtils
.xmlEscape(tags
) + "</div>");
500 builder
.append("\n <div class=\"cover\">");
501 builder
.append("\n <img alt=\"cover image\" src=\"images/cover."
502 + format
+ "\"></img>");
503 builder
.append("\n </div>");
504 builder
.append("\n <div class=\"author\">"
505 + StringUtils
.xmlEscape(author
) + "</div>");
506 builder
.append("\n </div>");
507 builder
.append("\n</body>");
508 builder
.append("\n</html>\n");
510 return builder
.toString();