From 25af49b6c63e1c005129856e107143864ad5b245 Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Fri, 9 Dec 2016 16:05:27 +0100 Subject: [PATCH] epub covers --- librarian/__init__.py | 11 ++++--- librarian/formats/cover/__init__.py | 16 ++++++---- librarian/formats/cover/evens/__init__.py | 36 +++++++++++++++++++++++ librarian/formats/epub/__init__.py | 9 +++--- librarian/formats/epub/res/cover.html | 4 +-- 5 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 librarian/formats/cover/evens/__init__.py diff --git a/librarian/__init__.py b/librarian/__init__.py index 0616f23..02464ef 100644 --- a/librarian/__init__.py +++ b/librarian/__init__.py @@ -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. # -import os 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 + class ParseError(UnicodeException): pass + class ValidationError(UnicodeException): pass +class BuildError(Exception): + pass + + 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/' - _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/' - '(?P[-a-z0-9]+)/?$') + _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P[-a-z0-9]+)/?$') 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() - diff --git a/librarian/formats/cover/__init__.py b/librarian/formats/cover/__init__.py index 7a787e8..b9b515a 100644 --- a/librarian/formats/cover/__init__.py +++ b/librarian/formats/cover/__init__.py @@ -126,6 +126,7 @@ class Cover(Format): logo_bottom = None logo_width = None + logo_file = get_resource('res/wl-logo.png') uses_dc_cover = False format = 'JPEG' @@ -164,14 +165,17 @@ class Cover(Format): 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: - 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( diff --git a/librarian/formats/cover/evens/__init__.py b/librarian/formats/cover/evens/__init__.py new file mode 100644 index 0000000..4207d46 --- /dev/null +++ b/librarian/formats/cover/evens/__init__.py @@ -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) diff --git a/librarian/formats/epub/__init__.py b/librarian/formats/epub/__init__.py index b9d1c7a..bf21a6f 100644 --- a/librarian/formats/epub/__init__.py +++ b/librarian/formats/epub/__init__.py @@ -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.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 @@ -25,7 +25,7 @@ class EpubFormat(Format): format_name = 'EPUB' format_ext = 'epub' - cover = WLCover + cover = EvensCover 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: - 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.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()) @@ -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): - 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) diff --git a/librarian/formats/epub/res/cover.html b/librarian/formats/epub/res/cover.html index 784067c..3233201 100644 --- a/librarian/formats/epub/res/cover.html +++ b/librarian/formats/epub/res/cover.html @@ -2,12 +2,12 @@ - Okładka + Cover
- Okładka + Cover
\ No newline at end of file -- 2.20.1