summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2020-02-29 17:21:48 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2020-03-02 01:28:01 +0900
commitfbfaf41e8309580b51ee0bb78e6f46ebcaf30633 (patch)
tree6f21a786776790279b48e2e0b6bd941911900f57
parentcd15ab658f76ce38c629fc9de31d35b2ba1061f1 (diff)
downloadsphinx-git-fbfaf41e8309580b51ee0bb78e6f46ebcaf30633.tar.gz
std domain: Generate node_id for generic objects in the right way
-rw-r--r--sphinx/domains/std.py25
-rw-r--r--tests/test_build_epub.py10
-rw-r--r--tests/test_build_html.py2
3 files changed, 28 insertions, 9 deletions
diff --git a/sphinx/domains/std.py b/sphinx/domains/std.py
index abf3be716..d7239f79d 100644
--- a/sphinx/domains/std.py
+++ b/sphinx/domains/std.py
@@ -66,9 +66,17 @@ class GenericObject(ObjectDescription):
return name
def add_target_and_index(self, name: str, sig: str, signode: desc_signature) -> None:
- targetname = '%s-%s' % (self.objtype, name)
- signode['ids'].append(targetname)
+ node_id = make_id(self.env, self.state.document, self.objtype, name)
+ signode['ids'].append(node_id)
+
+ # Assign old styled node_id not to break old hyperlinks (if possible)
+ # Note: Will be removed in Sphinx-5.0 (RemovedInSphinx50Warning)
+ old_node_id = self.make_old_id(name)
+ if old_node_id not in self.state.document.ids and old_node_id not in signode['ids']:
+ signode['ids'].append(old_node_id)
+
self.state.document.note_explicit_target(signode)
+
if self.indextemplate:
colon = self.indextemplate.find(':')
if colon != -1:
@@ -77,11 +85,18 @@ class GenericObject(ObjectDescription):
else:
indextype = 'single'
indexentry = self.indextemplate % (name,)
- self.indexnode['entries'].append((indextype, indexentry,
- targetname, '', None))
+ self.indexnode['entries'].append((indextype, indexentry, node_id, '', None))
std = cast(StandardDomain, self.env.get_domain('std'))
- std.note_object(self.objtype, name, targetname, location=signode)
+ std.note_object(self.objtype, name, node_id, location=signode)
+
+ def make_old_id(self, name: str) -> str:
+ """Generate old styled node_id for generic objects.
+
+ .. note:: Old Styled node_id was used until Sphinx-3.0.
+ This will be removed in Sphinx-5.0.
+ """
+ return self.objtype + '-' + name
class EnvVar(GenericObject):
diff --git a/tests/test_build_epub.py b/tests/test_build_epub.py
index a5780b04f..a61c3cbbb 100644
--- a/tests/test_build_epub.py
+++ b/tests/test_build_epub.py
@@ -320,9 +320,13 @@ def test_epub_anchor_id(app):
app.build()
html = (app.outdir / 'index.xhtml').read_text()
- assert '<p id="std-setting-STATICFILES_FINDERS">blah blah blah</p>' in html
- assert '<span id="std-setting-STATICFILES_SECTION"></span><h1>blah blah blah</h1>' in html
- assert 'see <a class="reference internal" href="#std-setting-STATICFILES_FINDERS">' in html
+ assert ('<p id="std-setting-staticfiles-finders">'
+ '<span id="std-setting-STATICFILES_FINDERS"></span>'
+ 'blah blah blah</p>' in html)
+ assert ('<span id="std-setting-staticfiles-section"></span>'
+ '<span id="std-setting-STATICFILES_SECTION"></span>'
+ '<h1>blah blah blah</h1>' in html)
+ assert 'see <a class="reference internal" href="#std-setting-staticfiles-finders">' in html
@pytest.mark.sphinx('epub', testroot='html_assets')
diff --git a/tests/test_build_html.py b/tests/test_build_html.py
index c53d0b97d..4c2618af9 100644
--- a/tests/test_build_html.py
+++ b/tests/test_build_html.py
@@ -218,7 +218,7 @@ def test_html4_output(app, status, warning):
"[@class='rfc reference external']/strong", 'RFC 1'),
(".//a[@href='https://tools.ietf.org/html/rfc1.html']"
"[@class='rfc reference external']/strong", 'Request for Comments #1'),
- (".//a[@href='objects.html#envvar-HOME']"
+ (".//a[@href='objects.html#envvar-home']"
"[@class='reference internal']/code/span[@class='pre']", 'HOME'),
(".//a[@href='#with']"
"[@class='reference internal']/code/span[@class='pre']", '^with$'),