Source

rst2epub / rst2epub / epub.py

Full commit
from zipfile import ZipFile, ZIP_STORED
from xml.dom.minidom import Document, getDOMImplementation

from container import Container
from package import Package
from ncx import NavigationControl

class EpubWriter:

    media_type = "application/epub+zip"
    """EPUB media type see 3.4 in OCF1.0"""

    extension = ".epub"
    """EPUB file extension see 3.4 in OCF1.0"""

    def __init__(self, to_file, title, identifier, language):
        self.to_file = to_file
        self.title = title
        self.identifier = identifier
        self.language = language

        # create the zip container with correct mimetype
        self.file = ZipFile(to_file, "w", ZIP_STORED)
        self.file.writestr("mimetype", EpubWriter.media_type)

        # create required container
        # user must add required rootfile
        self.container = Container()

        # create required package / OPF file
        self.package = Package()

        # ...add required metadata fields
        self.package.metadata.add_title(self.title)
        self.package.metadata.add_identifier(self.identifier, True)
        self.package.metadata.add_language("en")

        # create required NCX file
        # ... add required header fields
        # TODO: '1' should not be hard coded
        self.ncx = NavigationControl(self.identifier, 1, self.title)

    def add_content(self, content):
        self.content = content

    def write(self):
        self.file.writestr(self.container.path, self.container.to_xml())
        self.file.writestr(Package.path, self.package.to_xml())
        self.file.writestr(NavigationControl.path, self.ncx.to_xml())

        self.write_content()

        self.file.close()



    def write_content(self):
        self.file.writestr("content/content.xhtml", self.content)


if __name__ == "__main__":
    writer = EpubWriter("ebook.epub",   # name of generated file
                        "My Title",     # required infos
                        "unique-id",
                        "en"
                       )

    # add root file
    writer.container.add_rootfile(Package.path, Package.media_type)

    content = """<?xml version="1.0" encoding="UTF-8" ?>
                 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
                           "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
                 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
                     <head>
                         <meta http-equiv="Content-Type"
                               content="application/xhtml+xml; charset=utf-8" />
                         <title>My Title</title>
                     </head>
                     <body><p>This is a test epub file.</p></body>
                 </html>
              """
    writer.add_content(content)

    # list content items in manifest
    # TODO should be generated when content is added
    writer.package.manifest.add_item("item1", "content/content.xhtml", "application/xhtml+xml")

    # add content reference to spine
    # TODO should be generated when content is added
    writer.package.spine.add_itemref("item1")

    # reference start of content from guide -- optional
    guide = writer.package.create_guide()
    guide.add_reference("text", "Content", "content/content.xhtml")

    # add content to nav map
    # TODO should be generated when content is added
    writer.ncx.nav_map.add_nav_point("content", 1, "Content", "content/content.xhtml")

    writer.write()