2000-07-20 18:29:24 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
convert PEP's to (X)HTML - courtesy of /F
|
|
|
|
|
2000-07-24 23:51:44 -04:00
|
|
|
Syntax: pep2html [-n] [sf_username]
|
2000-07-20 18:29:24 -04:00
|
|
|
|
|
|
|
The user name 'sf_username' is used to upload the converted files
|
|
|
|
to the web pages at source forge.
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
|
|
If -n is given, the script doesn't actually try to install the
|
|
|
|
generated HTML at SourceForge.
|
|
|
|
|
2000-07-20 18:29:24 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import cgi, glob, os, re, sys
|
|
|
|
|
2000-07-20 18:44:36 -04:00
|
|
|
# this 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-25 00:12:28 -04:00
|
|
|
HOST = "shell.sourceforge.net" # host for update
|
|
|
|
HDIR = "/home/groups/python/htdocs/peps" # target host directory
|
|
|
|
|
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
|
|
|
|
|
|
|
fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|.")
|
2000-07-20 18:29:24 -04:00
|
|
|
|
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"
|
|
|
|
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
|
|
|
|
|
|
|
def fixfile(infile, outfile):
|
|
|
|
# convert plain text pep to minimal XHTML markup
|
|
|
|
fi = open(infile)
|
|
|
|
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()
|
|
|
|
if not line or ":" not in line:
|
|
|
|
break
|
|
|
|
key, value = line.split(":", 1)
|
|
|
|
value = value.strip()
|
|
|
|
header.append((key, value))
|
|
|
|
if key.lower() == "title":
|
2000-07-20 18:44:36 -04:00
|
|
|
title = value
|
|
|
|
if key.lower() == "pep":
|
|
|
|
pep = value
|
|
|
|
if pep:
|
|
|
|
title = "PEP " + pep + " -- " + title
|
|
|
|
if title:
|
2000-07-26 00:14:30 -04:00
|
|
|
fo.write(" <title>%s</title>\n" % cgi.escape(title))
|
2000-07-20 18:29:24 -04:00
|
|
|
fo.write("</head>\n")
|
|
|
|
# body
|
2000-07-26 00:14:30 -04:00
|
|
|
fo.write('<body bgcolor="white">\n')
|
|
|
|
fo.write('[<a href="../">home</a>]\n')
|
2000-07-24 23:51:44 -04:00
|
|
|
if os.path.basename(infile) != "pep-0000.txt":
|
2000-07-26 00:14:30 -04:00
|
|
|
fo.write('[<a href=".">index</a>]\n')
|
|
|
|
fo.write('<hr />\n<table border="0">\n')
|
2000-07-20 18:29:24 -04:00
|
|
|
for k, v in header:
|
2000-07-24 23:51:44 -04:00
|
|
|
fo.write(" <tr><th align='right'>%s:</th><td>%s</td></tr>\n"
|
|
|
|
% (cgi.escape(k), cgi.escape(v)))
|
2000-07-20 18:29:24 -04:00
|
|
|
title = 0
|
2000-07-26 00:14:30 -04:00
|
|
|
fo.write("</table>\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":
|
|
|
|
if line[0].strip():
|
2000-07-20 18:44:36 -04:00
|
|
|
if line.strip() == "Local Variables:":
|
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-07-24 23:51:44 -04:00
|
|
|
|
|
|
|
def main():
|
|
|
|
update = 1
|
|
|
|
for file in glob.glob("pep-*.txt"):
|
|
|
|
print file, "..."
|
|
|
|
fixfile(file, os.path.splitext(file)[0] + ".html")
|
|
|
|
|
|
|
|
if len(sys.argv) > 1 and sys.argv[1] == "-n":
|
|
|
|
update = 0
|
|
|
|
del sys.argv[1]
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
username = ""
|
|
|
|
elif len(sys.argv) == 2:
|
|
|
|
username = sys.argv[1]+"@"
|
|
|
|
else:
|
|
|
|
raise "Syntax: "+sys.argv[0]+" [-n] [sf_username]"
|
|
|
|
|
|
|
|
if update:
|
2000-07-25 00:12:28 -04:00
|
|
|
os.system("scp pep-*.html " + username + HOST + ":" + HDIR)
|
|
|
|
os.system("ssh " + username + HOST + " chmod 664 " + HDIR + "/*")
|
2000-07-24 23:51:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|