#!/usr/bin/env python
"""
convert PEP's to (X)HTML - courtesy of /F
Usage: %(PROGRAM)s [options] [peps]
Options:
-u/--user
SF username
-b/--browse
After generating the HTML, direct your web browser to view it
(using the Python webbrowser module). If both -i and -b are
given, this will browse the on-line HTML; otherwise it will
browse the local HTML. If no pep arguments are given, this
will browse PEP 0.
-i/--install
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 -u sf_username is given (in which case, it is
used instead). Without -i, -u is ignored.
-q/--quiet
Turn off verbose messages.
-h/--help
Print this help message and exit.
The optional argument `peps' is a list of either pep numbers or .txt files.
"""
import sys
import os
import re
import cgi
import glob
import getopt
import errno
import random
import time
PROGRAM = sys.argv[0]
RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html'
PEPURL = 'pep-%04d.html'
PEPCVSURL = 'http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/python/python/nondist/peps/pep-%04d.txt'
PEPDIRRUL = 'http://www.python.org/peps/'
HOST = "www.python.org" # host for update
HDIR = "/ftp/ftp.python.org/pub/www.python.org/peps" # target host directory
LOCALVARS = "Local Variables:"
# The generated HTML doesn't validate -- you cannot use
and
inside
#
tags. But if I change that, the result doesn't look very nice...
DTD = ('')
fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&,]+)|(pep-\d+(.txt)?)|"
"(RFC[- ]?(?P\d+))|"
"(PEP\s+(?P\d+))|"
".")
EMPTYSTRING = ''
SPACE = ' '
def usage(code, msg=''):
print >> sys.stderr, __doc__ % globals()
if msg:
print >> sys.stderr, msg
sys.exit(code)
def fixanchor(current, match):
text = match.group(0)
link = None
if text.startswith('http:') or text.startswith('ftp:'):
# Strip off trailing punctuation. Pattern taken from faqwiz.
ltext = list(text)
while ltext:
c = ltext.pop()
if c not in '();:,.?\'"<>':
ltext.append(c)
break
link = EMPTYSTRING.join(ltext)
elif text.startswith('pep-') and text <> current:
link = os.path.splitext(text)[0] + ".html"
elif text.startswith('PEP'):
pepnum = int(match.group('pepnum'))
link = PEPURL % pepnum
elif text.startswith('RFC'):
rfcnum = int(match.group('rfcnum'))
link = RFCURL % rfcnum
if link:
return '%s' % (link, cgi.escape(text))
return cgi.escape(match.group(0)) # really slow, but it works...
NON_MASKED_EMAILS = [
'peps@python.org',
'python-list@python.org',
'python-dev@python.org',
]
def fixemail(address, pepno):
if address.lower() in NON_MASKED_EMAILS:
# return hyperlinked version of email address
return linkemail(address, pepno)
else:
# return masked version of email address
parts = address.split('@', 1)
return '%s at %s' % (parts[0], parts[1])
def linkemail(address, pepno):
parts = address.split('@', 1)
return (''
'%s at %s'
% (parts[0], parts[1], pepno, parts[0], parts[1]))
def fixfile(infile, outfile):
basename = os.path.basename(infile)
# convert plain text pep to minimal XHTML markup
try:
fi = open(infile)
except IOError, e:
if e.errno <> errno.ENOENT: raise
print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename
return
fo = open(outfile, "w")
print >> fo, DTD
print >> fo, ''
print >> fo, ''
# head
header = []
pep = ""
title = ""
while 1:
line = fi.readline()
if not line.strip():
break
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
if key.lower() == "title":
title = value
elif key.lower() == "pep":
pep = value
if pep:
title = "PEP " + pep + " -- " + title
if title:
print >> fo, ' %s' % cgi.escape(title)
print >> fo, ' '
print >> fo, ''
# body
print >> fo, ''
print >> fo, '
'
for k, v in header:
if k.lower() in ('author', 'discussions-to'):
mailtos = []
for addr in v.split():
if '@' in addr:
if k.lower() == 'discussions-to':
m = linkemail(addr, pep)
else:
m = fixemail(addr, pep)
mailtos.append(m)
elif addr.startswith('http:'):
mailtos.append(
'%s' % (addr, addr))
else:
mailtos.append(addr)
v = SPACE.join(mailtos)
elif k.lower() in ('replaces', 'replaced-by'):
otherpeps = ''
for otherpep in v.split():
otherpep = int(otherpep)
otherpeps += '%i ' % (otherpep,
otherpep)
v = otherpeps
elif k.lower() in ('last-modified',):
url = PEPCVSURL % int(pep)
date = v or time.strftime('%d-%b-%Y',
time.localtime(os.stat(infile)[8]))
v = '%s ' % (url, cgi.escape(date))
else:
v = cgi.escape(v)
print >> fo, '
%s:
%s
' \
% (cgi.escape(k), v)
print >> fo, '
'
print >> fo, '
'
print >> fo, ''
print >> fo, '
'
need_pre = 1
while 1:
line = fi.readline()
if not line:
break
if line[0] == '\f':
continue
if line.strip() == LOCALVARS:
break
if line[0].strip():
if line.strip() == LOCALVARS:
break
if not need_pre:
print >> fo, ''
print >> fo, '
%s
' % line.strip()
need_pre = 1
elif not line.strip() and need_pre:
continue
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])
if need_pre:
print >> fo, '
'
need_pre = 0
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 filter it.
url = fixemail(parts[-1], pep)
if need_pre:
print >> fo, '