rst2epub / rst2epub /

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_identifier(self.identifier, True)

        # 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())



    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

    # 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"
                 <html xmlns="" xml:lang="en">
                         <meta http-equiv="Content-Type"
                               content="application/xhtml+xml; charset=utf-8" />
                         <title>My Title</title>
                     <body><p>This is a test epub file.</p></body>

    # 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

    # 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")