Revise the generated HTML to be a little nicer, but still pretty simple.
Also add a -n option to suppress installing the generated HTML at SourceForge to allow local use.
This commit is contained in:
parent
830acec859
commit
cc3cb98680
59
pep2html.py
59
pep2html.py
|
@ -2,10 +2,14 @@
|
||||||
"""
|
"""
|
||||||
convert PEP's to (X)HTML - courtesy of /F
|
convert PEP's to (X)HTML - courtesy of /F
|
||||||
|
|
||||||
Syntax: pep2html [sf_username]
|
Syntax: pep2html [-n] [sf_username]
|
||||||
|
|
||||||
The user name 'sf_username' is used to upload the converted files
|
The user name 'sf_username' is used to upload the converted files
|
||||||
to the web pages at source forge.
|
to the web pages at source forge.
|
||||||
|
|
||||||
|
If -n is given, the script doesn't actually try to install the
|
||||||
|
generated HTML at SourceForge.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import cgi, glob, os, re, sys
|
import cgi, glob, os, re, sys
|
||||||
|
@ -13,30 +17,29 @@ import cgi, glob, os, re, sys
|
||||||
# this doesn't validate -- you cannot use <hr> and <h3> inside <pre>
|
# 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...
|
# tags. but if I change that, the result doesn't look very nice...
|
||||||
|
|
||||||
DTD = ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
|
DTD = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"\n'
|
||||||
' "http://www.w3.org/TR/REC-html40/loose.dtd">')
|
' "http://www.w3.org/TR/REC-html40/loose.dtd">')
|
||||||
|
|
||||||
fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|.")
|
fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|.")
|
||||||
|
|
||||||
def fixanchor(match):
|
def fixanchor(current, match):
|
||||||
text = match.group(0)
|
text = match.group(0)
|
||||||
link = None
|
link = None
|
||||||
if text[:5] == "http:" or text[:4] == "ftp:":
|
if text[:5] == "http:" or text[:4] == "ftp:":
|
||||||
link = text
|
link = text
|
||||||
elif text[:3] == "pep":
|
elif text[:4] == "pep-" and text != current:
|
||||||
link = os.path.splitext(text)[0] + ".html"
|
link = os.path.splitext(text)[0] + ".html"
|
||||||
if link:
|
if link:
|
||||||
return "<a href='%s'>%s</a>" % (link, cgi.escape(link))
|
return "<a href='%s'>%s</a>" % (link, cgi.escape(text))
|
||||||
return cgi.escape(match.group(0)) # really slow, but it works...
|
return cgi.escape(match.group(0)) # really slow, but it works...
|
||||||
|
|
||||||
def fixfile(infile, outfile):
|
def fixfile(infile, outfile):
|
||||||
# convert plain text pep to minimal XHTML markup
|
# convert plain text pep to minimal XHTML markup
|
||||||
fi = open(infile)
|
fi = open(infile)
|
||||||
fo = open(outfile, "w")
|
fo = open(outfile, "w")
|
||||||
fo.write("%s\n<html>\n" % DTD)
|
fo.write(DTD + "\n<html>\n<head>\n")
|
||||||
# head
|
# head
|
||||||
header = []
|
header = []
|
||||||
fo.write("<head>\n")
|
|
||||||
pep = ""
|
pep = ""
|
||||||
title = ""
|
title = ""
|
||||||
while 1:
|
while 1:
|
||||||
|
@ -57,30 +60,29 @@ def fixfile(infile, outfile):
|
||||||
fo.write("</head>\n")
|
fo.write("</head>\n")
|
||||||
# body
|
# body
|
||||||
fo.write("<body bgcolor='white'>\n")
|
fo.write("<body bgcolor='white'>\n")
|
||||||
fo.write("<pre>\n")
|
fo.write("[<a href='../'>home</a>]\n")
|
||||||
fo.write("[<a href='..'>home</a>]")
|
if os.path.basename(infile) != "pep-0000.txt":
|
||||||
if os.path.basename(file) != "pep-0000.txt":
|
fo.write("[<a href='.'>index</a>]\n")
|
||||||
fo.write(" [<a href='.'>index</a>]")
|
fo.write("<hr />\n<table border='0'>\n")
|
||||||
fo.write("\n<hr />\n")
|
|
||||||
# fo.write("\n</pre><hr /><pre>\n")
|
|
||||||
for k, v in header:
|
for k, v in header:
|
||||||
fo.write("<b>%s:</b> %s\n" % (cgi.escape(k), cgi.escape(v)))
|
fo.write(" <tr><th align='right'>%s:</th><td>%s</td></tr>\n"
|
||||||
|
% (cgi.escape(k), cgi.escape(v)))
|
||||||
title = 0
|
title = 0
|
||||||
|
fo.write("</table>\n<pre>")
|
||||||
while 1:
|
while 1:
|
||||||
line = fi.readline()
|
line = fi.readline()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
if line[:1] == "\f":
|
if line[:1] == "\f":
|
||||||
fo.write("\n<hr />\n")
|
fo.write("<hr />")
|
||||||
# fo.write("\n</pre><hr /><pre>\n")
|
# fo.write("\n</pre><hr /><pre>\n")
|
||||||
title = 1
|
title = 1
|
||||||
elif title >= 0:
|
else:
|
||||||
line = fixpat.sub(fixanchor, line)
|
line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line)
|
||||||
if title:
|
if title:
|
||||||
if line.strip() == "Local Variables:":
|
if line.strip() == "Local Variables:":
|
||||||
title = -1
|
break
|
||||||
else:
|
fo.write("</pre>\n<h3>%s</h3>\n<pre>" % line.strip())
|
||||||
fo.write("<h3><tt>%s</tt></h3>\n" % line)
|
|
||||||
# fo.write("</pre><h3><tt>%s</tt></h3><pre>\n" % line)
|
# fo.write("</pre><h3><tt>%s</tt></h3><pre>\n" % line)
|
||||||
title = 0
|
title = 0
|
||||||
else:
|
else:
|
||||||
|
@ -89,15 +91,28 @@ def fixfile(infile, outfile):
|
||||||
fo.write("</body>\n")
|
fo.write("</body>\n")
|
||||||
fo.write("</html>\n")
|
fo.write("</html>\n")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
update = 1
|
||||||
for file in glob.glob("pep-*.txt"):
|
for file in glob.glob("pep-*.txt"):
|
||||||
print file, "..."
|
print file, "..."
|
||||||
fixfile(file, os.path.splitext(file)[0] + ".html")
|
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:
|
if len(sys.argv) == 1:
|
||||||
username = ""
|
username = ""
|
||||||
elif len(sys.argv) == 2:
|
elif len(sys.argv) == 2:
|
||||||
username = sys.argv[1]+"@"
|
username = sys.argv[1]+"@"
|
||||||
else:
|
else:
|
||||||
raise "Syntax: "+sys.argv[0]+" [sf_username]"
|
raise "Syntax: "+sys.argv[0]+" [-n] [sf_username]"
|
||||||
|
|
||||||
os.system("scp pep-*.html "+username+"shell.sourceforge.net:/home/groups/python/htdocs/peps")
|
if update:
|
||||||
|
os.system("scp pep-*.html " + username
|
||||||
|
+ "shell.sourceforge.net:/home/groups/python/htdocs/peps")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue