-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infra: Add new directives to point to PEP's canonical content (#2702)
As originally discussed in #2692 , this adds a three new custom directives intended to be used at the top of PEPs: * `pep-banner`, a generic admonition banner that can be subclassed with an arbitrary message, * `canonical-docs`, which renders a banner linking to a `Final` PEP's canonical documentation/specification. * `canonical-pypa-spec`, a banner for packaging interoperability PEPs linking to the canonical PyPA specs. Co-authored-by: Hugo van Kemenade <[email protected]>
- Loading branch information
1 parent
d31806f
commit f87de82
Showing
4 changed files
with
184 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
pep_sphinx_extensions/pep_processor/parsing/pep_banner_directive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
"""Roles to insert custom admonitions pointing readers to canonical content.""" | ||
|
||
from __future__ import annotations | ||
|
||
from docutils import nodes | ||
from docutils.parsers import rst | ||
|
||
|
||
PYPA_SPEC_BASE_URL = "https://packaging.python.org/en/latest/specifications/" | ||
|
||
|
||
class PEPBanner(rst.Directive): | ||
"""Insert a special banner admonition in a PEP document.""" | ||
|
||
has_content = True | ||
required_arguments = 0 | ||
optional_arguments = 1 | ||
final_argument_whitespace = True | ||
option_spec = {} | ||
|
||
admonition_pre_template = "" | ||
admonition_pre_text = "" | ||
admonition_post_text = "" | ||
|
||
admonition_class = nodes.important | ||
css_classes = ["pep-banner"] | ||
|
||
|
||
def run(self) -> list[nodes.admonition]: | ||
|
||
if self.arguments: | ||
link_content = self.arguments[0] | ||
pre_text = self.admonition_pre_template.format( | ||
link_content=link_content) | ||
else: | ||
pre_text = self.admonition_pre_text | ||
|
||
pre_text_node = nodes.paragraph(pre_text) | ||
pre_text_node.line = self.lineno | ||
pre_node, pre_msg = self.state.inline_text(pre_text, self.lineno) | ||
pre_text_node.extend(pre_node + pre_msg) | ||
|
||
post_text = self.admonition_post_text | ||
post_text_node = nodes.paragraph(post_text) | ||
post_text_node.line = self.lineno | ||
post_node, post_msg = self.state.inline_text(post_text, self.lineno) | ||
post_text_node.extend(post_node + post_msg) | ||
|
||
source_lines = [pre_text] + list(self.content or []) + [post_text] | ||
admonition_node = self.admonition_class( | ||
"\n".join(source_lines), classes=self.css_classes) | ||
|
||
admonition_node.append(pre_text_node) | ||
if self.content: | ||
self.state.nested_parse( | ||
self.content, self.content_offset, admonition_node) | ||
admonition_node.append(post_text_node) | ||
|
||
return [admonition_node] | ||
|
||
|
||
class CanonicalDocBanner(PEPBanner): | ||
"""Insert an admonition pointing readers to a PEP's canonical docs.""" | ||
|
||
admonition_pre_template = ( | ||
"This PEP is a historical document. " | ||
"The up-to-date, canonical documentation can now be found " | ||
"at {link_content}." | ||
) | ||
admonition_pre_text = ( | ||
"This PEP is a historical document. " | ||
"The up-to-date, canonical documentation can now be found elsewhere." | ||
) | ||
admonition_post_text = ( | ||
"See :pep:`1` for how to propose changes." | ||
) | ||
|
||
css_classes = [*PEPBanner.css_classes, "canonical-doc"] | ||
|
||
|
||
|
||
class CanonicalPyPASpecBanner(PEPBanner): | ||
"""Insert a specialized admonition for PyPA packaging specifications.""" | ||
|
||
admonition_pre_template = ( | ||
"This PEP is a historical document. " | ||
"the up-to-date, canonical spec, {link_content}, is maintained on " | ||
f"the `PyPA specs page <{PYPA_SPEC_BASE_URL}>`__." | ||
) | ||
admonition_pre_text = ( | ||
"This PEP is a historical document. " | ||
"The up-to-date, canonical specification is maintained on " | ||
f"the `PyPA specs page <{PYPA_SPEC_BASE_URL}>`__." | ||
) | ||
admonition_post_text = ( | ||
"See the `PyPA specification update process " | ||
"<https://www.pypa.io/en/latest/specifications/#handling-fixes-and-other-minor-updates>`__ " | ||
"for how to propose changes." | ||
) | ||
|
||
css_classes = [*PEPBanner.css_classes, "canonical-pypa-spec"] |