From a875e8f377ff0da75a64db915e124836bbf454f5 Mon Sep 17 00:00:00 2001 From: tk0miya Date: Sun, 5 Oct 2014 16:27:38 +0900 Subject: [PATCH] Set permanent links to every captions and titles --- sphinx/themes/agogo/static/agogo.css_t | 5 ++++- sphinx/themes/basic/static/basic.css_t | 5 ++++- sphinx/themes/scrolls/static/scrolls.css_t | 5 ++++- sphinx/writers/html.py | 19 +++++++++++++++++++ tests/test_directive_code.py | 14 ++++++++++---- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/sphinx/themes/agogo/static/agogo.css_t b/sphinx/themes/agogo/static/agogo.css_t index db4a621a4..686b1312d 100644 --- a/sphinx/themes/agogo/static/agogo.css_t +++ b/sphinx/themes/agogo/static/agogo.css_t @@ -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; } diff --git a/sphinx/themes/basic/static/basic.css_t b/sphinx/themes/basic/static/basic.css_t index 3616288c8..1d1c5e595 100644 --- a/sphinx/themes/basic/static/basic.css_t +++ b/sphinx/themes/basic/static/basic.css_t @@ -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; } diff --git a/sphinx/themes/scrolls/static/scrolls.css_t b/sphinx/themes/scrolls/static/scrolls.css_t index 9591f045f..2040d97fa 100644 --- a/sphinx/themes/scrolls/static/scrolls.css_t +++ b/sphinx/themes/scrolls/static/scrolls.css_t @@ -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; } diff --git a/sphinx/writers/html.py b/sphinx/writers/html.py index 82c228bdc..b9f3b3943 100644 --- a/sphinx/writers/html.py +++ b/sphinx/writers/html.py @@ -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'%s' % ( + _('Permalink to this %s' % figtype), + self.permalink_text)) + if isinstance(node.parent, nodes.container) and node.parent.get('literal_block'): self.body.append('\n') else: @@ -589,6 +603,11 @@ class HTMLTranslator(BaseTranslator): u'title="%s">%s' % ( _('Permalink to this headline'), self.permalink_text)) + elif close_tag.startswith(''): + self.body.append(u'%s' % ( + _('Permalink to this table'), + self.permalink_text)) BaseTranslator.depart_title(self, node) diff --git a/tests/test_directive_code.py b/tests/test_directive_code.py index 0c2cead62..90af8d5b1 100644 --- a/tests/test_directive_code.py +++ b/tests/test_directive_code.py @@ -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 = '
caption test rb
' + html = (app.outdir / 'caption.html').text(encoding='utf-8') + caption = (u'
' + u'caption test rb' + u'\xb6
') 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 = '
caption test py
' + html = (app.outdir / 'caption.html').text(encoding='utf-8') + caption = (u'
' + u'caption test py' + u'\xb6
') assert caption in html