from lxml import etree
import zipfile
-from librarian import XMLNamespace, RDFNS, DCNS, WLNS, NoDublinCore
-from librarian.dcparser import BookInfo
-
-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)
+import sys
+sys.path.append('..') # for running from working copy
- 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 = {}
-
- 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):
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)
'version="2005-1"><head></head><docTitle></docTitle><navMap>' \
'<navPoint id="NavPoint-1" playOrder="1"><navLabel>' \
'<text>Strona tytułowa</text></navLabel><content src="title.html" />' \
- '</navPoint><navPoint id="NavPoint-2" playOrder="2"><navLabel>' \
- '<text>Początek utworu</text></navLabel><content src="part1.html" />' \
'</navPoint></navMap></ncx>')
nav_map = toc_file[-1]
toc, chunk_counter = transform_file(input_xml)
- toc_counter = toc.write_to_xml(nav_map, 3) # we already have 2 navpoints
+ if not toc.children:
+ toc.add(u"Początek utworu", 1)
+ toc_counter = toc.write_to_xml(nav_map, 2)
# Last modifications in container files and EPUB creation
if len(annotations) > 0:
if __name__ == '__main__':
- import sys
+ from librarian import DirDocProvider
if len(sys.argv) < 2:
print >> sys.stderr, 'Usage: python epub.py <input file>'