+ def transform_file(input_xml, chunk_counter=1, first=True):
+ """ processes one input file and proceeds to its children """
+
+ children = [child.text for child in input_xml.findall('.//'+DCNS('relation.hasPart'))]
+
+ # every input file will have a TOC entry,
+ # pointing to starting chunk
+ toc = TOC(node_name(input_xml.find('.//'+DCNS('title'))), chunk_counter)
+ chars = set()
+ if first:
+ # write book title page
+ html_tree = xslt(input_xml, res('xsltTitle.xsl'))
+ chars = used_chars(html_tree.getroot())
+ zip.writestr('OPS/title.html',
+ etree.tostring(html_tree, pretty_print=True))
+ elif children:
+ # write title page for every parent
+ html_tree = xslt(input_xml, res('xsltChunkTitle.xsl'))
+ chars = used_chars(html_tree.getroot())
+ zip.writestr('OPS/part%d.html' % chunk_counter,
+ etree.tostring(html_tree, pretty_print=True))
+ add_to_manifest(manifest, chunk_counter)
+ add_to_spine(spine, chunk_counter)
+ chunk_counter += 1
+
+ if len(input_xml.getroot()) > 1:
+ # rdf before style master
+ main_text = input_xml.getroot()[1]
+ else:
+ # rdf in style master
+ main_text = input_xml.getroot()[0]
+ if main_text.tag == RDFNS('RDF'):
+ main_text = None
+
+ if main_text is not None:
+ replace_characters(main_text)
+
+ for chunk_xml in chop(main_text):
+ chunk_html, chunk_toc, chunk_chars = transform_chunk(chunk_xml, chunk_counter, annotations)
+ toc.extend(chunk_toc)
+ chars = chars.union(chunk_chars)
+ zip.writestr('OPS/part%d.html' % chunk_counter, chunk_html)
+ add_to_manifest(manifest, chunk_counter)
+ add_to_spine(spine, chunk_counter)
+ chunk_counter += 1
+
+ if children:
+ for child in children:
+ child_xml = etree.parse(provider.by_uri(child))
+ child_toc, chunk_counter, chunk_chars = transform_file(child_xml, chunk_counter, first=False)
+ toc.append(child_toc)
+ chars = chars.union(chunk_chars)
+
+ return toc, chunk_counter, chars
+
+ # 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:
+ if make_dir:
+ author = unicode(book_info.author)
+ output_dir = os.path.join(output_dir, author)
+ try:
+ os.makedirs(output_dir)
+ except OSError:
+ pass
+ output_file = open(os.path.join(output_dir, '%s.epub' % slug), 'w')