The branch, epub/new_features/navigation, has been updated. - Log -----------------------------------------------------------------
commit fa1f22c852f6f6349fcc0c6d87d5dfb49834f267 Author: Josh Hieronymus <[email protected]> Date: Mon Sep 16 02:22:37 2013 -0400 Implement navigation controls for exported EPUB files. If a document has an inserted Table of Contents, we can parse the intermediate XHTML file for this information. Right now, only flat navigation controls, not hierarchical controls, have been implemented (which means that parts, chapters, sections, etc. all appear at the same depth). diff --git a/lib/scripts/epub/epub_oebps.py b/lib/scripts/epub/epub_oebps.py index 824a3cd..2204003 100644 --- a/lib/scripts/epub/epub_oebps.py +++ b/lib/scripts/epub/epub_oebps.py @@ -18,6 +18,7 @@ from epub_xhtml_utilities import extract_author_file_as from epub_xhtml_utilities import extract_identifier from epub_xhtml_utilities import extract_identifier_scheme from epub_xhtml_utilities import extract_language +from epub_xhtml_utilities import extract_navigation_information from epub_xhtml_utilities import extract_title class Oebps(object): @@ -225,7 +226,6 @@ class Content(object): itemref.set("idref", xhtml_label) self.root.append(itemref) -# TODO: (Potentially) add NavPoints for skipping around in file, bookmarks, etc. class Toc(object): FILENAME = "toc.ncx" XML_VERSION = "1.0" @@ -310,11 +310,21 @@ class Toc(object): text.text = author self.root.append(text) - # TODO: parse xhtml_root to create navigable EPUB with proper section titles + # TODO: (potentially) enable hierachical structure of navigation points class NavMap(object): def __init__(self, xhtml_name, xhtml_label, xhtml_root): self.root = ET.Element("navMap") - self.add_navpoint(self.root, "chapter", xhtml_label, "1", "The Only Chapter", xhtml_name) + self.add_navpoint(self.root, "document", xhtml_label, "1", "Front", xhtml_name) + navigation_information = extract_navigation_information(xhtml_root) + i = 2 + for nav_info in navigation_information: + id_ = "navpoint-" + str(i) + play_order = str(i) + src = xhtml_name + nav_info["src"] + self.add_navpoint( + self.root, nav_info["class"], id_, play_order, + nav_info["nav_label_text"], src) + i += 1 def add_navpoint(self, root, class_, id_, play_order, nav_label_text, src): nav_point = ET.Element("navPoint") diff --git a/lib/scripts/epub/epub_xhtml_utilities.py b/lib/scripts/epub/epub_xhtml_utilities.py index f8f474c..729da52 100644 --- a/lib/scripts/epub/epub_xhtml_utilities.py +++ b/lib/scripts/epub/epub_xhtml_utilities.py @@ -115,3 +115,38 @@ def extract_author_file_as(xhtml_root): """ author_file_as = extract_attribute(xhtml_root, "AUTHOR_FILE_AS") return author_file_as + +def extract_navigation_information(xhtml_root): + """Extract navigation information content from an XHTML file and return it. + + Returns a list of dictionaries for each navigation point found in the XHTML + file. Each dictionary contains its navigation point's class (with key + "class"), the text found within its navLabel element's child element (key + "nav_label_text"), and the location in the document to which it links (key + "src"). + + Keyword Arguments: + xhtml_root -- an ElementTree Element representing the root element of a LyX + document's exported XHTML file + + """ + navigation_information = [] + toc_root_xpath_schema = ".//{{{0}}}div[@class='toc']" + toc_root_xpath = toc_root_xpath_schema.format(_XHTML_XMLNS) + toc_root = xhtml_root.find(toc_root_xpath) + if toc_root is None: + return navigation_information + toc_entry_parent_xpath_schema = ".//{{{0}}}a[@class='tocentry']/.." + toc_entry_parent_xpath = toc_entry_parent_xpath_schema.format(_XHTML_XMLNS) + toc_entry_parents = toc_root.findall(toc_entry_parent_xpath) + for toc_entry_parent in toc_entry_parents: + toc_entry = toc_entry_parent.find("*") + class_ = toc_entry_parent.get("class") + nav_label_text = toc_entry.text + src = toc_entry.get("href") + navigation_information.append({ + "class": class_, + "nav_label_text": nav_label_text, + "src": src}) + + return navigation_information ----------------------------------------------------------------------- Summary of changes: lib/scripts/epub/epub_oebps.py | 16 +++++++++++-- lib/scripts/epub/epub_xhtml_utilities.py | 35 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) hooks/post-receive -- Repositories for GSOC work
