11import datetime
22import email .utils
33from pathlib import Path
4- import re
54
6- from dateutil import parser
7- import docutils .frontend
8- import docutils .nodes
9- import docutils .parsers .rst
10- import docutils .utils
5+ from docutils import frontend
6+ from docutils import nodes
7+ from docutils import utils
8+ from docutils .parsers import rst
119from feedgen import entry
1210from feedgen import feed
1311
@@ -44,37 +42,26 @@ def first_line_starting_with(full_path: Path, text: str) -> str:
4442
4543def pep_creation (full_path : Path ) -> datetime .datetime :
4644 created_str = first_line_starting_with (full_path , "Created:" )
47- # bleh, I was hoping to avoid re but some PEPs editorialize on the Created line
48- # (note as of Aug 2020 only PEP 102 has additional content on the Created line)
49- m = re .search (r"(\d+[- ][\w\d]+[- ]\d{2,4})" , created_str )
50- if not m :
51- # some older ones have an empty line, that's okay, if it's old we ipso facto don't care about it.
52- # "return None" would make the most sense but datetime objects refuse to compare with that. :-|
53- return datetime .datetime (1900 , 1 , 1 )
54- created_str = m .group (1 )
55- try :
56- return parser .parse (created_str , dayfirst = True )
57- except (ValueError , OverflowError ):
58- return datetime .datetime (1900 , 1 , 1 )
59-
60-
61- def parse_rst (text : str ) -> docutils .nodes .document :
62- rst_parser = docutils .parsers .rst .Parser ()
63- components = (docutils .parsers .rst .Parser ,)
64- settings = docutils .frontend .OptionParser (components = components ).get_default_values ()
65- document = docutils .utils .new_document ('<rst-doc>' , settings = settings )
66- rst_parser .parse (text , document )
45+ if full_path .stem == "pep-0102" :
46+ # remove additional content on the Created line
47+ created_str = created_str .split (" " , 1 )[0 ]
48+ return datetime .datetime .strptime (created_str , "%d-%b-%Y" )
49+
50+
51+ def parse_rst (text : str ) -> nodes .document :
52+ settings = frontend .OptionParser ((rst .Parser ,)).get_default_values ()
53+ document = utils .new_document ('<rst-doc>' , settings = settings )
54+ rst .Parser ().parse (text , document )
6755 return document
6856
6957
7058def pep_abstract (full_path : Path ) -> str :
7159 """Return the first paragraph of the PEP abstract"""
7260 text = full_path .read_text (encoding = "utf-8" )
73- for node in parse_rst (text ):
74- if "<title>Abstract</title>" in str (node ):
75- for child in node :
76- if child .tagname == "paragraph" :
77- return child .astext ().strip ().replace ("\n " , " " )
61+ # TODO replace .traverse with .findall when Sphinx updates to docutils>=0.18.1
62+ for node in parse_rst (text ).traverse (nodes .section ):
63+ if node .next_node (nodes .title ).astext () == "Abstract" :
64+ return node .next_node (nodes .paragraph ).astext ().strip ().replace ("\n " , " " )
7865 return ""
7966
8067
@@ -119,7 +106,7 @@ def main():
119106 Newest Python Enhancement Proposals (PEPs) - Information on new
120107 language features, and some meta-information like release
121108 procedure and schedules.
122- """ . replace ( " \n " , " " ). strip ()
109+ """
123110
124111 # Setup feed generator
125112 fg = feed .FeedGenerator ()
@@ -131,7 +118,7 @@ def main():
131118 fg .title ("Newest Python PEPs" )
132119 fg .link (href = "https://www.python.org/dev/peps" )
133120 fg .link (href = "https://www.python.org/dev/peps/peps.rss" , rel = "self" )
134- fg .description (desc )
121+ fg .description (" " . join ( desc . split ()) )
135122 fg .lastBuildDate (datetime .datetime .utcnow ().replace (tzinfo = datetime .timezone .utc ))
136123
137124 # Add PEP information (ordered by newest first)
0 commit comments