4
4
5
5
from docutils import nodes
6
6
from docutils import transforms
7
- from docutils .transforms import misc
8
- from docutils .transforms import references
9
7
10
8
from pep_sphinx_extensions import config
11
9
12
10
13
11
class PEPFooter (transforms .Transform ):
14
12
"""Footer transforms for PEPs.
15
13
16
- - Appends external links to footnotes .
14
+ - Removes the References section if it is empty when rendered .
17
15
- Creates a link to the (GitHub) source text.
18
16
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
-
24
17
Source Link:
25
18
Create the link to the source file from the document source path,
26
19
and append the text to the end of the document.
@@ -35,60 +28,25 @@ def apply(self) -> None:
35
28
if not pep_source_path .match ("pep-*" ):
36
29
return # not a PEP file, exit early
37
30
38
- doc = self .document [0 ]
39
- reference_section = copyright_section = None
40
-
41
31
# 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 ]):
44
33
if not isinstance (section , nodes .section ):
45
34
continue
46
35
title_words = section [0 ].astext ().lower ().split ()
47
36
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 )
49
42
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 )
75
43
76
44
# Add link to source text and last modified date
77
45
if pep_source_path .stem != "pep-0000" :
78
46
self .document += _add_source_link (pep_source_path )
79
47
self .document += _add_commit_history_info (pep_source_path )
80
48
81
49
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
-
92
50
def _add_source_link (pep_source_path : Path ) -> nodes .paragraph :
93
51
"""Add link to source text on VCS (GitHub)"""
94
52
source_link = config .pep_vcs_url + pep_source_path .name
0 commit comments