1. lyhcode
  2. sphinx


sphinx / sphinx / builders / mobi.py

# -*- coding: utf-8 -*-

    Build epub files.
    Originally derived from epub.py.

    :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.

import os

from sphinx.builders.epub import EpubBuilder

_content_template = u'''\
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0"
  <metadata xmlns:opf="http://www.idpf.org/2007/opf"
    <dc:creator opf:role="aut">%(author)s</dc:creator>
    <dc:identifier id="%(uid)s" opf:scheme="%(scheme)s">%(id)s</dc:identifier>
    <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml" />
  <spine toc="ncx">
    <reference type="toc" title="Table of Contents" href="contents.html" />
    <reference type="text" title="Overview" href="index.html" />

_mimetype_template = 'application/x-mobipocket-ebook'

class MobiBuilder(EpubBuilder):
    Builder that outputs mobi files.

    It creates the metainfo files container.opf, toc.ncx, mimetype, and
    META-INF/container.xml.  Afterwards, the kindlegen utility is used
    to generate the mobi file.
    name = 'mobi'

    def init(self):

    # Finish by building the mobi file
    def handle_finish(self):
        """Create the metainfo files and finally the mobi."""
        _epub_file = os.path.join(self.outdir,
            self.config.epub_basename + '.epub')
        os.system("kindlegen -c1 -rebuild -verbose %s" % _epub_file)

    def get_theme_config(self):
        return self.config.mobi_theme, {}

    def build_content(self, outdir, outname):
        """Write the metainfo file content.opf It contains bibliographic data,
        a file list, the spine (the reading order) and the guide (first page
        and html version of the toc).

        EpubBuilder.build_content(self, outdir, outname,