summaryrefslogtreecommitdiff
path: root/sphinx/util/nodes.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/util/nodes.py')
-rw-r--r--sphinx/util/nodes.py89
1 files changed, 54 insertions, 35 deletions
diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py
index e6ccc3733..2b0fff81d 100644
--- a/sphinx/util/nodes.py
+++ b/sphinx/util/nodes.py
@@ -36,20 +36,27 @@ caption_ref_re = explicit_title_re # b/w compat alias
def apply_source_workaround(node):
+ # workaround: nodes.term have wrong rawsource if classifier is specified.
+ # The behavior of docutils-0.11, 0.12 is:
+ # * when ``term text : classifier1 : classifier2`` is specified,
+ # * rawsource of term node will have: ``term text : classifier1 : classifier2``
+ # * rawsource of classifier node will be None
+ if isinstance(node, nodes.classifier) and not node.rawsource:
+ definition_list_item = node.parent
+ node.source = definition_list_item.source
+ node.line = definition_list_item.line - 1
+ node.rawsource = node.astext() # set 'classifier1' (or 'classifier2')
+ if isinstance(node, nodes.term):
+ # overwrite: ``term : classifier1 : classifier2`` -> ``term text``
+ node.rawsource = node.astext()
+
+ # workaround: recommonmark-0.2.0 doesn't set rawsource attribute
+ if not node.rawsource:
+ node.rawsource = node.astext()
+
if node.source and node.rawsource:
return
- # workaround: nodes.term doesn't have source, line and rawsource
- # (fixed in Docutils r7495)
- if isinstance(node, nodes.term):
- definition_list_item = node.parent
- if definition_list_item.line is not None:
- node.source = definition_list_item.source
- node.line = definition_list_item.line - 1
- node.rawsource = definition_list_item. \
- rawsource.split("\n", 2)[0]
- return
-
# workaround: docutils-0.10.0 or older's nodes.caption for nodes.figure
# and nodes.title for nodes.admonition doesn't have source, line.
# this issue was filed to Docutils tracker:
@@ -71,16 +78,20 @@ IGNORED_NODES = (
nodes.Inline,
nodes.literal_block,
nodes.doctest_block,
- #XXX there are probably more
+ addnodes.versionmodified,
+ # XXX there are probably more
)
+
+
def is_translatable(node):
if isinstance(node, nodes.TextElement):
- apply_source_workaround(node)
-
if not node.source:
- return False # built-in message
+ return False # built-in message
if isinstance(node, IGNORED_NODES) and 'translatable' not in node:
return False
+ if not node.get('translatable', True):
+ # not(node['translatable'] == True or node['translatable'] is None)
+ return False
# <field_name>orphan</field_name>
# XXX ignore all metadata (== docinfo)
if isinstance(node, nodes.field_name) and node.children[0] == 'orphan':
@@ -101,6 +112,8 @@ LITERAL_TYPE_NODES = (
IMAGE_TYPE_NODES = (
nodes.image,
)
+
+
def extract_messages(doctree):
"""Extract translatable messages from a document tree."""
for node in doctree.traverse(is_translatable):
@@ -136,7 +149,7 @@ def traverse_translatable_index(doctree):
"""Traverse translatable index node from a document tree."""
def is_block_index(node):
return isinstance(node, addnodes.index) and \
- node.get('inline') == False
+ node.get('inline') is False
for node in doctree.traverse(is_block_index):
if 'raw_entries' in node:
entries = node['raw_entries']
@@ -184,6 +197,7 @@ indextypes = [
'single', 'pair', 'double', 'triple', 'see', 'seealso',
]
+
def process_index_entry(entry, targetid):
indexentries = []
entry = entry.strip()
@@ -220,7 +234,7 @@ def process_index_entry(entry, targetid):
return indexentries
-def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc):
+def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc, traversed):
"""Inline all toctrees in the *tree*.
Record all docnames in *docnameset*, and output docnames with *colorfunc*.
@@ -230,22 +244,25 @@ def inline_all_toctrees(builder, docnameset, docname, tree, colorfunc):
newnodes = []
includefiles = map(text_type, 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
- for sectionnode in sof.traverse(nodes.section):
- if 'docname' not in sectionnode:
- sectionnode['docname'] = includefile
- newnodes.append(sof)
+ if includefile not in traversed:
+ try:
+ traversed.append(includefile)
+ builder.info(colorfunc(includefile) + " ", nonl=1)
+ subtree = inline_all_toctrees(builder, docnameset, includefile,
+ builder.env.get_doctree(includefile),
+ colorfunc, traversed)
+ 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
+ for sectionnode in sof.traverse(nodes.section):
+ if 'docname' not in sectionnode:
+ sectionnode['docname'] = includefile
+ newnodes.append(sof)
toctreenode.parent.replace(toctreenode, newnodes)
return tree
@@ -256,8 +273,8 @@ def make_refnode(builder, fromdocname, todocname, targetid, child, title=None):
if fromdocname == todocname:
node['refid'] = targetid
else:
- node['refuri'] = (builder.get_relative_uri(fromdocname, todocname)
- + '#' + targetid)
+ node['refuri'] = (builder.get_relative_uri(fromdocname, todocname) +
+ '#' + targetid)
if title:
node['reftitle'] = title
node.append(child)
@@ -268,9 +285,11 @@ def set_source_info(directive, node):
node.source, node.line = \
directive.state_machine.get_source_and_line(directive.lineno)
+
def set_role_source_info(inliner, lineno, node):
node.source, node.line = inliner.reporter.get_source_and_line(lineno)
+
# monkey-patch Element.copy to copy the rawsource
def _new_copy(self):