2000-07-20 18:29:24 -04:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
"""
|
|
|
|
|
convert PEP's to (X)HTML - courtesy of /F
|
|
|
|
|
|
2000-08-28 12:00:49 -04:00
|
|
|
|
Usage: %(PROGRAM)s [options] [peps]
|
|
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
|
|
The optional argument peps can be either pep numbers or .txt files.
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
Options:
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
2000-08-28 12:00:49 -04:00
|
|
|
|
-u/--user
|
|
|
|
|
SF username
|
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
-i/--install
|
2001-03-20 10:07:21 -05:00
|
|
|
|
After generating the HTML, install it and the plain text source file
|
|
|
|
|
(.txt) SourceForge. In that case the user's name is used in the scp
|
|
|
|
|
and ssh commands, unless sf_username is given (in which case, it is
|
|
|
|
|
used instead). Without -i, sf_username is ignored.
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
2000-11-03 10:43:28 -05:00
|
|
|
|
-q/--quiet
|
|
|
|
|
Turn off verbose messages.
|
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
-h/--help
|
|
|
|
|
Print this help message and exit.
|
2000-07-20 18:29:24 -04:00
|
|
|
|
"""
|
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
import cgi
|
|
|
|
|
import glob
|
|
|
|
|
import getopt
|
2001-07-05 14:44:20 -04:00
|
|
|
|
import errno
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
PROGRAM = sys.argv[0]
|
2001-06-05 13:21:19 -04:00
|
|
|
|
RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html'
|
2001-07-05 14:44:20 -04:00
|
|
|
|
PEPURL = 'pep-%04d.html'
|
2000-07-20 18:44:36 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HOST = "shell.sourceforge.net" # host for update
|
2001-05-01 13:53:52 -04:00
|
|
|
|
HDIR = "/home/groups/p/py/python/htdocs/peps" # target host directory
|
2000-07-28 02:40:10 -04:00
|
|
|
|
LOCALVARS = "Local Variables:"
|
2000-07-25 00:12:28 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
# The generated HTML doesn't validate -- you cannot use <hr> and <h3> inside
|
|
|
|
|
# <pre> tags. But if I change that, the result doesn't look very nice...
|
2000-07-24 23:51:44 -04:00
|
|
|
|
DTD = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"\n'
|
|
|
|
|
' "http://www.w3.org/TR/REC-html40/loose.dtd">')
|
2000-07-20 18:44:36 -04:00
|
|
|
|
|
2001-03-21 07:37:22 -05:00
|
|
|
|
fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|"
|
2001-07-05 14:44:20 -04:00
|
|
|
|
"(RFC[- ]?(?P<rfcnum>\d+))|"
|
|
|
|
|
"(PEP\s+(?P<pepnum>\d+))|"
|
|
|
|
|
".")
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def usage(code, msg=''):
|
|
|
|
|
sys.stderr.write(__doc__ % globals() + '\n')
|
|
|
|
|
if msg:
|
|
|
|
|
msg = str(msg)
|
|
|
|
|
if msg[-1] <> '\n':
|
|
|
|
|
msg = msg + '\n'
|
|
|
|
|
sys.stderr.write(msg)
|
|
|
|
|
sys.exit(code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-07-24 23:51:44 -04:00
|
|
|
|
def fixanchor(current, match):
|
2000-07-20 18:29:24 -04:00
|
|
|
|
text = match.group(0)
|
|
|
|
|
link = None
|
|
|
|
|
if text[:5] == "http:" or text[:4] == "ftp:":
|
|
|
|
|
link = text
|
2000-07-24 23:51:44 -04:00
|
|
|
|
elif text[:4] == "pep-" and text != current:
|
2000-07-20 18:29:24 -04:00
|
|
|
|
link = os.path.splitext(text)[0] + ".html"
|
2001-07-05 14:44:20 -04:00
|
|
|
|
elif text[:3] == 'PEP':
|
|
|
|
|
pepnum = int(match.group('pepnum'))
|
|
|
|
|
link = PEPURL % pepnum
|
2001-03-21 07:37:22 -05:00
|
|
|
|
elif text[:3] == 'RFC':
|
2001-07-05 14:44:20 -04:00
|
|
|
|
rfcnum = int(match.group('rfcnum'))
|
2001-06-05 13:21:19 -04:00
|
|
|
|
link = RFCURL % rfcnum
|
2000-07-20 18:29:24 -04:00
|
|
|
|
if link:
|
2000-07-24 23:51:44 -04:00
|
|
|
|
return "<a href='%s'>%s</a>" % (link, cgi.escape(text))
|
2000-07-20 18:44:36 -04:00
|
|
|
|
return cgi.escape(match.group(0)) # really slow, but it works...
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
|
|
|
|
|
2000-07-20 18:29:24 -04:00
|
|
|
|
def fixfile(infile, outfile):
|
|
|
|
|
# convert plain text pep to minimal XHTML markup
|
2001-07-05 14:44:20 -04:00
|
|
|
|
try:
|
|
|
|
|
fi = open(infile)
|
|
|
|
|
except IOError, e:
|
|
|
|
|
if e.errno <> errno.ENOENT: raise
|
|
|
|
|
print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename
|
|
|
|
|
return
|
2000-07-20 18:29:24 -04:00
|
|
|
|
fo = open(outfile, "w")
|
2000-07-24 23:51:44 -04:00
|
|
|
|
fo.write(DTD + "\n<html>\n<head>\n")
|
2000-07-20 18:29:24 -04:00
|
|
|
|
# head
|
|
|
|
|
header = []
|
2000-07-20 18:44:36 -04:00
|
|
|
|
pep = ""
|
|
|
|
|
title = ""
|
2000-07-20 18:29:24 -04:00
|
|
|
|
while 1:
|
|
|
|
|
line = fi.readline()
|
2000-07-27 15:18:59 -04:00
|
|
|
|
if not line.strip():
|
2000-07-20 18:29:24 -04:00
|
|
|
|
break
|
2000-07-27 15:18:59 -04:00
|
|
|
|
if line[0].strip():
|
|
|
|
|
if ":" not in line:
|
|
|
|
|
break
|
|
|
|
|
key, value = line.split(":", 1)
|
|
|
|
|
value = value.strip()
|
|
|
|
|
header.append((key, value))
|
|
|
|
|
else:
|
|
|
|
|
# continuation line
|
|
|
|
|
key, value = header[-1]
|
|
|
|
|
value = value + line
|
|
|
|
|
header[-1] = key, value
|
2000-07-20 18:29:24 -04:00
|
|
|
|
if key.lower() == "title":
|
2000-07-20 18:44:36 -04:00
|
|
|
|
title = value
|
2000-07-27 15:18:59 -04:00
|
|
|
|
elif key.lower() == "pep":
|
2000-07-20 18:44:36 -04:00
|
|
|
|
pep = value
|
|
|
|
|
if pep:
|
|
|
|
|
title = "PEP " + pep + " -- " + title
|
|
|
|
|
if title:
|
2000-07-27 14:44:44 -04:00
|
|
|
|
fo.write(" <title>%s</title>\n"
|
|
|
|
|
' <link rel="STYLESHEET" href="style.css">\n'
|
|
|
|
|
% cgi.escape(title))
|
2000-07-20 18:29:24 -04:00
|
|
|
|
fo.write("</head>\n")
|
|
|
|
|
# body
|
2000-07-27 14:44:44 -04:00
|
|
|
|
fo.write('<body bgcolor="white">\n'
|
|
|
|
|
'<div class="navigation">\n')
|
|
|
|
|
fo.write('[<b><a href="../">home</a></b>]\n')
|
2000-07-24 23:51:44 -04:00
|
|
|
|
if os.path.basename(infile) != "pep-0000.txt":
|
2000-07-27 14:44:44 -04:00
|
|
|
|
fo.write('[<b><a href=".">index</a></b>]\n')
|
2001-03-20 10:00:13 -05:00
|
|
|
|
fo.write('[<b><a href="pep-%04d.txt">PEP source</a></b>]\n' % int(pep))
|
2000-07-27 14:44:44 -04:00
|
|
|
|
fo.write('</div>\n'
|
|
|
|
|
'<div class="header">\n<table border="0">\n')
|
2000-07-20 18:29:24 -04:00
|
|
|
|
for k, v in header:
|
2001-03-21 12:26:05 -05:00
|
|
|
|
if k.lower() in ('author', 'discussions-to'):
|
2000-08-17 00:27:04 -04:00
|
|
|
|
mailtos = []
|
|
|
|
|
for addr in v.split():
|
|
|
|
|
if '@' in addr:
|
|
|
|
|
mailtos.append(
|
|
|
|
|
'<a href="mailto:%s?subject=PEP%%20%s">%s</a>' %
|
|
|
|
|
(addr, pep, addr))
|
2001-03-21 13:59:03 -05:00
|
|
|
|
elif addr.startswith('http:'):
|
|
|
|
|
mailtos.append(
|
|
|
|
|
'<a href="%s">%s</a>' % (addr, addr))
|
2000-08-17 00:27:04 -04:00
|
|
|
|
else:
|
|
|
|
|
mailtos.append(addr)
|
|
|
|
|
v = ' '.join(mailtos)
|
2001-03-21 12:26:05 -05:00
|
|
|
|
elif k.lower() in ('replaces', 'replaced-by'):
|
|
|
|
|
peps = ''
|
|
|
|
|
for pep in v.split():
|
|
|
|
|
pep = int(pep)
|
|
|
|
|
peps += '<a href="pep-%04d.html">%i</a> ' % (pep, pep)
|
|
|
|
|
v = peps
|
2000-08-17 00:27:04 -04:00
|
|
|
|
else:
|
|
|
|
|
v = cgi.escape(v)
|
2000-07-24 23:51:44 -04:00
|
|
|
|
fo.write(" <tr><th align='right'>%s:</th><td>%s</td></tr>\n"
|
2000-08-17 00:27:04 -04:00
|
|
|
|
% (cgi.escape(k), v))
|
2000-07-20 18:29:24 -04:00
|
|
|
|
title = 0
|
2000-07-27 14:44:44 -04:00
|
|
|
|
fo.write("</table>\n</div>\n<hr />\n"
|
|
|
|
|
"<pre>")
|
2000-07-20 18:29:24 -04:00
|
|
|
|
while 1:
|
|
|
|
|
line = fi.readline()
|
|
|
|
|
if not line:
|
|
|
|
|
break
|
2000-07-26 00:14:30 -04:00
|
|
|
|
if line[0] != "\f":
|
2000-08-15 06:35:17 -04:00
|
|
|
|
if line[0].strip():
|
2000-07-28 02:40:10 -04:00
|
|
|
|
if line.strip() == LOCALVARS:
|
2000-07-24 23:51:44 -04:00
|
|
|
|
break
|
|
|
|
|
fo.write("</pre>\n<h3>%s</h3>\n<pre>" % line.strip())
|
|
|
|
|
title = 0
|
2000-07-20 18:29:24 -04:00
|
|
|
|
else:
|
2000-07-26 00:14:30 -04:00
|
|
|
|
line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line)
|
2000-07-20 18:29:24 -04:00
|
|
|
|
fo.write(line)
|
2000-07-26 00:14:30 -04:00
|
|
|
|
fo.write("</pre>\n"
|
|
|
|
|
"</body>\n"
|
|
|
|
|
"</html>\n")
|
2000-07-25 00:12:28 -04:00
|
|
|
|
fo.close()
|
|
|
|
|
os.chmod(outfile, 0664)
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
2000-08-28 12:00:49 -04:00
|
|
|
|
|
|
|
|
|
def find_pep(pep_str):
|
|
|
|
|
"""Find the .txt file indicated by a cmd line argument"""
|
|
|
|
|
if os.path.exists(pep_str):
|
|
|
|
|
return pep_str
|
|
|
|
|
num = int(pep_str)
|
|
|
|
|
return "pep-%04d.txt" % num
|
|
|
|
|
|
2000-09-06 21:26:46 -04:00
|
|
|
|
def make_html(file, verbose=0):
|
2000-08-28 12:00:49 -04:00
|
|
|
|
newfile = os.path.splitext(file)[0] + ".html"
|
2000-09-06 21:26:46 -04:00
|
|
|
|
if verbose:
|
|
|
|
|
print file, "->", newfile
|
2000-08-28 12:00:49 -04:00
|
|
|
|
fixfile(file, newfile)
|
|
|
|
|
return newfile
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
2001-03-20 10:07:21 -05:00
|
|
|
|
SPACE = ' '
|
|
|
|
|
def push_pep(htmlfiles, txtfiles, username, verbose):
|
2000-09-08 11:31:36 -04:00
|
|
|
|
if verbose:
|
|
|
|
|
quiet = ""
|
|
|
|
|
else:
|
|
|
|
|
quiet = "-q"
|
|
|
|
|
if username:
|
|
|
|
|
username = username + "@"
|
|
|
|
|
target = username + HOST + ":" + HDIR
|
2001-03-20 10:07:21 -05:00
|
|
|
|
files = htmlfiles[:]
|
|
|
|
|
files.extend(txtfiles)
|
2000-09-08 11:31:36 -04:00
|
|
|
|
files.append("style.css")
|
2001-03-20 10:07:21 -05:00
|
|
|
|
filelist = SPACE.join(files)
|
2000-09-08 11:31:36 -04:00
|
|
|
|
rc = os.system("scp %s %s %s" % (quiet, filelist, target))
|
|
|
|
|
if rc:
|
|
|
|
|
sys.exit(rc)
|
|
|
|
|
rc = os.system("ssh %s%s chmod 664 %s/*" % (username, HOST, HDIR))
|
|
|
|
|
if rc:
|
|
|
|
|
sys.exit(rc)
|
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
2000-07-24 23:51:44 -04:00
|
|
|
|
def main():
|
2000-08-15 01:53:19 -04:00
|
|
|
|
# defaults
|
|
|
|
|
update = 0
|
|
|
|
|
username = ''
|
2000-09-06 21:26:46 -04:00
|
|
|
|
verbose = 1
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
|
|
|
|
try:
|
2000-11-01 18:45:47 -05:00
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'ihqu:',
|
|
|
|
|
['install', 'help', 'quiet', 'user='])
|
2000-08-15 01:53:19 -04:00
|
|
|
|
except getopt.error, msg:
|
|
|
|
|
usage(1, msg)
|
|
|
|
|
|
|
|
|
|
for opt, arg in opts:
|
|
|
|
|
if opt in ('-h', '--help'):
|
|
|
|
|
usage(0)
|
|
|
|
|
elif opt in ('-i', '--install'):
|
|
|
|
|
update = 1
|
2000-08-28 12:00:49 -04:00
|
|
|
|
elif opt in ('-u', '--user'):
|
2000-09-08 11:31:36 -04:00
|
|
|
|
username = arg
|
2000-09-06 21:26:46 -04:00
|
|
|
|
elif opt in ('-q', '--quiet'):
|
|
|
|
|
verbose = 0
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
2000-08-28 12:00:49 -04:00
|
|
|
|
if args:
|
2001-03-20 10:07:21 -05:00
|
|
|
|
peptxt = []
|
2000-08-28 12:00:49 -04:00
|
|
|
|
html = []
|
|
|
|
|
for pep in args:
|
|
|
|
|
file = find_pep(pep)
|
2001-03-20 10:07:21 -05:00
|
|
|
|
peptxt.append(file)
|
2000-09-06 21:26:46 -04:00
|
|
|
|
newfile = make_html(file, verbose=verbose)
|
2000-08-28 12:00:49 -04:00
|
|
|
|
html.append(newfile)
|
|
|
|
|
else:
|
|
|
|
|
# do them all
|
2001-03-20 10:07:21 -05:00
|
|
|
|
peptxt = []
|
2000-08-28 12:00:49 -04:00
|
|
|
|
for file in glob.glob("pep-*.txt"):
|
2001-03-20 10:07:21 -05:00
|
|
|
|
peptxt.append(file)
|
2000-09-06 21:26:46 -04:00
|
|
|
|
make_html(file, verbose=verbose)
|
2000-09-08 11:31:36 -04:00
|
|
|
|
html = ["pep-*.html"]
|
2000-07-24 23:51:44 -04:00
|
|
|
|
if update:
|
2001-03-20 10:07:21 -05:00
|
|
|
|
push_pep(html, peptxt, username, verbose)
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
2000-08-15 01:53:19 -04:00
|
|
|
|
|
2000-07-24 23:51:44 -04:00
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|