Skip to content

Commit 054286f

Browse files
authored
Infra: Move "Source" and "Last modified" from <article> to <footer> (#4977)
1 parent fc7b518 commit 054286f

5 files changed

Lines changed: 50 additions & 51 deletions

File tree

pep_sphinx_extensions/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
pep_parser,
2424
pep_role,
2525
)
26+
from pep_sphinx_extensions.pep_processor.transforms import pep_footer
2627
from pep_sphinx_extensions.pep_processor.transforms import pep_references
2728
from pep_sphinx_extensions.pep_zero_generator.pep_index_generator import create_pep_zero
2829

@@ -68,6 +69,9 @@ def set_description(
6869
else:
6970
context["description"] = "Python Enhancement Proposals (PEPs)"
7071

72+
if pagename != "pep-0000":
73+
context.update(pep_footer.get_page_footer_context(pagename))
74+
7175

7276
def setup(app: Sphinx) -> dict[str, bool]:
7377
"""Initialize Sphinx extension."""

pep_sphinx_extensions/pep_processor/transforms/pep_footer.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ class PEPFooter(transforms.Transform):
1010
"""Footer transforms for PEPs.
1111
1212
- Remove the References/Footnotes section if it is empty when rendered.
13-
- Create a link to the (GitHub) source text.
14-
15-
Source Link:
16-
Create the link to the source file from the document source path,
17-
and append the text to the end of the document.
18-
1913
"""
2014

2115
# Uses same priority as docutils.transforms.TargetNotes
@@ -44,31 +38,21 @@ def apply(self) -> None:
4438
section.parent.extend(to_hoist)
4539
section.parent.remove(section)
4640

47-
# Add link to source text and last modified date
48-
if pep_source_path.stem != "pep-0000":
49-
if pep_source_path.stem != "pep-0210": # 210 is entirely empty, skip
50-
self.document += nodes.transition()
51-
self.document += _add_source_link(pep_source_path)
52-
self.document += _add_commit_history_info(pep_source_path)
53-
54-
55-
def _add_source_link(pep_source_path: Path) -> nodes.paragraph:
56-
"""Add link to source text on VCS (GitHub)"""
57-
source_link = f"https://github.com/python/peps/blob/main/peps/{pep_source_path.name}"
58-
link_node = nodes.reference("", source_link, refuri=source_link)
59-
return nodes.paragraph("", "Source: ", link_node)
60-
61-
62-
def _add_commit_history_info(pep_source_path: Path) -> nodes.paragraph:
63-
"""Use local git history to find last modified date."""
64-
try:
65-
iso_time = _LAST_MODIFIED_TIMES[pep_source_path.stem]
66-
except KeyError:
67-
return nodes.paragraph()
6841

69-
commit_link = f"https://github.com/python/peps/commits/main/peps/{pep_source_path.name}"
70-
link_node = nodes.reference("", f"{iso_time} GMT", refuri=commit_link)
71-
return nodes.paragraph("", "Last modified: ", link_node)
42+
def get_page_footer_context(pep_stem: str) -> dict[str, str]:
43+
"""Template context for the page footer, rendered by ``page.html``."""
44+
context = {
45+
"source_link": (
46+
f"https://github.com/python/peps/blob/main/peps/{pep_stem}.rst"
47+
),
48+
}
49+
iso_time = _LAST_MODIFIED_TIMES.get(pep_stem, "")
50+
if iso_time:
51+
context["last_modified"] = iso_time
52+
context["commit_link"] = (
53+
f"https://github.com/python/peps/commits/main/peps/{pep_stem}.rst"
54+
)
55+
return context
7256

7357

7458
def _get_last_modified_timestamps():

pep_sphinx_extensions/pep_theme/static/mq.css

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@
3838
padding: 0.5rem 1rem 0;
3939
width: 100%;
4040
}
41-
section#pep-page-section > article {
41+
section#pep-page-section > article,
42+
section#pep-page-section > footer#pep-page-footer {
4243
max-width: 37em;
4344
width: 74%;
4445
float: right;
4546
margin-right: 0;
4647
font-size: 1rem;
4748
}
49+
section#pep-page-section > footer#pep-page-footer {
50+
clear: right;
51+
}
4852
nav#pep-sidebar {
4953
width: 24%;
5054
float: left;
@@ -61,7 +65,8 @@
6165
}
6266
}
6367
@media (min-width: 60em) {
64-
section#pep-page-section > article {
68+
section#pep-page-section > article,
69+
section#pep-page-section > footer#pep-page-footer {
6570
max-width: 56em;
6671
padding-left: 3.2%;
6772
padding-right: 3.2%;
@@ -158,7 +163,8 @@
158163
display: none;
159164
}
160165

161-
section#pep-page-section > article {
166+
section#pep-page-section > article,
167+
section#pep-page-section > footer#pep-page-footer {
162168
float: none;
163169
max-width: 100%;
164170
width: auto;

pep_sphinx_extensions/pep_theme/templates/page.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ <h2>Contents</h2>
7272
{%- endif %}
7373
</nav>
7474
{%- endif %}
75+
{%- if source_link %}
76+
<footer id="pep-page-footer">
77+
<p>Source: <a href="{{ source_link }}">{{ source_link }}</a></p>
78+
{%- if last_modified %}
79+
<p>Last modified: <a href="{{ commit_link }}">{{ last_modified }} UTC</a></p>
80+
{%- endif %}
81+
</footer>
82+
{%- endif %}
7583
</section>
7684
<script src="{{ pathto('_static/colour_scheme.js', resource=True) }}"></script>
7785
<script src="{{ pathto('_static/wrap_tables.js', resource=True) }}"></script>

pep_sphinx_extensions/tests/pep_processor/transform/test_pep_footer.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@
22

33
from pep_sphinx_extensions.pep_processor.transforms import pep_footer
44

5-
from ...conftest import PEP_ROOT
65

6+
def test_get_page_footer_context():
7+
out = pep_footer.get_page_footer_context("pep-0008")
78

8-
def test_add_source_link():
9-
out = pep_footer._add_source_link(PEP_ROOT / "pep-0008.rst")
10-
11-
assert "https://github.com/python/peps/blob/main/peps/pep-0008.rst" in str(out)
12-
13-
14-
def test_add_commit_history_info():
15-
out = pep_footer._add_commit_history_info(PEP_ROOT / "pep-0008.rst")
16-
17-
assert str(out).startswith(
18-
"<paragraph>Last modified: "
19-
'<reference refuri="https://github.com/python/peps/commits/main/peps/pep-0008.rst">'
9+
assert out["source_link"] == (
10+
"https://github.com/python/peps/blob/main/peps/pep-0008.rst"
11+
)
12+
assert out["commit_link"] == (
13+
"https://github.com/python/peps/commits/main/peps/pep-0008.rst"
2014
)
21-
# A variable timestamp comes next, don't test that
22-
assert str(out).endswith("</reference></paragraph>")
15+
# A variable timestamp, don't test the exact value
16+
assert out["last_modified"]
2317

2418

25-
def test_add_commit_history_info_invalid():
26-
out = pep_footer._add_commit_history_info(PEP_ROOT / "pep-not-found.rst")
19+
def test_get_page_footer_context_no_history():
20+
out = pep_footer.get_page_footer_context("pep-not-found")
2721

28-
assert str(out) == "<paragraph/>"
22+
# No git history -> only the static source link
23+
assert out == {
24+
"source_link": "https://github.com/python/peps/blob/main/peps/pep-not-found.rst",
25+
}
2926

3027

3128
def test_get_last_modified_timestamps():

0 commit comments

Comments
 (0)