X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/b6ec0976a383cc1823f4a199bc3e6dc40880b049..941b8e6effd4c0ffff52fa7e548d866f8454dddb:/librarian/epub.py?ds=inline
diff --git a/librarian/epub.py b/librarian/epub.py
index b063380..48bb2f2 100644
--- a/librarian/epub.py
+++ b/librarian/epub.py
@@ -5,6 +5,7 @@
#
from __future__ import with_statement
+from copy import deepcopy
import os
import os.path
import subprocess
@@ -16,6 +17,7 @@ from tempfile import mkdtemp, NamedTemporaryFile
from shutil import rmtree
from librarian import RDFNS, WLNS, NCXNS, OPFNS, XHTMLNS, OutputFile
+from librarian.cover import WLCover
from librarian import functions, get_resource
@@ -290,8 +292,8 @@ def transform(wldoc, verbose=False,
""" produces a EPUB file
sample=n: generate sample e-book (with at least n paragraphs)
- cover: a cover.Cover object
- flags: less-advertising, without-fonts
+ cover: a cover.Cover object or True for default
+ flags: less-advertising, without-fonts, images, not-wl
"""
def transform_file(wldoc, chunk_counter=1, first=True, sample=None):
@@ -373,6 +375,7 @@ def transform(wldoc, verbose=False,
spine = opf.find('.//' + OPFNS('spine'))
output_file = NamedTemporaryFile(prefix='librarian', suffix='.epub', delete=False)
+
zip = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED)
# write static elements
@@ -386,16 +389,29 @@ def transform(wldoc, verbose=False,
'' \
'')
- zip.write(get_resource('res/wl-logo-small.png'), os.path.join('OPS', 'logo_wolnelektury.png'))
- zip.write(get_resource('res/jedenprocent.png'), os.path.join('OPS', 'jedenprocent.png'))
+ if not flags or 'not-wl' not in flags:
+ manifest.append(etree.fromstring(
+ ' '))
+ manifest.append(etree.fromstring(
+ ' '))
+ zip.write(get_resource('res/wl-logo-small.png'), os.path.join('OPS', 'logo_wolnelektury.png'))
+ zip.write(get_resource('res/jedenprocent.png'), os.path.join('OPS', 'jedenprocent.png'))
+
if not style:
style = get_resource('epub/style.css')
zip.write(style, os.path.join('OPS', 'style.css'))
-
if cover:
+ if cover is True:
+ cover = WLCover
+ if cover.uses_dc_cover:
+ if document.book_info.cover_by:
+ document.edoc.getroot().set('data-cover-by', document.book_info.cover_by)
+ if document.book_info.cover_source:
+ document.edoc.getroot().set('data-cover-source', document.book_info.cover_source)
+
cover_file = StringIO()
- c = cover(document.book_info.author.readable(), document.book_info.title)
+ c = cover(document.book_info)
c.save(cover_file)
c_name = 'cover.%s' % c.ext()
zip.writestr(os.path.join('OPS', c_name), cover_file.getvalue())
@@ -414,6 +430,26 @@ def transform(wldoc, verbose=False,
opf.getroot()[0].append(etree.fromstring(''))
guide.append(etree.fromstring(''))
+ if flags and 'images' in flags:
+ for ilustr in document.edoc.findall('//ilustr'):
+ src = ilustr.get('src')
+ mime = ImageCover(src)().mime_type()
+ zip.write(src, os.path.join('OPS', src))
+ manifest.append(etree.fromstring(
+ ' ' % (src, src, mime)))
+ # get it up to master
+ after = ilustr
+ while after.getparent().tag not in ['powiesc', 'opowiadanie', 'liryka_l', 'liryka_lp', 'dramat_wierszowany_l', 'dramat_wierszowany_lp', 'dramat_wspolczesny']:
+ after = after.getparent()
+ if not(after is ilustr):
+ moved = deepcopy(ilustr)
+ ilustr.tag = 'extra'
+ ilustr.text = None
+ moved.tail = None
+ after.addnext(moved)
+ else:
+ for ilustr in document.edoc.findall('//ilustr'):
+ ilustr.tag = 'extra'
annotations = etree.Element('annotations')
@@ -454,7 +490,13 @@ def transform(wldoc, verbose=False,
' '))
spine.append(etree.fromstring(
''))
- html_tree = xslt(document.edoc, get_resource('epub/xsltLast.xsl'))
+ stopka = document.edoc.find('//stopka')
+ if stopka is not None:
+ stopka.tag = 'stopka_'
+ replace_by_verse(stopka)
+ html_tree = xslt(stopka, get_resource('epub/xsltScheme.xsl'))
+ else:
+ html_tree = xslt(document.edoc, get_resource('epub/xsltLast.xsl'))
chars.update(used_chars(html_tree.getroot()))
zip.writestr('OPS/last.html', etree.tostring(
html_tree, method="html", pretty_print=True))