1. Nikolaj Šujskij
  2. rst2epub

Source

rst2epub / rst2epub / ncx.py

from xml.dom.minidom import Document, Element

class NavigationControl(Document):
    """Represents NCX filet see [OPF20]_ and [NCX]_.

       .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
          http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
       .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
          http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX


        This file is required exactly once.
    """

    extension = ".ncx"
    """File extension."""

    media_type = "application/x-dtbncx+xml"
    """Media typ as specified in [OPF20]_."""

    path = "toc" + extension    # conventions used in this implementation
    """Path of the OPF Package Document within the container."""

    namespace = "http://www.daisy.org/z3986/2005/ncx/"
    """Namespace for OPF Package Document."""

    version = "2005-1"
    """NCX file version supported by this implemntation."""


    def __init__(self, dtb_uid, dtb_depth, title):
        Document.__init__(self)

        self.ncx = self.createElement("ncx")
        self.appendChild(self.ncx)
        self.ncx.setAttribute("version", self.version)
        self.ncx.setAttribute("xmlns", self.namespace)

        self.head = self.create_head(dtb_uid, dtb_depth)
        self.doc_title = self.create_doc_title(title)
        self.nav_map = self.create_nav_map()

    def create_head(self, dtb_uid, dtb_depth, dtb_totalPageCount=0,
                    dtb_bacPageNumber=0):
        head = Head(self, dtb_uid, str(dtb_depth), str(dtb_totalPageCount),
                    str(dtb_bacPageNumber))
        self.ncx.appendChild(head)
        return head

    def create_doc_title(self, title):
        doc_title = DocTitle(self, title)
        self.ncx.appendChild(doc_title)
        return doc_title

    def create_nav_map(self):
        nav_map = NavMap(self)
        self.ncx.appendChild(nav_map)
        return nav_map

    def create_text_element(self, value):
        text = self.createElement("text")
        text.appendChild(self.createTextNode(value))
        return text

    def to_xml(self):
        return self.toprettyxml(indent="    ", encoding="utf-8")


class Head(Element):
    """Represents head element see [NCX]_.

       Only meta elements are used by OPF.

       .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
          http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
       .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
          http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX

        This element is required.
    """

    def __init__(self, nxc_document, dtb_uid, dtb_depth,
                 dtb_totalPageCount, dtb_bacPageNumber):
        Element.__init__(self, "head")
        self.ownerDocument = nxc_document

        # add required meta elements
        self.add_meta("dtb:uid", dtb_uid)
        self.add_meta("dtb:depth", dtb_depth)
        # not used by OPF - shoulg be 0
        self.add_meta("dtb:totalPageCount", dtb_totalPageCount)
        self.add_meta("dtb:maxPageNumber", dtb_bacPageNumber)

    def add_meta(self, name, content):
        element = self.ownerDocument.createElement("meta")
        self.appendChild(element)
        element.setAttribute("name", name)
        element.setAttribute("content", content)

class DocTitle(Element):
    """Represents docTitle element see [NCX]_.

       audio element is not used in OPF.

       .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
          http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
       .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
          http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX

        This element is required.
    """

    def __init__(self, nxc_document, text):
        Element.__init__(self, "docTitle")
        self.ownerDocument = nxc_document

        self.appendChild(self.ownerDocument.create_text_element(text))

class NavMap(Element):
    """Represents navMap element see [NCX]_.


       .. [OPF20] Open Packaging Format (OPF) 2.0 v0.9871.0
          http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html
       .. [NCX] Navigation Control File (NCX) (in ANSI/NISO Z39.86-2005)
          http://www.niso.org/workrooms/daisy/Z39-86-2005.html#NCX

        This element is required.
    """

    def __init__(self, nxc_document):
        Element.__init__(self, "navMap")
        self.ownerDocument = nxc_document

    def add_nav_point(self, id, play_order, nav_label, content_src):
        point = self.ownerDocument.createElement("navPoint")
        self.appendChild(point)
        point.setAttribute("id", id)
        point.setAttribute("playOrder", str(play_order))

        label = self.ownerDocument.createElement("navLabel")
        point.appendChild(label)
        label.appendChild(self.ownerDocument.create_text_element(nav_label))

        content = self.ownerDocument.createElement("content")
        point.appendChild(content)
        content.setAttribute("src", content_src)