import os
import os.path
-import shutil
-import sys
from copy import deepcopy
from lxml import etree
import zipfile
-from librarian import XMLNamespace, RDFNS, DCNS, WLNS, XHTMLNS, NoDublinCore
-from librarian.parser import WLDocument
-
-#TODO: shouldn't be repeated here
-NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
-OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
-
-
-class DocProvider(object):
- class DoesNotExist(Exception):
- pass
-
- def by_slug(self, slug):
- raise NotImplemented
-
- def __getitem__(self, slug):
- return self.by_slug(slug)
-
- def by_uri(self, uri):
- return self.by_slug(uri.rsplit('/', 1)[1])
-
-
-class DirDocProvider(DocProvider):
- def __init__(self, dir):
- self.dir = dir
- self.files = {}
+import sys
+sys.path.append('..') # for running from working copy
- def by_slug(self, slug):
- return open(os.path.join(self.dir, '%s.xml' % slug))
+from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NCXNS, OPFNS, NoDublinCore
+from librarian.dcparser import BookInfo
def inner_xml(node):
<a>x<b>y</b>z</a>
"""
-
p = etree.fromstring('<x>%s</x>' % text)
node.text = p.text
node[:] = p[:]
return output_html, toc
-def transform(provider, slug, output_file):
+def transform(provider, slug, output_file=None, output_dir=None):
""" produces an epub
provider is a DocProvider
- output_file should be filelike object
+ either output_file (a file-like object) or output_dir (path to file/dir) should be specified
+ if output_dir is specified, file will be written to <output_dir>/<author>/<slug>.epub
"""
def transform_file(input_xml, chunk_counter=1, first=True):
if main_text is not None:
replace_characters(main_text)
- for chunk_no, chunk_xml in enumerate(chop(main_text), chunk_counter):
+ for chunk_xml in chop(main_text):
chunk_html, chunk_toc = transform_chunk(chunk_xml, chunk_counter, annotations)
toc.extend(chunk_toc)
zip.writestr('OPS/part%d.html' % chunk_counter, chunk_html)
return toc, chunk_counter
+ # read metadata from the first file
+ input_xml = etree.parse(provider[slug])
+ metadata = input_xml.find('.//'+RDFNS('Description'))
+ if metadata is None:
+ raise NoDublinCore('Document has no DublinCore - which is required.')
+ book_info = BookInfo.from_element(input_xml)
+ metadata = etree.ElementTree(metadata)
+
+ # if output to dir, create the file
+ if output_dir is not None:
+ author = unicode(book_info.author)
+ author_dir = os.path.join(output_dir, author)
+ try:
+ os.makedirs(author_dir)
+ except OSError:
+ pass
+ output_file = open(os.path.join(author_dir, '%s.epub' % slug), 'w')
+
zip = zipfile.ZipFile(output_file, 'w', zipfile.ZIP_DEFLATED)
for fname in 'style.css', 'logo_wolnelektury.png':
zip.write(res(fname), os.path.join('OPS', fname))
- # metadata from first file
- input_xml = etree.parse(provider[slug])
- metadata = input_xml.find('.//'+RDFNS('Description'))
- if metadata is None:
- raise NoDublinCore('Document has no DublinCore - which is required.')
- metadata = etree.ElementTree(metadata)
opf = xslt(metadata, res('xsltContent.xsl'))
manifest = opf.find('.//' + OPFNS('manifest'))
spine = opf.find('.//' + OPFNS('spine'))
if __name__ == '__main__':
+ from librarian import DirDocProvider
+
if len(sys.argv) < 2:
print >> sys.stderr, 'Usage: python epub.py <input file>'
sys.exit(1)
main_input = sys.argv[1]
basepath, ext = os.path.splitext(main_input)
path, slug = os.path.realpath(basepath).rsplit('/', 1)
- output = basepath + '.epub'
provider = DirDocProvider(path)
- transform(provider, slug, open(output, 'w'))
+ transform(provider, slug, output_dir=path)