Source

sphinx-gsoc2009 / sphinx / web / singlebuilder.py

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    sphinx.web.singlebuilder
    ~~~~~~~~~~~~~~~~~~~~~~~~

    A HTML builder for a single file.

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

import os
from os import path

from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.builders.webapp.webapp import WebappHTMLTranslator
from sphinx.environment import BuildEnvironment
from sphinx.errors import SphinxError

# this class should be moved somewhere
class SinglefileHTMLBuilder(StandaloneHTMLBuilder, Sphinx):
    """Render a file when requested using data from a pickled archive.
    Handle theming."""
    def __init__(self, www_dir, repopath, piddbfile, licence):
        self.outdir = www_dir
        self.srcdir = repopath
        self.piddbfile = piddbfile
        self.licence = licence
        self.cwd = os.getcwd()
        self.confdir = self.cwd
        self.doctreedir = path.join(self.cwd, '../doctrees')
        self.docsettings = None
        self.env = None
        self.freshenv = None
        confdir = path.join(self.cwd, '../../') # <cwd>/_build/webapp/../../

        self.app = Sphinx(self.srcdir, confdir, self.outdir, self.doctreedir,
                          'html', {}, None)
        self.info = self.app.info
        self.warn = self.app.warn
        self.config = self.app.config
        self.config.html_theme_path = 'themes/' + self.config.html_theme

        self.env = BuildEnvironment(self.srcdir, self.doctreedir, self.config)
        #self.env.find_files(self.config)
        self.env.set_warnfunc(self.warn)

        # XXX got to figure out what's this
        self.env.domaindata = {}
        self.env.domaindata['py'] = {}
        self.env.domaindata['py']['modules'] = []
        #self.load_i18n()
        #self.load_env()

        StandaloneHTMLBuilder.init(self)

        self.app.add_javascript('jquery.form.js')
        self.app.add_javascript('comments.js')
        self.prepare_writing((None,))
        self.style = self.theme.get_confstr('theme', 'stylesheet')
       
        self.config.html_additional_pages = {}
        self.images = {}

    def init_translator_class(self):
        WebappHTMLTranslator.licence = self.licence
        WebappHTMLTranslator.piddbfile = self.piddbfile
        WebappHTMLTranslator.repopath = self.srcdir
        self.translator_class = WebappHTMLTranslator

    def render_page(self, docname):
        # rst file has changed and thus we need to build new doctree file
        # and then build new html file
        self.env.read_doc(docname)
        doctree = self.env.get_and_resolve_doctree(docname, self)
        StandaloneHTMLBuilder.write_doc(self, docname, doctree)