+ def transform_file(input_xml, chunk_counter=1, first=True, sample=None):
+ """ processes one input file and proceeds to its children """
+
+ replace_characters(input_xml.getroot())
+
+ 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, get_resource('epub/xsltTitle.xsl'))
+ chars = used_chars(html_tree.getroot())
+ zip.writestr('OPS/title.html',
+ etree.tostring(html_tree, method="html", pretty_print=True))
+ elif children:
+ # write title page for every parent
+ if sample is not None and sample <= 0:
+ chars = set()
+ html_string = open(get_resource('epub/emptyChunk.html')).read()
+ else:
+ html_tree = xslt(input_xml, get_resource('epub/xsltChunkTitle.xsl'))
+ chars = used_chars(html_tree.getroot())
+ html_string = etree.tostring(html_tree, method="html", pretty_print=True)
+ zip.writestr('OPS/part%d.html' % chunk_counter, html_string)
+ 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:
+ for chunk_xml in chop(main_text):
+ empty = False
+ if sample is not None:
+ if sample <= 0:
+ empty = True
+ else:
+ sample -= len(chunk_xml.xpath('//strofa|//akap|//akap_cd|//akap_dialog'))
+ chunk_html, chunk_toc, chunk_chars = transform_chunk(chunk_xml, chunk_counter, annotations, empty)
+
+ 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, sample = transform_file(child_xml, chunk_counter, first=False, sample=sample)
+ toc.append(child_toc)
+ chars = chars.union(chunk_chars)
+
+ return toc, chunk_counter, chars, sample
+
+ # read metadata from the first file
+ if file_path:
+ if slug:
+ raise ValueError('slug or file_path should be specified, not both')
+ f = open(file_path, 'r')
+ input_xml = etree.parse(f)
+ f.close()
+ else:
+ if not slug:
+ raise ValueError('either slug or file_path should be specified')
+ 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
+ if slug:
+ output_file = open(os.path.join(output_dir, '%s.epub' % slug), 'w')
+ else:
+ output_file = open(os.path.join(output_dir, os.path.splitext(os.path.basename(file_path))[0] + '.epub'), 'w')