Skip to content

Commit

Permalink
Further simplify footer references section cleanup code
Browse files Browse the repository at this point in the history
Co-Authored-By: Adam Turner <[email protected]>
  • Loading branch information
CAM-Gerlach and AA-Turner committed Nov 24, 2021
1 parent 129ecb2 commit 5788d8a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 46 deletions.
34 changes: 10 additions & 24 deletions pep2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

from docutils import core, nodes, utils
from docutils.readers import standalone
from docutils.transforms import frontmatter, misc, peps, Transform
from docutils.transforms import frontmatter, peps, Transform
from docutils.parsers import rst

class DataError(Exception):
Expand Down Expand Up @@ -442,37 +442,23 @@ class PEPFooter(Transform):
default_priority = 520

def apply(self):
pep_source_path = Path(self.document["source"])
if not pep_source_path.match("pep-*"):
pep_source_path = Path(self.document['source'])
if not pep_source_path.match('pep-*'):
return # not a PEP file, exit early

doc = self.document
reference_section = None

# Iterate through sections from the end of the document
for i, section in enumerate(reversed(doc)):
for section in reversed(self.document):
if not isinstance(section, nodes.section):
continue
title_words = section[0].astext().lower().split()
if "references" in title_words:
reference_section = section
if 'references' in title_words:
# Remove references section if there are no displayed
# footnotes (it only has title & link target nodes)
if all(isinstance(ref_node, (nodes.title, nodes.target))
for ref_node in section):
section.parent.remove(section)
break

# Remove references section if there are no displayed footnotes
if reference_section:
pending = nodes.pending(
misc.CallBack, details={"callback": _cleanup_callback})
reference_section.append(pending)
self.document.note_pending(pending, priority=1)


def _cleanup_callback(pending):
"""Remove an empty "References" section."""
for footer_node in pending.parent:
if isinstance(footer_node, (nodes.title, nodes.target, nodes.pending)):
return
pending.parent.parent.remove(pending.parent)


class PEPReader(standalone.Reader):

Expand Down
28 changes: 6 additions & 22 deletions pep_sphinx_extensions/pep_processor/transforms/pep_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from docutils import nodes
from docutils import transforms
from docutils.transforms import misc

from pep_sphinx_extensions import config

Expand All @@ -29,40 +28,25 @@ def apply(self) -> None:
if not pep_source_path.match("pep-*"):
return # not a PEP file, exit early

doc = self.document[0]
reference_section = None

# Iterate through sections from the end of the document
for i, section in enumerate(reversed(doc)):
for section in reversed(self.document[0]):
if not isinstance(section, nodes.section):
continue
title_words = section[0].astext().lower().split()
if "references" in title_words:
reference_section = section
# Remove references section if there are no displayed
# footnotes (it only has title & link target nodes)
if all(isinstance(ref_node, (nodes.title, nodes.target))
for ref_node in section):
section.parent.remove(section)
break

# Remove references section if there are no displayed footnotes
if reference_section:
pending = nodes.pending(misc.CallBack, details={"callback": _cleanup_callback})
reference_section.append(pending)
self.document.note_pending(pending, priority=1)

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


def _cleanup_callback(pending: nodes.pending) -> None:
"""Remove an empty "References" section."""
for ref_node in pending.parent:
# Don't remove section if has more than title, link targets and pending
if not isinstance(
ref_node, (nodes.title, nodes.target, nodes.pending)):
return
pending.parent.parent.remove(pending.parent)


def _add_source_link(pep_source_path: Path) -> nodes.paragraph:
"""Add link to source text on VCS (GitHub)"""
source_link = config.pep_vcs_url + pep_source_path.name
Expand Down

0 comments on commit 5788d8a

Please sign in to comment.