mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Refactor creating permalinks on HTML writer
This commit is contained in:
@@ -102,12 +102,7 @@ class HTMLTranslator(BaseTranslator):
|
||||
and node['ids'] and node['first']:
|
||||
self.body.append('<!--[%s]-->' % 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'<a class="headerlink" href="#%s" '
|
||||
% node['ids'][0] +
|
||||
u'title="%s">%s</a>' % (
|
||||
_('Permalink to this definition'),
|
||||
self.permalink_text))
|
||||
self.add_permalink_ref(node, 'definition')
|
||||
self.body.append('</dt>\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'<a class="headerlink" href="#%s" title="%s">%s</a>'
|
||||
self.body.append(format % (node['ids'][0], title, self.permalink_text))
|
||||
|
||||
# overwritten to avoid emitting empty <ul></ul>
|
||||
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'<a class="headerlink" href="#%s" '
|
||||
% node.parent['ids'][0] +
|
||||
u'title="%s">%s</a>' % (
|
||||
_('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('</div>\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('</h'):
|
||||
self.body.append(u'<a class="headerlink" href="#%s" ' % aname +
|
||||
u'title="%s">%s</a>' % (
|
||||
_('Permalink to this headline'),
|
||||
self.permalink_text))
|
||||
self.add_permalink_ref(node.parent, 'headline')
|
||||
elif close_tag.startswith('</a></h'):
|
||||
self.body.append(u'</a><a class="headerlink" href="#%s" ' %
|
||||
aname +
|
||||
node.parent['ids'][0] +
|
||||
u'title="%s">%s' % (
|
||||
_('Permalink to this headline'),
|
||||
self.permalink_text))
|
||||
elif close_tag.startswith('</caption>'):
|
||||
self.body.append(u'<a class="headerlink" href="#%s" ' % aname +
|
||||
u'title="%s">%s</a>' % (
|
||||
_('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)
|
||||
|
||||
|
Reference in New Issue
Block a user