diff options
| author | tk0miya <i.tkomiya@gmail.com> | 2014-10-05 20:54:50 +0900 |
|---|---|---|
| committer | tk0miya <i.tkomiya@gmail.com> | 2014-10-05 20:54:50 +0900 |
| commit | 2d746cbd82ecc1f0445e4bd5d32049d6b13e7530 (patch) | |
| tree | 56c3ca5b8f31e3d790e8add40cb62bbc003f0bd9 /sphinx | |
| parent | de43236d792a74d6ae108f9255bf7bd73f36ab4e (diff) | |
| download | sphinx-2d746cbd82ecc1f0445e4bd5d32049d6b13e7530.tar.gz | |
Refactor creating permalinks on HTML writer
Diffstat (limited to 'sphinx')
| -rw-r--r-- | sphinx/writers/html.py | 48 |
1 files changed, 17 insertions, 31 deletions
diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index b9f3b394..17cbe5fd 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('<!--[%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) |
