Skip to content

Commit de65166

Browse files
AA-Turnerhugovk
authored andcommitted
pythonGH-121970: Extract issue_role into a new extension (pythonGH-130615)
(cherry picked from commit 043ab3a) Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent a55a9cd commit de65166

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

Doc/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'changes',
3030
'glossary_search',
3131
'implementation_detail',
32+
'issue_role',
3233
'lexers',
3334
'misc_news',
3435
'pydoc_topics',

Doc/tools/extensions/issue_role.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Support for referencing issues in the tracker."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
from docutils import nodes
8+
from sphinx.util.docutils import SphinxRole
9+
10+
if TYPE_CHECKING:
11+
from docutils.nodes import Element
12+
from sphinx.application import Sphinx
13+
from sphinx.util.typing import ExtensionMetadata
14+
15+
16+
class BPOIssue(SphinxRole):
17+
ISSUE_URI = "https://bugs.python.org/issue?@action=redirect&bpo={0}"
18+
19+
def run(self) -> tuple[list[Element], list[nodes.system_message]]:
20+
issue = self.text
21+
22+
# sanity check: there are no bpo issues within these two values
23+
if 47_261 < int(issue) < 400_000:
24+
msg = self.inliner.reporter.error(
25+
f"The BPO ID {issue!r} seems too high. "
26+
"Use :gh:`...` for GitHub IDs.",
27+
line=self.lineno,
28+
)
29+
prb = self.inliner.problematic(self.rawtext, self.rawtext, msg)
30+
return [prb], [msg]
31+
32+
issue_url = self.ISSUE_URI.format(issue)
33+
refnode = nodes.reference(issue, f"bpo-{issue}", refuri=issue_url)
34+
self.set_source_info(refnode)
35+
return [refnode], []
36+
37+
38+
class GitHubIssue(SphinxRole):
39+
ISSUE_URI = "https://github.com/python/cpython/issues/{0}"
40+
41+
def run(self) -> tuple[list[Element], list[nodes.system_message]]:
42+
issue = self.text
43+
44+
# sanity check: all GitHub issues have ID >= 32426
45+
# even though some of them are also valid BPO IDs
46+
if int(issue) < 32_426:
47+
msg = self.inliner.reporter.error(
48+
f"The GitHub ID {issue!r} seems too low. "
49+
"Use :issue:`...` for BPO IDs.",
50+
line=self.lineno,
51+
)
52+
prb = self.inliner.problematic(self.rawtext, self.rawtext, msg)
53+
return [prb], [msg]
54+
55+
issue_url = self.ISSUE_URI.format(issue)
56+
refnode = nodes.reference(issue, f"gh-{issue}", refuri=issue_url)
57+
self.set_source_info(refnode)
58+
return [refnode], []
59+
60+
61+
def setup(app: Sphinx) -> ExtensionMetadata:
62+
app.add_role("issue", BPOIssue())
63+
app.add_role("gh", GitHubIssue())
64+
65+
return {
66+
"version": "1.0",
67+
"parallel_read_safe": True,
68+
"parallel_write_safe": True,
69+
}

Doc/tools/extensions/pyspecific.py

-35
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
from sphinx.locale import _ as sphinx_gettext
2222
from sphinx.util.docutils import SphinxDirective
2323

24-
25-
ISSUE_URI = 'https://bugs.python.org/issue?@action=redirect&bpo=%s'
26-
GH_ISSUE_URI = 'https://github.com/python/cpython/issues/%s'
2724
# Used in conf.py and updated here by python/release-tools/run_release.py
2825
SOURCE_URI = 'https://github.com/python/cpython/tree/3.12/%s'
2926

@@ -34,36 +31,6 @@
3431
Body.enum.converters['lowerroman'] = \
3532
Body.enum.converters['upperroman'] = lambda x: None
3633

37-
# Support for marking up and linking to bugs.python.org issues
38-
39-
def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
40-
issue = unescape(text)
41-
# sanity check: there are no bpo issues within these two values
42-
if 47261 < int(issue) < 400000:
43-
msg = inliner.reporter.error(f'The BPO ID {text!r} seems too high -- '
44-
'use :gh:`...` for GitHub IDs', line=lineno)
45-
prb = inliner.problematic(rawtext, rawtext, msg)
46-
return [prb], [msg]
47-
text = 'bpo-' + issue
48-
refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue)
49-
return [refnode], []
50-
51-
52-
# Support for marking up and linking to GitHub issues
53-
54-
def gh_issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
55-
issue = unescape(text)
56-
# sanity check: all GitHub issues have ID >= 32426
57-
# even though some of them are also valid BPO IDs
58-
if int(issue) < 32426:
59-
msg = inliner.reporter.error(f'The GitHub ID {text!r} seems too low -- '
60-
'use :issue:`...` for BPO IDs', line=lineno)
61-
prb = inliner.problematic(rawtext, rawtext, msg)
62-
return [prb], [msg]
63-
text = 'gh-' + issue
64-
refnode = nodes.reference(text, text, refuri=GH_ISSUE_URI % issue)
65-
return [refnode], []
66-
6734

6835
class PyAwaitableMixin(object):
6936
def handle_signature(self, sig, signode):
@@ -160,8 +127,6 @@ def patch_pairindextypes(app, _env) -> None:
160127

161128

162129
def setup(app):
163-
app.add_role('issue', issue_role)
164-
app.add_role('gh', gh_issue_role)
165130
app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)
166131
app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)
167132
app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event)

0 commit comments

Comments
 (0)