Ignore images in section titles when generating link captions.

(Otherwise, the "alt" text leaks into the caption, and docutils automatically
assigns an alt text to images in substitutions.)
This commit is contained in:
Georg Brandl 2010-01-03 11:42:15 +01:00
parent 6049dae619
commit 0b70a32efe
3 changed files with 15 additions and 5 deletions

View File

@ -1,6 +1,8 @@
Release 0.6.4 (in development) Release 0.6.4 (in development)
============================== ==============================
* Ignore images in section titles when generating link captions.
* #310: support exception messages in the ``testoutput`` blocks of * #310: support exception messages in the ``testoutput`` blocks of
the ``doctest`` extension. the ``doctest`` extension.

View File

@ -37,7 +37,7 @@ from docutils.transforms.parts import ContentsFilter
from sphinx import addnodes from sphinx import addnodes
from sphinx.util import movefile, get_matching_docs, SEP, ustrftime, \ from sphinx.util import movefile, get_matching_docs, SEP, ustrftime, \
docname_join, FilenameUniqDict, url_re docname_join, FilenameUniqDict, url_re, clean_astext
from sphinx.errors import SphinxError from sphinx.errors import SphinxError
from sphinx.directives import additional_xref_types from sphinx.directives import additional_xref_types
@ -820,11 +820,11 @@ class BuildEnvironment:
node.line) node.line)
self.anonlabels[name] = docname, labelid self.anonlabels[name] = docname, labelid
if node.tagname == 'section': if node.tagname == 'section':
sectname = node[0].astext() # node[0] == title node sectname = clean_astext(node[0]) # node[0] == title node
elif node.tagname == 'figure': elif node.tagname == 'figure':
for n in node: for n in node:
if n.tagname == 'caption': if n.tagname == 'caption':
sectname = n.astext() sectname = clean_astext(n)
break break
else: else:
continue continue
@ -1074,7 +1074,7 @@ class BuildEnvironment:
# toctree originates # toctree originates
ref = toctreenode['parent'] ref = toctreenode['parent']
if not title: if not title:
title = self.titles[ref].astext() title = clean_astext(self.titles[ref])
reference = nodes.reference('', '', reference = nodes.reference('', '',
refuri=ref, refuri=ref,
anchorname='', anchorname='',
@ -1222,7 +1222,7 @@ class BuildEnvironment:
# reference with explicit title # reference with explicit title
caption = node.astext() caption = node.astext()
else: else:
caption = self.titles[docname].astext() caption = clean_astext(self.titles[docname])
innernode = nodes.emphasis(caption, caption) innernode = nodes.emphasis(caption, caption)
newnode = nodes.reference('', '') newnode = nodes.reference('', '')
newnode['refuri'] = builder.get_relative_uri( newnode['refuri'] = builder.get_relative_uri(

View File

@ -442,6 +442,14 @@ def copy_static_entry(source, target, builder, context={}):
shutil.copytree(source, target) shutil.copytree(source, target)
def clean_astext(node):
"""Like node.astext(), but ignore images."""
node = node.deepcopy()
for img in node.traverse(docutils.nodes.image):
img['alt'] = ''
return node.astext()
# 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!