mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Refactor: move process_only_nodes() to sphinx.util.nodes
This commit is contained in:
@@ -37,7 +37,8 @@ from sphinx import addnodes
|
||||
from sphinx.io import SphinxStandaloneReader, SphinxDummyWriter, SphinxFileInput
|
||||
from sphinx.util import url_re, get_matching_docs, docname_join, split_into, \
|
||||
FilenameUniqDict, split_index_msg
|
||||
from sphinx.util.nodes import clean_astext, WarningStream, is_translatable
|
||||
from sphinx.util.nodes import clean_astext, WarningStream, is_translatable, \
|
||||
process_only_nodes
|
||||
from sphinx.util.osutil import SEP, getcwd, fs_encoding, ensuredir
|
||||
from sphinx.util.images import guess_mimetype
|
||||
from sphinx.util.i18n import find_catalog_files, get_image_filename_for_language, \
|
||||
@@ -1058,7 +1059,7 @@ class BuildEnvironment(object):
|
||||
# the document does not exist anymore: return a dummy node that
|
||||
# renders to nothing
|
||||
return nodes.paragraph()
|
||||
self.process_only_nodes(toc, builder, docname)
|
||||
process_only_nodes(toc, builder.tags, warn_node=self.warn_node)
|
||||
for node in toc.traverse(nodes.reference):
|
||||
node['refuri'] = node['anchorname'] or '#'
|
||||
return toc
|
||||
@@ -1270,7 +1271,7 @@ class BuildEnvironment(object):
|
||||
maxdepth = self.metadata[ref].get('tocdepth', 0)
|
||||
if ref not in toctree_ancestors or (prune and maxdepth > 0):
|
||||
self._toctree_prune(toc, 2, maxdepth, collapse)
|
||||
self.process_only_nodes(toc, builder, ref)
|
||||
process_only_nodes(toc, builder.tags, warn_node=self.warn_node)
|
||||
if title and toc.children and len(toc.children) == 1:
|
||||
child = toc.children[0]
|
||||
for refnode in child.traverse(nodes.reference):
|
||||
@@ -1403,7 +1404,7 @@ class BuildEnvironment(object):
|
||||
node.replace_self(newnode or contnode)
|
||||
|
||||
# remove only-nodes that do not belong to our builder
|
||||
self.process_only_nodes(doctree, builder, fromdocname)
|
||||
process_only_nodes(doctree, builder.tags, warn_node=self.warn_node)
|
||||
|
||||
# allow custom references to be resolved
|
||||
builder.app.emit('doctree-resolved', doctree, fromdocname)
|
||||
@@ -1492,24 +1493,6 @@ class BuildEnvironment(object):
|
||||
newnode[0]['classes'].append(res_role.replace(':', '-'))
|
||||
return newnode
|
||||
|
||||
def process_only_nodes(self, doctree, builder, fromdocname=None):
|
||||
# A comment on the comment() nodes being inserted: replacing by [] would
|
||||
# result in a "Losing ids" exception if there is a target node before
|
||||
# the only node, so we make sure docutils can transfer the id to
|
||||
# something, even if it's just a comment and will lose the id anyway...
|
||||
for node in doctree.traverse(addnodes.only):
|
||||
try:
|
||||
ret = builder.tags.eval_condition(node['expr'])
|
||||
except Exception as err:
|
||||
self.warn_node('exception while evaluating only '
|
||||
'directive expression: %s' % err, node)
|
||||
node.replace_self(node.children or nodes.comment())
|
||||
else:
|
||||
if ret:
|
||||
node.replace_self(node.children or nodes.comment())
|
||||
else:
|
||||
node.replace_self(nodes.comment())
|
||||
|
||||
def assign_section_numbers(self):
|
||||
"""Assign a section number to each heading under a numbered toctree."""
|
||||
# a list of all docnames whose section numbers changed
|
||||
|
||||
@@ -321,6 +321,27 @@ def set_role_source_info(inliner, lineno, node):
|
||||
node.source, node.line = inliner.reporter.get_source_and_line(lineno)
|
||||
|
||||
|
||||
def process_only_nodes(doctree, tags, warn_node=None):
|
||||
# A comment on the comment() nodes being inserted: replacing by [] would
|
||||
# result in a "Losing ids" exception if there is a target node before
|
||||
# the only node, so we make sure docutils can transfer the id to
|
||||
# something, even if it's just a comment and will lose the id anyway...
|
||||
for node in doctree.traverse(addnodes.only):
|
||||
try:
|
||||
ret = tags.eval_condition(node['expr'])
|
||||
except Exception as err:
|
||||
if warn_node is None:
|
||||
raise err
|
||||
warn_node('exception while evaluating only '
|
||||
'directive expression: %s' % err, node)
|
||||
node.replace_self(node.children or nodes.comment())
|
||||
else:
|
||||
if ret:
|
||||
node.replace_self(node.children or nodes.comment())
|
||||
else:
|
||||
node.replace_self(nodes.comment())
|
||||
|
||||
|
||||
# monkey-patch Element.copy to copy the rawsource and line
|
||||
|
||||
def _new_copy(self):
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
import re
|
||||
|
||||
from docutils import nodes
|
||||
from sphinx.util.nodes import process_only_nodes
|
||||
|
||||
from util import with_app
|
||||
|
||||
@@ -46,7 +47,7 @@ def test_sectioning(app, status, warning):
|
||||
|
||||
app.builder.build(['only'])
|
||||
doctree = app.env.get_doctree('only')
|
||||
app.env.process_only_nodes(doctree, app.builder)
|
||||
process_only_nodes(doctree, app.builder.tags)
|
||||
|
||||
parts = [getsects(n)
|
||||
for n in [_n for _n in doctree.children if isinstance(_n, nodes.section)]]
|
||||
|
||||
Reference in New Issue
Block a user