mfrasca / txt2epub

create a epub file from a set of text files
47 stars 11 forks source link

add cover page #23

Open mfrasca opened 7 years ago

mfrasca commented 7 years ago

something like this, following the hints at https://www.safaribooksonline.com/blog/2009/11/20/best-practices-in-epub-cover-images/

but this does not add the necessary metadata. without that, the cover is not shown. just too lazy to complete it now.

diff --git a/python/scripts/txt2epub b/python/scripts/txt2epub
index 9b70352..aac4c37 100755
--- a/python/scripts/txt2epub
+++ b/python/scripts/txt2epub
@@ -31,6 +31,7 @@ if __name__ == '__main__':
     parser.add_argument('--keep-line-breaks', action='store_true')
     parser.add_argument('--nokeep-line-breaks', action='store_false', dest='keep_line_breaks')
     parser.add_argument('--type')
+    parser.add_argument('--cover-page')
     parser.add_argument('--title')
     parser.add_argument('--author')
     parser.add_argument('--creator')
diff --git a/python/txt2epublib/__init__.py b/python/txt2epublib/__init__.py
index b462f70..bbaa283 100644
--- a/python/txt2epublib/__init__.py
+++ b/python/txt2epublib/__init__.py
@@ -116,6 +116,27 @@ def main(destination, sources, **options):
         template = env.get_template("item.html")
         split_on = '\n\n'
     included = []
+
+    if options.get('cover_page'):
+        options['cover_page_basename'] = os.path.basename(options['cover_page'])
+        out = open(tempdir + "/content/cover.html", "w")
+        out.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <title>Cover</title>
+    <style type="text/css"> img { max-width: 100%%; } </style>
+  </head>
+  <body>
+    <div id="cover-image">
+        <img src="%(cover_page_basename)s" alt="Title of this Thing"/>
+    </div>
+  </body>
+</html>""" % options)
+        out.close()
+        copyfile(options['cover_page'], tempdir + "/content/" + options['cover_page_basename'])
+        included.append(options['cover_page_basename'])
+        included.append('cover.html')
+
     for item in sources:
         if item['type'] in ["png", "jpg"]:
             copyfile(item['orig'], tempdir + "/content/" + item['full'])