10000 Don't auto-add inline links to ref section & rm if empty, per #2130 (… · python/peps@f82973d · GitHub
[go: up one dir, main page]

Skip to content

Commit f82973d

Browse files
Don't auto-add inline links to ref section & rm if empty, per #2130 (#2155)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 21f6df5 commit f82973d

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

pep2html.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@
4545
import random
4646
import time
4747
from io import open
48+
from pathlib import Path
4849
try:
4950
from html import escape
5051
except ImportError:
5152
from cgi import escape
5253

5354
from docutils import core, nodes, utils
5455
from docutils.readers import standalone
55-
from docutils.transforms import peps, frontmatter, Transform
56+
from docutils.transforms import frontmatter, peps, Transform
5657
from docutils.parsers import rst
5758

5859
class DataError(Exception):
@@ -433,6 +434,32 @@ def apply(self):
433434
elif name == 'version' and len(body):
434435
utils.clean_rcs_keywords(para, self.rcs_keyword_substitutions)
435436

437+
438+
class PEPFooter(Transform):
439+
"""Remove the References section if it is empty when rendered."""
440+
441+
# Uses same priority as docutils.transforms.TargetNotes
442+
default_priority = 520
443+
444+
def apply(self):
445+
pep_source_path = Path(self.document['source'])
446+
if not pep_source_path.match('pep-*'):
447+
return # not a PEP file, exit early
448+
449+
# Iterate through sections from the end of the document
450+
for section in reversed(self.document):
451+
if not isinstance(section, nodes.section):
452+
continue
453+
title_words = section[0].astext().lower().split()
454+
if 'references' in title_words:
455+
# Remove references section if there are no displayed
456+
# footnotes (it only has title & link target nodes)
457+
if all(isinstance(ref_node, (nodes.title, nodes.target))
458+
for ref_node in section):
459+
section.parent.remove(section)
460+
break
461+
462+
436463
class PEPReader(standalone.Reader):
437464

438465
supported = ('pep',)
@@ -453,7 +480,7 @@ def get_transforms(self):
453480
transforms.remove(frontmatter.DocTitle)
454481
transforms.remove(frontmatter.SectionSubTitle)
455482
transforms.remove(frontmatter.DocInfo)
456-
transforms.extend([PEPHeaders, peps.Contents, peps.TargetNotes])
483+
transforms.extend([PEPHeaders, peps.Contents, PEPFooter])
457484
return transforms
458485

459486
settings_default_overrides = {'pep_references': 1, 'rfc_references': 1}

pep_sphinx_extensions/pep_processor/transforms/pep_footer.py

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,16 @@
44

55
from docutils import nodes
66
from docutils import transforms
7-
from docutils.transforms import misc
8-
from docutils.transforms import references
97

108
from pep_sphinx_extensions import config
119

1210

1311
class PEPFooter(transforms.Transform):
1412
"""Footer transforms for PEPs.
1513
16-
- Appends external links to footnotes.
14+
- Removes the References section if it is empty when rendered.
1715
- Creates a link to the (GitHub) source text.
1816
19-
TargetNotes:
20-
Locate the `References` section, insert a placeholder at the end
21-
for an external target footnote insertion transform, and schedule
22-
the transform to run immediately.
23-
2417
Source Link:
2518
Create the link to the source file from the document source path,
2619
and append the text to the end of the document.
@@ -35,60 +28,25 @@ def apply(self) -> None:
3528
if not pep_source_path.match("pep-*"):
3629
return # not a PEP file, exit early
3730

38-
doc = self.document[0]
39-
reference_section = copyright_section = None
40-
4131
# Iterate through sections from the end of the document
42-
num_sections = len(doc)
43-
for i, section in enumerate(reversed(doc)):
32+
for section in reversed(self.document[0]):
4433
if not isinstance(section, nodes.section):
4534
continue
4635
title_words = section[0].astext().lower().split()
4736
if "references" in title_words:
48-
reference_section = section
37+
# Remove references section if there are no displayed
38+
# footnotes (it only has title & link target nodes)
39+
if all(isinstance(ref_node, (nodes.title, nodes.target))
40+
for ref_node in section):
41+
section.parent.remove(section)
4942
break
50-
elif "copyright" in title_words:
51-
copyright_section = num_sections - i - 1
52-
53-
# Add a references section if we didn't find one
54-
if not reference_section:
55-
reference_section = nodes.section()
56-
reference_section += nodes.title("", "References")
57-
self.document.set_id(reference_section)
58-
if copyright_section:
59-
# Put the new "References" section before "Copyright":
60-
doc.insert(copyright_section, reference_section)
61-
else:
62-
# Put the new "References" section at end of doc:
63-
doc.append(reference_section)
64-
65-
# Add and schedule execution of the TargetNotes transform
66-
pending = nodes.pending(references.TargetNotes)
67-
reference_section.append(pending)
68-
self.document.note_pending(pending, priority=0)
69-
70-
# If there are no references after TargetNotes has finished, remove the
71-
# references section
72-
pending = nodes.pending(misc.CallBack, details={"callback": _cleanup_callback})
73-
reference_section.append(pending)
74-
self.document.note_pending(pending, priority=1)
7543

7644
# Add link to source text and last modified date
7745
if pep_source_path.stem != "pep-0000":
7846
self.document += _add_source_link(pep_source_path)
7947
self.document += _add_commit_history_info(pep_source_path)
8048

8149

82-
def _cleanup_callback(pending: nodes.pending) -> None:
83-
"""Remove an empty "References" section.
84-
85-
Called after the `references.TargetNotes` transform is complete.
86-
87-
"""
88-
if len(< 5B07 span class=pl-s1>pending.parent) == 2: # <title> and <pending>
89-
pending.parent.parent.remove(pending.parent)
90-
91-
9250
def _add_source_link(pep_source_path: Path) -> nodes.paragraph:
9351
"""Add link to source text on VCS (GitHub)"""
9452
source_link = config.pep_vcs_url + pep_source_path.name

0 commit comments

Comments
 (0)
0