1. Nikolaj Šujskij
  2. rst2epub

Source

rst2epub / rst2epub / converter.py

import os
import sys

from rst import RstDocument
from epub import EpubWriter

from package import Package

def main(rstfile, epubfile):
    rst = RstDocument(rstfile)
    title = rst.get_title()
    author = rst.get_author()
    identifier = "TODO"
    language = "en_TODO"
    writer = EpubWriter(epubfile, title, identifier, language, author)

    writer.container.add_rootfile(Package.path, Package.media_type)

    writer.package.manifest.add_item("item1", "content/content.xhtml", "application/xhtml+xml")
    writer.package.spine.add_itemref("item1")

    writer.ncx.nav_map.add_nav_point("content", 1, "Content", "content/content.xhtml")

    writer.add_content(rst.to_xhtml())
    writer.write()


if __name__ == "__main__":
    usage = '''Usage:
    $ {0} <input_file.rst> <output_file.epub>\n'''.format(sys.argv[0])
    if '--help' in sys.argv:
        print(usage)
        sys.exit(0)
    if len(sys.argv) != 3:
        sys.stderr.write(usage)
        sys.stderr.write("Invalid argument number\n")
        sys.exit(1)
    rstfile, epubfile = sys.argv[1:]
    if not (os.path.exists(rstfile) and os.path.isfile(rstfile)):
        sys.stderr.write("Input file does not exist or is not a file\n")
        sys.exit(1)
    main(rstfile, epubfile)