Merge branch 'master' into ofop
[librarian.git] / librarian / epub.py
index b063380..48bb2f2 100644 (file)
@@ -5,6 +5,7 @@
 #
 from __future__ import with_statement
 
 #
 from __future__ import with_statement
 
+from copy import deepcopy
 import os
 import os.path
 import subprocess
 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 shutil import rmtree
 
 from librarian import RDFNS, WLNS, NCXNS, OPFNS, XHTMLNS, OutputFile
+from librarian.cover import WLCover
 
 from librarian import functions, get_resource
 
 
 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)
     """ 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):
     """
 
     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)
     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
     zip = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED)
 
     # write static elements
@@ -386,16 +389,29 @@ def transform(wldoc, verbose=False,
                        '<rootfiles><rootfile full-path="OPS/content.opf" ' \
                        'media-type="application/oebps-package+xml" />' \
                        '</rootfiles></container>')
                        '<rootfiles><rootfile full-path="OPS/content.opf" ' \
                        'media-type="application/oebps-package+xml" />' \
                        '</rootfiles></container>')
-    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(
+            '<item id="logo_wolnelektury" href="logo_wolnelektury.png" media-type="image/png" />'))
+        manifest.append(etree.fromstring(
+            '<item id="jedenprocent" href="jedenprocent.png" media-type="image/png" />'))
+        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 not style:
         style = get_resource('epub/style.css')
     zip.write(style, os.path.join('OPS', 'style.css'))
 
-
     if cover:
     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()
         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())
         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('<meta name="cover" content="cover-image"/>'))
         guide.append(etree.fromstring('<reference href="cover.html" type="cover" title="Okładka"/>'))
 
         opf.getroot()[0].append(etree.fromstring('<meta name="cover" content="cover-image"/>'))
         guide.append(etree.fromstring('<reference href="cover.html" type="cover" title="Okładka"/>'))
 
+    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(
+                '<item id="%s" href="%s" media-type="%s" />' % (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')
 
 
     annotations = etree.Element('annotations')
 
@@ -454,7 +490,13 @@ def transform(wldoc, verbose=False,
         '<item id="last" href="last.html" media-type="application/xhtml+xml" />'))
     spine.append(etree.fromstring(
         '<itemref idref="last" />'))
         '<item id="last" href="last.html" media-type="application/xhtml+xml" />'))
     spine.append(etree.fromstring(
         '<itemref idref="last" />'))
-    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))
     chars.update(used_chars(html_tree.getroot()))
     zip.writestr('OPS/last.html', etree.tostring(
                         html_tree, method="html", pretty_print=True))