Set permanent links to every captions and titles

This commit is contained in:
tk0miya 2014-10-05 16:27:38 +09:00
parent 5eb459f5a6
commit a875e8f377
5 changed files with 41 additions and 7 deletions

View File

@ -97,7 +97,10 @@ h3:hover > a.headerlink,
h4:hover > a.headerlink,
h5:hover > a.headerlink,
h6:hover > a.headerlink,
dt:hover > a.headerlink {
dt:hover > a.headerlink,
caption:hover > a.headerlink,
p.caption:hover > a.headerlink,
div.code-block-caption:hover > a.headerlink {
visibility: visible;
}

View File

@ -197,7 +197,10 @@ h3:hover > a.headerlink,
h4:hover > a.headerlink,
h5:hover > a.headerlink,
h6:hover > a.headerlink,
dt:hover > a.headerlink {
dt:hover > a.headerlink,
caption:hover > a.headerlink,
p.caption:hover > a.headerlink,
div.code-block-caption:hover > a.headerlink {
visibility: visible;
}

View File

@ -250,7 +250,10 @@ h4:hover > a.headerlink,
h5:hover > a.headerlink,
h6:hover > a.headerlink,
dt:hover > a.headerlink,
dt:hover > a.headerlink {
dt:hover > a.headerlink,
caption:hover > a.headerlink,
p.caption:hover > a.headerlink,
div.code-block-caption:hover > a.headerlink {
visibility: visible;
}

View File

@ -309,6 +309,20 @@ 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))
if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'):
self.body.append('</div>\n')
else:
@ -589,6 +603,11 @@ class HTMLTranslator(BaseTranslator):
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))
BaseTranslator.depart_title(self, node)

View File

@ -52,8 +52,11 @@ def test_code_block_dedent(app, status, warning):
@with_app('html', testroot='directive-code')
def test_code_block_caption_html(app, status, warning):
app.builder.build(['caption'])
html = (app.outdir / 'caption.html').text()
caption = '<div class="code-block-caption">caption <em>test</em> rb</div>'
html = (app.outdir / 'caption.html').text(encoding='utf-8')
caption = (u'<div class="code-block-caption">'
u'caption <em>test</em> rb'
u'<a class="headerlink" href="#id1" '
u'title="Permalink to this code">\xb6</a></div>')
assert caption in html
@ -98,8 +101,11 @@ def test_literal_include_dedent(app, status, warning):
@with_app('html', testroot='directive-code')
def test_literalinclude_caption_html(app, status, warning):
app.builder.build('index')
html = (app.outdir / 'caption.html').text()
caption = '<div class="code-block-caption">caption <strong>test</strong> py</div>'
html = (app.outdir / 'caption.html').text(encoding='utf-8')
caption = (u'<div class="code-block-caption">'
u'caption <strong>test</strong> py'
u'<a class="headerlink" href="#id2" '
u'title="Permalink to this code">\xb6</a></div>')
assert caption in html