fixfile(): Added special casing to handle the new PEP 0 format, which

eliminates the "filename" field in favor of just the unadorned PEP
    number.
This commit is contained in:
Barry Warsaw 2001-08-14 16:45:19 +00:00
parent f603ea417a
commit c673087bfc
1 changed files with 59 additions and 36 deletions

View File

@ -57,12 +57,9 @@ fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|"
def usage(code, msg=''): def usage(code, msg=''):
sys.stderr.write(__doc__ % globals() + '\n') print >> sys.stderr, __docs__ % globals()
if msg: if msg:
msg = str(msg) print >> sys.stderr, msg
if msg[-1] <> '\n':
msg = msg + '\n'
sys.stderr.write(msg)
sys.exit(code) sys.exit(code)
@ -81,12 +78,13 @@ def fixanchor(current, match):
rfcnum = int(match.group('rfcnum')) rfcnum = int(match.group('rfcnum'))
link = RFCURL % rfcnum link = RFCURL % rfcnum
if link: if link:
return "<a href='%s'>%s</a>" % (link, cgi.escape(text)) return '<a href="%s">%s</a>' % (link, cgi.escape(text))
return cgi.escape(match.group(0)) # really slow, but it works... return cgi.escape(match.group(0)) # really slow, but it works...
def fixfile(infile, outfile): def fixfile(infile, outfile):
basename = os.path.basename(infile)
# convert plain text pep to minimal XHTML markup # convert plain text pep to minimal XHTML markup
try: try:
fi = open(infile) fi = open(infile)
@ -95,7 +93,9 @@ def fixfile(infile, outfile):
print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename
return return
fo = open(outfile, "w") fo = open(outfile, "w")
fo.write(DTD + "\n<html>\n<head>\n") print >> fo, DTD
print >> fo, '<html>'
print >> fo, '<head>'
# head # head
header = [] header = []
pep = "" pep = ""
@ -122,19 +122,18 @@ def fixfile(infile, outfile):
if pep: if pep:
title = "PEP " + pep + " -- " + title title = "PEP " + pep + " -- " + title
if title: if title:
fo.write(" <title>%s</title>\n" print >> fo, ' <title>%s</title>' % cgi.escape(title)
' <link rel="STYLESHEET" href="style.css">\n' print >> fo, ' <link rel="STYLESHEET" href="style.css">'
% cgi.escape(title)) print >> fo, '</head>'
fo.write("</head>\n")
# body # body
fo.write('<body bgcolor="white">\n' print >> fo, '<body bgcolor="white">'
'<div class="navigation">\n') print >> fo, '<div class="navigation">'
fo.write('[<b><a href="../">home</a></b>]\n') print >> fo, '[<b><a href="../">home</a></b>]'
if os.path.basename(infile) != "pep-0000.txt": if basename <> 'pep-0000.txt':
fo.write('[<b><a href=".">index</a></b>]\n') print >> fo, '[<b><a href=".">index</a></b>]'
fo.write('[<b><a href="pep-%04d.txt">PEP source</a></b>]\n' % int(pep)) print >> fo, '[<b><a href="pep-%04d.txt">PEP source</a></b>]' % int(pep)
fo.write('</div>\n' print >> fo, '</div>'
'<div class="header">\n<table border="0">\n') print >> fo, '<div class="header">\n<table border="0">'
for k, v in header: for k, v in header:
if k.lower() in ('author', 'discussions-to'): if k.lower() in ('author', 'discussions-to'):
mailtos = [] mailtos = []
@ -157,30 +156,53 @@ def fixfile(infile, outfile):
v = peps v = peps
else: else:
v = cgi.escape(v) v = cgi.escape(v)
fo.write(" <tr><th align='right'>%s:</th><td>%s</td></tr>\n" print >> fo, ' <tr><th align="right">%s:</th><td>%s</td></tr>' % (
% (cgi.escape(k), v)) cgi.escape(k), v)
title = 0 print >> fo, '</table>'
fo.write("</table>\n</div>\n<hr />\n" print >> fo, '</div>'
"<pre>") print >> fo, '<hr />'
print >> fo, '<pre>'
while 1: while 1:
line = fi.readline() line = fi.readline()
if not line: if not line:
break break
if line[0] != "\f": if line[0] == '\f':
if line[0].strip(): continue
if line.strip() == LOCALVARS: if line.strip() == LOCALVARS:
break break
fo.write("</pre>\n<h3>%s</h3>\n<pre>" % line.strip()) if line[0].strip():
title = 0 if line.strip() == LOCALVARS:
else: break
line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line) print >> fo, '</pre>'
fo.write(line) print >> fo, '<h3>%s</h3>' % line.strip()
fo.write("</pre>\n" print >> fo, '<pre>',
"</body>\n" else:
"</html>\n") # PEP 0 has some special treatment
if basename == 'pep-0000.txt':
parts = line.split()
if len(parts) > 1 and re.match(r'\s*\d{1,4}', parts[1]):
# This is a PEP summary line, which we need to hyperlink
url = PEPURL % int(parts[1])
print >> fo, re.sub(
parts[1],
'<a href="%s">%s</a>' % (url, parts[1]),
line, 1),
continue
elif parts and '@' in parts[-1]:
# This is a pep email address line, so hyperlink it
url = '<a href="mailto:%s">%s</a>' % (parts[-1], parts[-1])
print >> fo, re.sub(
parts[-1], url, line, 1),
continue
line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line)
fo.write(line)
print >> fo, '</pre>'
print >> fo, '</body>'
print >> fo, '</html>'
fo.close() fo.close()
os.chmod(outfile, 0664) os.chmod(outfile, 0664)
def find_pep(pep_str): def find_pep(pep_str):
"""Find the .txt file indicated by a cmd line argument""" """Find the .txt file indicated by a cmd line argument"""
@ -257,6 +279,7 @@ def main():
if update: if update:
push_pep(html, peptxt, username, verbose) push_pep(html, peptxt, username, verbose)
if __name__ == "__main__": if __name__ == "__main__":
main() main()