Georg Brandl avatar Georg Brandl committed 583226f

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.)

Comments (0)

Files changed (3)

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

sphinx/environment.py

 
 from sphinx import addnodes
 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.directives import additional_xref_types
 
                           node.line)
             self.anonlabels[name] = docname, labelid
             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':
                 for n in node:
                     if n.tagname == 'caption':
-                        sectname = n.astext()
+                        sectname = clean_astext(n)
                         break
                 else:
                     continue
                         # toctree originates
                         ref = toctreenode['parent']
                         if not title:
-                            title = self.titles[ref].astext()
+                            title = clean_astext(self.titles[ref])
                         reference = nodes.reference('', '',
                                                     refuri=ref,
                                                     anchorname='',
                             # reference with explicit title
                             caption = node.astext()
                         else:
-                            caption = self.titles[docname].astext()
+                            caption = clean_astext(self.titles[docname])
                         innernode = nodes.emphasis(caption, caption)
                         newnode = nodes.reference('', '')
                         newnode['refuri'] = builder.get_relative_uri(

sphinx/util/__init__.py

         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
 # traverse() is called so many times during a build that it saves
 # on average 20-25% overall build time!
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.