epub covers
authorJan Szejko <janek37@gmail.com>
Fri, 9 Dec 2016 15:05:27 +0000 (16:05 +0100)
committerJan Szejko <janek37@gmail.com>
Fri, 9 Dec 2016 15:05:27 +0000 (16:05 +0100)
librarian/__init__.py
librarian/formats/cover/__init__.py
librarian/formats/cover/evens/__init__.py [new file with mode: 0644]
librarian/formats/epub/__init__.py
librarian/formats/epub/res/cover.html

index 0616f23..02464ef 100644 (file)
@@ -3,7 +3,6 @@
 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
-import os
 import re
 import urllib
 from .utils import XMLNamespace
 import re
 import urllib
 from .utils import XMLNamespace
@@ -23,13 +22,19 @@ class UnicodeException(Exception):
             message = unicode(args, encoding='utf-8', errors='ignore')
         return message
 
             message = unicode(args, encoding='utf-8', errors='ignore')
         return message
 
+
 class ParseError(UnicodeException):
     pass
 
 class ParseError(UnicodeException):
     pass
 
+
 class ValidationError(UnicodeException):
     pass
 
 
 class ValidationError(UnicodeException):
     pass
 
 
+class BuildError(Exception):
+    pass
+
+
 class EmptyNamespace(XMLNamespace):
     def __init__(self):
         super(EmptyNamespace, self).__init__('')
 class EmptyNamespace(XMLNamespace):
     def __init__(self):
         super(EmptyNamespace, self).__init__('')
@@ -53,8 +58,7 @@ class WLURI(object):
     slug = None
 
     example = 'http://wolnelektury.pl/katalog/lektura/template/'
     slug = None
 
     example = 'http://wolnelektury.pl/katalog/lektura/template/'
-    _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/'
-            '(?P<slug>[-a-z0-9]+)/?$')
+    _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
 
     def __init__(self, uri):
         uri = unicode(uri)
 
     def __init__(self, uri):
         uri = unicode(uri)
@@ -93,4 +97,3 @@ class WLURI(object):
 class URLOpener(urllib.FancyURLopener):
     version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
 urllib._urlopener = URLOpener()
 class URLOpener(urllib.FancyURLopener):
     version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
 urllib._urlopener = URLOpener()
-
index 7a787e8..b9b515a 100644 (file)
@@ -126,6 +126,7 @@ class Cover(Format):
 
     logo_bottom = None
     logo_width = None
 
     logo_bottom = None
     logo_width = None
+    logo_file = get_resource('res/wl-logo.png')
     uses_dc_cover = False
 
     format = 'JPEG'
     uses_dc_cover = False
 
     format = 'JPEG'
@@ -164,14 +165,17 @@ class Cover(Format):
 
         if self.background_img:
             background = Image.open(self.background_img)
 
         if self.background_img:
             background = Image.open(self.background_img)
-            img.paste(background, None, background)
-            del background
+            resized = background.resize((1024, background.height*1024/background.width), Image.ANTIALIAS)
+            resized = resized.convert('RGBA')
+            img.paste(resized, (0, 0), resized)
+            del background, resized
 
 
-        # WL logo
         if metr.logo_width:
         if metr.logo_width:
-            logo = Image.open(get_resource('res/wl-logo.png'))
-            logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]))
-            img.paste(logo, ((metr.width - metr.logo_width) / 2, img.size[1] - logo.size[1] - metr.logo_bottom))
+            logo = Image.open(self.logo_file)
+            logo = logo.resize((metr.logo_width, logo.size[1] * metr.logo_width / logo.size[0]), Image.ANTIALIAS)
+            logo = logo.convert('RGBA')
+            img.paste(logo, ((metr.width - metr.logo_width) / 2,
+                             img.size[1] - logo.size[1] - metr.logo_bottom), logo)
 
         top = metr.author_top
         tbox = TextBox(
 
         top = metr.author_top
         tbox = TextBox(
diff --git a/librarian/formats/cover/evens/__init__.py b/librarian/formats/cover/evens/__init__.py
new file mode 100644 (file)
index 0000000..4207d46
--- /dev/null
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+import urllib
+from urllib2 import urlopen, URLError
+
+from librarian import DCNS, BuildError
+from .. import Cover
+
+
+class EvensCover(Cover):
+    format_name = u"Evens cover image"
+    width = 1024
+    height = 1365
+    author_top = 900
+    title_top = 30
+    logo_bottom = 100
+
+    def __init__(self, doc, format=None, width=None, height=None):
+        super(EvensCover, self).__init__(doc, format=format, width=width, height=height)
+        self.doc = doc
+
+    def set_images(self, ctx):
+        cover_url = self.doc.meta.get(DCNS('relation.coverimage.url'))[0]
+        if cover_url.startswith('file://'):
+            cover_url = ctx.files_path + urllib.quote(cover_url[7:])
+        try:
+            self.background_img = urlopen(cover_url)
+        except URLError:
+            raise BuildError('Cannot open the cover image: %s' % cover_url)
+
+        if getattr(ctx, 'cover_logo', None):
+            self.logo_width = 150
+            self.logo_file = urlopen(ctx.cover_logo)
index b9d1c7a..bf21a6f 100644 (file)
@@ -15,7 +15,7 @@ from lxml import etree
 from librarian import OPFNS, NCXNS, XHTMLNS, DCNS
 from librarian import core
 from librarian.formats import Format
 from librarian import OPFNS, NCXNS, XHTMLNS, DCNS
 from librarian import core
 from librarian.formats import Format
-from librarian.formats.cover.wolnelektury import WLCover
+from librarian.formats.cover.evens import EvensCover
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer, UnknownElement
 from librarian.utils import Context, get_resource, extend_element
 from librarian.output import OutputFile
 from librarian.renderers import Register, TreeRenderer, UnknownElement
 from librarian.utils import Context, get_resource, extend_element
@@ -25,7 +25,7 @@ class EpubFormat(Format):
     format_name = 'EPUB'
     format_ext = 'epub'
 
     format_name = 'EPUB'
     format_ext = 'epub'
 
-    cover = WLCover
+    cover = EvensCover
     renderers = Register()
 
     def __init__(self, doc, cover=None, with_fonts=True):
     renderers = Register()
 
     def __init__(self, doc, cover=None, with_fonts=True):
@@ -79,8 +79,9 @@ class EpubFormat(Format):
         # nav_map = toc_file[-1]
 
         if self.cover is not None:
         # nav_map = toc_file[-1]
 
         if self.cover is not None:
-            cover_image = self.doc.meta.get(DCNS('relation.coverimage.url'))[0]
+            cover_image = self.doc.meta.get(DCNS('relation.coverimage.url'))[0]
             cover = self.cover(self.doc)
             cover = self.cover(self.doc)
+            cover.set_images(ctx)
             cover_output = cover.build()
             cover_name = 'cover.%s' % cover.format_ext
             zip.writestr(os.path.join('OPS', cover_name), cover_output.get_string())
             cover_output = cover.build()
             cover_name = 'cover.%s' % cover.format_ext
             zip.writestr(os.path.join('OPS', cover_name), cover_output.get_string())
@@ -117,7 +118,7 @@ class EpubFormat(Format):
 
         wrap_tmpl = etree.parse(get_resource('formats/epub/res/chapter.html'))
         for e in self.render(self.doc.edoc.getroot(), ctx):
 
         wrap_tmpl = etree.parse(get_resource('formats/epub/res/chapter.html'))
         for e in self.render(self.doc.edoc.getroot(), ctx):
-            if not len(e) and not e.text.strip():
+            if not len(e) and not (e.text and e.text.strip()):
                 continue
             wrap = deepcopy(wrap_tmpl)
             extend_element(wrap.find('//*[@id="book-text"]'), e)
                 continue
             wrap = deepcopy(wrap_tmpl)
             extend_element(wrap.find('//*[@id="book-text"]'), e)
index 784067c..3233201 100644 (file)
@@ -2,12 +2,12 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
     <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
 <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
     <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
-    <title>Okładka</title>
+    <title>Cover</title>
     <style type="text/css"> img { max-width: 100%; } </style>
   </head>
   <body style="oeb-column-number: 1;">
     <div id="cover-image">
     <style type="text/css"> img { max-width: 100%; } </style>
   </head>
   <body style="oeb-column-number: 1;">
     <div id="cover-image">
-      <img alt="Okładka" />
+      <img alt="Cover" />
     </div>
   </body>
 </html>
\ No newline at end of file
     </div>
   </body>
 </html>
\ No newline at end of file