Port pep2html.py to 2/3.
This is a fairly naive port, just ran 2to3 over it and then tweaked enough to make both versions happy.
This commit is contained in:
parent
d7aefb884c
commit
c1bc6693ac
123
pep2html.py
123
pep2html.py
|
@ -34,6 +34,8 @@ Options:
|
||||||
The optional arguments ``peps`` are either pep numbers or .txt files.
|
The optional arguments ``peps`` are either pep numbers or .txt files.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import print_function, unicode_literals
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -43,8 +45,9 @@ import getopt
|
||||||
import errno
|
import errno
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
from io import open
|
||||||
|
|
||||||
REQUIRES = {'python': '2.2',
|
REQUIRES = {'python': '2.6',
|
||||||
'docutils': '0.2.7'}
|
'docutils': '0.2.7'}
|
||||||
PROGRAM = sys.argv[0]
|
PROGRAM = sys.argv[0]
|
||||||
RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html'
|
RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html'
|
||||||
|
@ -85,9 +88,9 @@ def usage(code, msg=''):
|
||||||
out = sys.stdout
|
out = sys.stdout
|
||||||
else:
|
else:
|
||||||
out = sys.stderr
|
out = sys.stderr
|
||||||
print >> out, __doc__ % globals()
|
print(__doc__ % globals(), file=out)
|
||||||
if msg:
|
if msg:
|
||||||
print >> out, msg
|
print(msg, file=out)
|
||||||
sys.exit(code)
|
sys.exit(code)
|
||||||
|
|
||||||
|
|
||||||
|
@ -105,7 +108,7 @@ def fixanchor(current, match):
|
||||||
ltext.append(c)
|
ltext.append(c)
|
||||||
break
|
break
|
||||||
link = EMPTYSTRING.join(ltext)
|
link = EMPTYSTRING.join(ltext)
|
||||||
elif text.startswith('pep-') and text <> current:
|
elif text.startswith('pep-') and text != current:
|
||||||
link = os.path.splitext(text)[0] + ".html"
|
link = os.path.splitext(text)[0] + ".html"
|
||||||
elif text.startswith('PEP'):
|
elif text.startswith('PEP'):
|
||||||
pepnum = int(match.group('pepnum'))
|
pepnum = int(match.group('pepnum'))
|
||||||
|
@ -143,14 +146,17 @@ def linkemail(address, pepno):
|
||||||
|
|
||||||
|
|
||||||
def fixfile(inpath, input_lines, outfile):
|
def fixfile(inpath, input_lines, outfile):
|
||||||
from email.Utils import parseaddr
|
try:
|
||||||
|
from email.Utils import parseaddr
|
||||||
|
except ImportError:
|
||||||
|
from email.utils import parseaddr
|
||||||
basename = os.path.basename(inpath)
|
basename = os.path.basename(inpath)
|
||||||
infile = iter(input_lines)
|
infile = iter(input_lines)
|
||||||
# convert plaintext pep to minimal XHTML markup
|
# convert plaintext pep to minimal XHTML markup
|
||||||
print >> outfile, DTD
|
print(DTD, file=outfile)
|
||||||
print >> outfile, '<html>'
|
print('<html>', file=outfile)
|
||||||
print >> outfile, COMMENT
|
print(COMMENT, file=outfile)
|
||||||
print >> outfile, '<head>'
|
print('<head>', file=outfile)
|
||||||
# head
|
# head
|
||||||
header = []
|
header = []
|
||||||
pep = ""
|
pep = ""
|
||||||
|
@ -176,9 +182,9 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
if pep:
|
if pep:
|
||||||
title = "PEP " + pep + " -- " + title
|
title = "PEP " + pep + " -- " + title
|
||||||
if title:
|
if title:
|
||||||
print >> outfile, ' <title>%s</title>' % cgi.escape(title)
|
print(' <title>%s</title>' % cgi.escape(title), file=outfile)
|
||||||
r = random.choice(range(64))
|
r = random.choice(list(range(64)))
|
||||||
print >> outfile, (
|
print((
|
||||||
' <link rel="STYLESHEET" href="style.css" type="text/css" />\n'
|
' <link rel="STYLESHEET" href="style.css" type="text/css" />\n'
|
||||||
'</head>\n'
|
'</head>\n'
|
||||||
'<body bgcolor="white">\n'
|
'<body bgcolor="white">\n'
|
||||||
|
@ -189,18 +195,18 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
'<img src="../pics/PyBanner%03d.gif" alt="[Python]"\n'
|
'<img src="../pics/PyBanner%03d.gif" alt="[Python]"\n'
|
||||||
' border="0" width="150" height="35" /></a></td>\n'
|
' border="0" width="150" height="35" /></a></td>\n'
|
||||||
'<td class="textlinks" align="left">\n'
|
'<td class="textlinks" align="left">\n'
|
||||||
'[<b><a href="../">Python Home</a></b>]' % r)
|
'[<b><a href="../">Python Home</a></b>]' % r), file=outfile)
|
||||||
if basename <> 'pep-0000.txt':
|
if basename != 'pep-0000.txt':
|
||||||
print >> outfile, '[<b><a href=".">PEP Index</a></b>]'
|
print('[<b><a href=".">PEP Index</a></b>]', file=outfile)
|
||||||
if pep:
|
if pep:
|
||||||
try:
|
try:
|
||||||
print >> outfile, ('[<b><a href="pep-%04d.txt">PEP Source</a>'
|
print(('[<b><a href="pep-%04d.txt">PEP Source</a>'
|
||||||
'</b>]' % int(pep))
|
'</b>]' % int(pep)), file=outfile)
|
||||||
except ValueError, error:
|
except ValueError as error:
|
||||||
print >> sys.stderr, ('ValueError (invalid PEP number): %s'
|
print(('ValueError (invalid PEP number): %s'
|
||||||
% error)
|
% error), file=sys.stderr)
|
||||||
print >> outfile, '</td></tr></table>'
|
print('</td></tr></table>', file=outfile)
|
||||||
print >> outfile, '<div class="header">\n<table border="0">'
|
print('<div class="header">\n<table border="0">', file=outfile)
|
||||||
for k, v in header:
|
for k, v in header:
|
||||||
if k.lower() in ('author', 'bdfl-delegate', 'discussions-to'):
|
if k.lower() in ('author', 'bdfl-delegate', 'discussions-to'):
|
||||||
mailtos = []
|
mailtos = []
|
||||||
|
@ -236,7 +242,7 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
try:
|
try:
|
||||||
url = PEPCVSURL % int(pep)
|
url = PEPCVSURL % int(pep)
|
||||||
v = '<a href="%s">%s</a> ' % (url, cgi.escape(date))
|
v = '<a href="%s">%s</a> ' % (url, cgi.escape(date))
|
||||||
except ValueError, error:
|
except ValueError as error:
|
||||||
v = date
|
v = date
|
||||||
elif k.lower() in ('content-type',):
|
elif k.lower() in ('content-type',):
|
||||||
url = PEPURL % 9
|
url = PEPURL % 9
|
||||||
|
@ -247,12 +253,12 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
v = cgi.escape(v[11:-2])
|
v = cgi.escape(v[11:-2])
|
||||||
else:
|
else:
|
||||||
v = cgi.escape(v)
|
v = cgi.escape(v)
|
||||||
print >> outfile, ' <tr><th>%s: </th><td>%s</td></tr>' \
|
print(' <tr><th>%s: </th><td>%s</td></tr>' \
|
||||||
% (cgi.escape(k), v)
|
% (cgi.escape(k), v), file=outfile)
|
||||||
print >> outfile, '</table>'
|
print('</table>', file=outfile)
|
||||||
print >> outfile, '</div>'
|
print('</div>', file=outfile)
|
||||||
print >> outfile, '<hr />'
|
print('<hr />', file=outfile)
|
||||||
print >> outfile, '<div class="content">'
|
print('<div class="content">', file=outfile)
|
||||||
need_pre = 1
|
need_pre = 1
|
||||||
for line in infile:
|
for line in infile:
|
||||||
if line[0] == '\f':
|
if line[0] == '\f':
|
||||||
|
@ -261,8 +267,8 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
break
|
break
|
||||||
if line[0].strip():
|
if line[0].strip():
|
||||||
if not need_pre:
|
if not need_pre:
|
||||||
print >> outfile, '</pre>'
|
print('</pre>', file=outfile)
|
||||||
print >> outfile, '<h3>%s</h3>' % line.strip()
|
print('<h3>%s</h3>' % line.strip(), file=outfile)
|
||||||
need_pre = 1
|
need_pre = 1
|
||||||
elif not line.strip() and need_pre:
|
elif not line.strip() and need_pre:
|
||||||
continue
|
continue
|
||||||
|
@ -274,32 +280,32 @@ def fixfile(inpath, input_lines, outfile):
|
||||||
# This is a PEP summary line, which we need to hyperlink
|
# This is a PEP summary line, which we need to hyperlink
|
||||||
url = PEPURL % int(parts[1])
|
url = PEPURL % int(parts[1])
|
||||||
if need_pre:
|
if need_pre:
|
||||||
print >> outfile, '<pre>'
|
print('<pre>', file=outfile)
|
||||||
need_pre = 0
|
need_pre = 0
|
||||||
print >> outfile, re.sub(
|
print(re.sub(
|
||||||
parts[1],
|
parts[1],
|
||||||
'<a href="%s">%s</a>' % (url, parts[1]),
|
'<a href="%s">%s</a>' % (url, parts[1]),
|
||||||
line, 1),
|
line, 1), end=' ', file=outfile)
|
||||||
continue
|
continue
|
||||||
elif parts and '@' in parts[-1]:
|
elif parts and '@' in parts[-1]:
|
||||||
# This is a pep email address line, so filter it.
|
# This is a pep email address line, so filter it.
|
||||||
url = fixemail(parts[-1], pep)
|
url = fixemail(parts[-1], pep)
|
||||||
if need_pre:
|
if need_pre:
|
||||||
print >> outfile, '<pre>'
|
print('<pre>', file=outfile)
|
||||||
need_pre = 0
|
need_pre = 0
|
||||||
print >> outfile, re.sub(
|
print(re.sub(
|
||||||
parts[-1], url, line, 1),
|
parts[-1], url, line, 1), end=' ', file=outfile)
|
||||||
continue
|
continue
|
||||||
line = fixpat.sub(lambda x, c=inpath: fixanchor(c, x), line)
|
line = fixpat.sub(lambda x, c=inpath: fixanchor(c, x), line)
|
||||||
if need_pre:
|
if need_pre:
|
||||||
print >> outfile, '<pre>'
|
print('<pre>', file=outfile)
|
||||||
need_pre = 0
|
need_pre = 0
|
||||||
outfile.write(line)
|
outfile.write(line)
|
||||||
if not need_pre:
|
if not need_pre:
|
||||||
print >> outfile, '</pre>'
|
print('</pre>', file=outfile)
|
||||||
print >> outfile, '</div>'
|
print('</div>', file=outfile)
|
||||||
print >> outfile, '</body>'
|
print('</body>', file=outfile)
|
||||||
print >> outfile, '</html>'
|
print('</html>', file=outfile)
|
||||||
|
|
||||||
|
|
||||||
docutils_settings = None
|
docutils_settings = None
|
||||||
|
@ -320,7 +326,7 @@ def fix_rst_pep(inpath, input_lines, outfile):
|
||||||
settings=docutils_settings,
|
settings=docutils_settings,
|
||||||
# Allow Docutils traceback if there's an exception:
|
# Allow Docutils traceback if there's an exception:
|
||||||
settings_overrides={'traceback': 1})
|
settings_overrides={'traceback': 1})
|
||||||
outfile.write(output)
|
outfile.write(output.decode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
def get_pep_type(input_lines):
|
def get_pep_type(input_lines):
|
||||||
|
@ -345,10 +351,10 @@ def get_pep_type(input_lines):
|
||||||
|
|
||||||
def get_input_lines(inpath):
|
def get_input_lines(inpath):
|
||||||
try:
|
try:
|
||||||
infile = open(inpath)
|
infile = open(inpath, encoding='utf-8')
|
||||||
except IOError, e:
|
except IOError as e:
|
||||||
if e.errno <> errno.ENOENT: raise
|
if e.errno != errno.ENOENT: raise
|
||||||
print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename
|
print('Error: Skipping missing PEP file:', e.filename, file=sys.stderr)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
return None
|
return None
|
||||||
lines = infile.read().splitlines(1) # handles x-platform line endings
|
lines = infile.read().splitlines(1) # handles x-platform line endings
|
||||||
|
@ -369,12 +375,12 @@ def make_html(inpath, verbose=0):
|
||||||
return None
|
return None
|
||||||
pep_type = get_pep_type(input_lines)
|
pep_type = get_pep_type(input_lines)
|
||||||
if pep_type is None:
|
if pep_type is None:
|
||||||
print >> sys.stderr, 'Error: Input file %s is not a PEP.' % inpath
|
print('Error: Input file %s is not a PEP.' % inpath, file=sys.stderr)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
return None
|
return None
|
||||||
elif not PEP_TYPE_DISPATCH.has_key(pep_type):
|
elif pep_type not in PEP_TYPE_DISPATCH:
|
||||||
print >> sys.stderr, ('Error: Unknown PEP type for input file %s: %s'
|
print(('Error: Unknown PEP type for input file %s: %s'
|
||||||
% (inpath, pep_type))
|
% (inpath, pep_type)), file=sys.stderr)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
return None
|
return None
|
||||||
elif PEP_TYPE_DISPATCH[pep_type] == None:
|
elif PEP_TYPE_DISPATCH[pep_type] == None:
|
||||||
|
@ -382,12 +388,12 @@ def make_html(inpath, verbose=0):
|
||||||
return None
|
return None
|
||||||
outpath = os.path.splitext(inpath)[0] + ".html"
|
outpath = os.path.splitext(inpath)[0] + ".html"
|
||||||
if verbose:
|
if verbose:
|
||||||
print inpath, "(%s)" % pep_type, "->", outpath
|
print(inpath, "(%s)" % pep_type, "->", outpath)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
outfile = open(outpath, "w")
|
outfile = open(outpath, "w", encoding='utf-8')
|
||||||
PEP_TYPE_DISPATCH[pep_type](inpath, input_lines, outfile)
|
PEP_TYPE_DISPATCH[pep_type](inpath, input_lines, outfile)
|
||||||
outfile.close()
|
outfile.close()
|
||||||
os.chmod(outfile.name, 0664)
|
os.chmod(outfile.name, 0o664)
|
||||||
return outpath
|
return outpath
|
||||||
|
|
||||||
def push_pep(htmlfiles, txtfiles, username, verbose, local=0):
|
def push_pep(htmlfiles, txtfiles, username, verbose, local=0):
|
||||||
|
@ -425,9 +431,8 @@ PEP_TYPE_MESSAGES = {}
|
||||||
|
|
||||||
def check_requirements():
|
def check_requirements():
|
||||||
# Check Python:
|
# Check Python:
|
||||||
try:
|
# This is pretty much covered by the __future__ imports...
|
||||||
from email.Utils import parseaddr
|
if sys.version_info < (2, 6, 0):
|
||||||
except ImportError:
|
|
||||||
PEP_TYPE_DISPATCH['text/plain'] = None
|
PEP_TYPE_DISPATCH['text/plain'] = None
|
||||||
PEP_TYPE_MESSAGES['text/plain'] = (
|
PEP_TYPE_MESSAGES['text/plain'] = (
|
||||||
'Python %s or better required for "%%(pep_type)s" PEP '
|
'Python %s or better required for "%%(pep_type)s" PEP '
|
||||||
|
@ -453,7 +458,7 @@ def check_requirements():
|
||||||
% (REQUIRES['docutils'], docutils.__version__))
|
% (REQUIRES['docutils'], docutils.__version__))
|
||||||
|
|
||||||
def pep_type_error(inpath, pep_type):
|
def pep_type_error(inpath, pep_type):
|
||||||
print >> sys.stderr, 'Error: ' + PEP_TYPE_MESSAGES[pep_type] % locals()
|
print('Error: ' + PEP_TYPE_MESSAGES[pep_type] % locals(), file=sys.stderr)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
@ -492,7 +497,7 @@ def main(argv=None):
|
||||||
opts, args = getopt.getopt(
|
opts, args = getopt.getopt(
|
||||||
argv, 'bilhqu:',
|
argv, 'bilhqu:',
|
||||||
['browse', 'install', 'local', 'help', 'quiet', 'user='])
|
['browse', 'install', 'local', 'help', 'quiet', 'user='])
|
||||||
except getopt.error, msg:
|
except getopt.error as msg:
|
||||||
usage(1, msg)
|
usage(1, msg)
|
||||||
|
|
||||||
for opt, arg in opts:
|
for opt, arg in opts:
|
||||||
|
|
Loading…
Reference in New Issue