diff --git a/pep2html.py b/pep2html.py index 760bc62fc..ec6e76b6d 100755 --- a/pep2html.py +++ b/pep2html.py @@ -57,12 +57,9 @@ fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|" def usage(code, msg=''): - sys.stderr.write(__doc__ % globals() + '\n') + print >> sys.stderr, __docs__ % globals() if msg: - msg = str(msg) - if msg[-1] <> '\n': - msg = msg + '\n' - sys.stderr.write(msg) + print >> sys.stderr, msg sys.exit(code) @@ -81,12 +78,13 @@ def fixanchor(current, match): rfcnum = int(match.group('rfcnum')) link = RFCURL % rfcnum if link: - return "%s" % (link, cgi.escape(text)) + return '%s' % (link, cgi.escape(text)) return cgi.escape(match.group(0)) # really slow, but it works... def fixfile(infile, outfile): + basename = os.path.basename(infile) # convert plain text pep to minimal XHTML markup try: fi = open(infile) @@ -95,7 +93,9 @@ def fixfile(infile, outfile): print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename return fo = open(outfile, "w") - fo.write(DTD + "\n\n
\n") + print >> fo, DTD + print >> fo, '' + print >> fo, '' # head header = [] pep = "" @@ -122,19 +122,18 @@ def fixfile(infile, outfile): if pep: title = "PEP " + pep + " -- " + title if title: - fo.write("%s: | %s |
---|
") + print >> fo, '' % ( + cgi.escape(k), v) + print >> fo, ' %s: %s
' while 1: line = fi.readline() if not line: break - if line[0] != "\f": - if line[0].strip(): - if line.strip() == LOCALVARS: - break - fo.write("\n
" % line.strip()) - title = 0 - else: - line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line) - fo.write(line) - fo.write("\n" - "\n" - "\n") + if line[0] == '\f': + continue + if line.strip() == LOCALVARS: + break + if line[0].strip(): + if line.strip() == LOCALVARS: + break + print >> fo, '' + print >> fo, '
', + else: + # 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], + '%s' % (url, parts[1]), + line, 1), + continue + elif parts and '@' in parts[-1]: + # This is a pep email address line, so hyperlink it + url = '%s' % (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, '' + print >> fo, '' + print >> fo, '' fo.close() os.chmod(outfile, 0664) + def find_pep(pep_str): """Find the .txt file indicated by a cmd line argument""" @@ -257,6 +279,7 @@ def main(): if update: push_pep(html, peptxt, username, verbose) + if __name__ == "__main__": main()