diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py
index b9f3b3943..17cbe5fd5 100644
--- a/sphinx/writers/html.py
+++ b/sphinx/writers/html.py
@@ -102,12 +102,7 @@ class HTMLTranslator(BaseTranslator):
and node['ids'] and node['first']:
self.body.append('' % node['ids'][0])
def depart_desc_signature(self, node):
- if node['ids'] and self.permalink_text and self.builder.add_permalinks:
- self.body.append(u'' % (
- _('Permalink to this definition'),
- self.permalink_text))
+ self.add_permalink_ref(node, 'definition')
self.body.append('\n')
def visit_desc_addname(self, node):
@@ -264,6 +259,12 @@ class HTMLTranslator(BaseTranslator):
elif isinstance(node.parent, nodes.container):
append_fignumber('code-block', node.parent['ids'][0])
+ def add_permalink_ref(self, node, typename):
+ if node['ids'] and self.permalink_text and self.builder.add_permalinks:
+ title = _('Permalink to this %s' % typename)
+ format = u''
+ self.body.append(format % (node['ids'][0], title, self.permalink_text))
+
# overwritten to avoid emitting empty
def visit_bullet_list(self, node):
if len(node) == 1 and node[0].tagname == 'toctree':
@@ -309,19 +310,11 @@ class HTMLTranslator(BaseTranslator):
self.add_fignumber(node)
def depart_caption(self, node):
- if node.parent['ids'] and self.permalink_text and self.builder.add_permalinks:
- if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
- figtype = 'code'
- elif isinstance(node.parent, nodes.figure):
- figtype = 'image'
- else:
- figtype = 'caption'
-
- self.body.append(u'' % (
- _('Permalink to this %s' % figtype),
- self.permalink_text))
+ # append permalink if available
+ if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
+ self.add_permalink_ref(node.parent, 'code')
+ elif isinstance(node.parent, nodes.figure):
+ self.add_permalink_ref(node.parent, 'image')
if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
self.body.append('\n')
@@ -589,25 +582,18 @@ class HTMLTranslator(BaseTranslator):
def depart_title(self, node):
close_tag = self.context[-1]
if (self.permalink_text and self.builder.add_permalinks and
- node.parent.hasattr('ids') and node.parent['ids']):
- aname = node.parent['ids'][0]
+ node.parent.hasattr('ids') and node.parent['ids']):
# add permalink anchor
if close_tag.startswith('%s' % (
- _('Permalink to this headline'),
- self.permalink_text))
+ self.add_permalink_ref(node.parent, 'headline')
elif close_tag.startswith('' % (
- _('Permalink to this table'),
- self.permalink_text))
+ elif isinstance(node.parent, nodes.table):
+ self.add_permalink_ref(node.parent, 'table')
BaseTranslator.depart_title(self, node)