Beispiel #1
0
def generate_cover(book_data):
	"""
    Create a cover page object for ``cover.xhtml``.

    :param book_data: a data object collecting the necessary information for the creation of the new package file
    :return: Root of the generated package file; an :py:class:`ElementTree.Element` object
    """
	# Setting the default namespace; this is important when the file is generated
	ET.register_namespace('', "http://www.w3.org/1999/xhtml")
	cover = ElementTree(ET.fromstring(COVER))

	# Set the title
	title      = cover.findall(".//{http://www.w3.org/1999/xhtml}title")[0]
	title.text = book_data.title

	# Set the authors in the meta
	editors = cover.findall(".//{http://www.w3.org/1999/xhtml}meta[@name='author']")[0]
	editors.set("content", Utils.editors_to_string(book_data.editors))

	# Set the title in the text
	title      = cover.findall(".//{http://www.w3.org/1999/xhtml}h1[@id='btitle']")[0]
	title.text = book_data.title

	# Set the editors
	editors      = cover.findall(".//{http://www.w3.org/1999/xhtml}p[@id='editors']")[0]
	editors.text = book_data.editors

	if len(book_data.editors) != 0 :
		editors = cover.findall(".//{http://www.w3.org/1999/xhtml}p[@id='editors']")[0]
		editors.text = Utils.editors_to_string(book_data.editors)

	# Set the authors
	if len(book_data.authors) != 0 :
		authors = cover.findall(".//{http://www.w3.org/1999/xhtml}p[@id='authors']")[0]
		authors.text = Utils.editors_to_string(book_data.authors, editor=False)

	# Set a pointer to the original
	orig      = cover.findall(".//{http://www.w3.org/1999/xhtml}a[@id='ref_original']")[0]
	orig.text = "original documents"
	orig.tag = "span"

	# Set the correct copyright date
	span      = cover.findall(".//{http://www.w3.org/1999/xhtml}span[@id='cpdate']")[0]
	span.text = book_data.date.strftime("%Y")

	return cover
Beispiel #2
0
def generate_ncx(book_data):
	"""
    Generation of a new NCX object, combining the OPF information from the chapters.

    :param book_data: a data object collecting the necessary information for the creation of the new package file
    :return: Root of the generated package file; an :py:class:`ElementTree.Element` object
    """
	def massage_and_add_toc(toc, order):
		"""
        Add a toc item to `nav_map`, modifying its play order and its @id value. To be used in a `map` function

        :param toc: the item to be added (and ElementTree Element instance)
        :param order: play order value (essentially: index into the array of chapter's toc entries, shifted with the chapter number)
        """
		toc.set("playOrder", "%s" % order)
		toc.set("id", "nav%s" % order)
		nav_map.append(toc)

	ET.register_namespace('', "http://www.daisy.org/z3986/2005/ncx/")
	ncx = ElementTree(ET.fromstring(TOC))

	# Set the title
	title    = ncx.findall(".//{http://www.daisy.org/z3986/2005/ncx/}docTitle")[0]
	txt      = SubElement(title, "{http://www.daisy.org/z3986/2005/ncx/}text")
	txt.text = book_data.title

	# Set the authors
	authors    = ncx.findall(".//{http://www.daisy.org/z3986/2005/ncx/}docAuthor")[0]
	txt        = SubElement(authors, "{http://www.daisy.org/z3986/2005/ncx/}text")
	txt.text   = Utils.editors_to_string(book_data.editors)

	# Set the book ID
	meta_id = ncx.findall(".//{http://www.daisy.org/z3986/2005/ncx/}meta[@name='dtb:uid']")[0]
	meta_id.set('content', book_data.id)

	play_order = 2
	nav_map = ncx.find(".//{http://www.daisy.org/z3986/2005/ncx/}navMap")
	assert nav_map is not None
	for c in book_data.chapters:
		map(massage_and_add_toc, c.ncx, range(play_order, play_order + len(c.ncx)))
		play_order += len(c.ncx)

	return ncx