Refactor toctree inlining to into a util function.

This commit is contained in:
Georg Brandl 2010-01-17 16:26:44 +01:00
parent 11089371b1
commit f3fc36b9f0
2 changed files with 32 additions and 22 deletions

View File

@ -21,6 +21,7 @@ from sphinx import package_dir, addnodes
from sphinx.util import SEP, texescape, copyfile from sphinx.util import SEP, texescape, copyfile
from sphinx.builders import Builder from sphinx.builders import Builder
from sphinx.environment import NoUri from sphinx.environment import NoUri
from sphinx.util import inline_all_toctrees
from sphinx.util.console import bold, darkgreen from sphinx.util.console import bold, darkgreen
from sphinx.writers.latex import LaTeXWriter from sphinx.writers.latex import LaTeXWriter
@ -114,27 +115,6 @@ class LaTeXBuilder(Builder):
def assemble_doctree(self, indexfile, toctree_only, appendices): def assemble_doctree(self, indexfile, toctree_only, appendices):
self.docnames = set([indexfile] + appendices) self.docnames = set([indexfile] + appendices)
self.info(darkgreen(indexfile) + " ", nonl=1) self.info(darkgreen(indexfile) + " ", nonl=1)
def process_tree(docname, tree):
tree = tree.deepcopy()
for toctreenode in tree.traverse(addnodes.toctree):
newnodes = []
includefiles = map(str, toctreenode['includefiles'])
for includefile in includefiles:
try:
self.info(darkgreen(includefile) + " ", nonl=1)
subtree = process_tree(
includefile, self.env.get_doctree(includefile))
self.docnames.add(includefile)
except Exception:
self.warn('toctree contains ref to nonexisting '
'file %r' % includefile,
self.env.doc2path(docname))
else:
sof = addnodes.start_of_file(docname=includefile)
sof.children = subtree.children
newnodes.append(sof)
toctreenode.parent.replace(toctreenode, newnodes)
return tree
tree = self.env.get_doctree(indexfile) tree = self.env.get_doctree(indexfile)
tree['docname'] = indexfile tree['docname'] = indexfile
if toctree_only: if toctree_only:
@ -148,7 +128,8 @@ class LaTeXBuilder(Builder):
for node in tree.traverse(addnodes.toctree): for node in tree.traverse(addnodes.toctree):
new_sect += node new_sect += node
tree = new_tree tree = new_tree
largetree = process_tree(indexfile, tree) largetree = inline_all_toctrees(self, self.docnames, indexfile, tree,
darkgreen)
largetree['docname'] = indexfile largetree['docname'] = indexfile
for docname in appendices: for docname in appendices:
appendix = self.env.get_doctree(docname) appendix = self.env.get_doctree(docname)

View File

@ -28,6 +28,7 @@ from docutils.utils import relative_path
import jinja2 import jinja2
import sphinx import sphinx
from sphinx import addnodes
# Errnos that we need. # Errnos that we need.
EEXIST = getattr(errno, 'EEXIST', 0) EEXIST = getattr(errno, 'EEXIST', 0)
@ -503,6 +504,34 @@ except NameError:
return True return True
return False return False
def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc):
"""Inline all toctrees in the *tree*.
Record all docnames in *docnameset*, and output docnames with *colorfunc*.
"""
tree = tree.deepcopy()
for toctreenode in tree.traverse(addnodes.toctree):
newnodes = []
includefiles = map(str, toctreenode['includefiles'])
for includefile in includefiles:
try:
builder.info(colorfunc(includefile) + " ", nonl=1)
subtree = inline_all_toctrees(builder, docnameset, includefile,
builder.env.get_doctree(includefile), colorfunc)
docnameset.add(includefile)
except Exception:
builder.warn('toctree contains ref to nonexisting '
'file %r' % includefile,
builder.env.doc2path(docname))
else:
sof = addnodes.start_of_file(docname=includefile)
sof.children = subtree.children
newnodes.append(sof)
toctreenode.parent.replace(toctreenode, newnodes)
return tree
# monkey-patch Node.traverse to get more speed # monkey-patch Node.traverse to get more speed
# traverse() is called so many times during a build that it saves # traverse() is called so many times during a build that it saves
# on average 20-25% overall build time! # on average 20-25% overall build time!