[Python-checkins] peps: Revise the generated HTML to be a little nicer, but still pretty simple.

georg.brandl python-checkins at python.org
Wed Mar 23 21:23:06 CET 2011


http://hg.python.org/peps/rev/1a74debcbd5f
changeset:   40:1a74debcbd5f
user:        Fred Drake <fdrake at acm.org>
date:        Tue Jul 25 03:51:44 2000 +0000
summary:
  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.

files:
  pep2html.py |  83 ++++++++++++++++++++++++----------------
  1 files changed, 49 insertions(+), 34 deletions(-)


diff --git a/pep2html.py b/pep2html.py
--- a/pep2html.py
+++ b/pep2html.py
@@ -2,10 +2,14 @@
 """
 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
 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
@@ -13,30 +17,29 @@
 # 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...
 
-DTD = ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
-       '"http://www.w3.org/TR/REC-html40/loose.dtd">')
+DTD = ('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"\n'
+       '                      "http://www.w3.org/TR/REC-html40/loose.dtd">')
 
 fixpat = re.compile("((http|ftp):[-_a-zA-Z0-9/.+~:?#$=&]+)|(pep-\d+(.txt)?)|.")
 
-def fixanchor(match):
+def fixanchor(current, match):
     text = match.group(0)
     link = None
     if text[:5] == "http:" or text[:4] == "ftp:":
         link = text
-    elif text[:3] == "pep":
+    elif text[:4] == "pep-" and text != current:
         link = os.path.splitext(text)[0] + ".html"
     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...
 
 def fixfile(infile, outfile):
     # convert plain text pep to minimal XHTML markup
     fi = open(infile)
     fo = open(outfile, "w")
-    fo.write("%s\n<html>\n" % DTD)
+    fo.write(DTD + "\n<html>\n<head>\n")
     # head
     header = []
-    fo.write("<head>\n")
     pep = ""
     title = ""
     while 1:
@@ -57,47 +60,59 @@
     fo.write("</head>\n")
     # body
     fo.write("<body bgcolor='white'>\n")
-    fo.write("<pre>\n")
-    fo.write("[<a href='..'>home</a>]")
-    if os.path.basename(file) != "pep-0000.txt":
-        fo.write(" [<a href='.'>index</a>]")
-    fo.write("\n<hr />\n")
-    # fo.write("\n</pre><hr /><pre>\n")
+    fo.write("[<a href='../'>home</a>]\n")
+    if os.path.basename(infile) != "pep-0000.txt":
+        fo.write("[<a href='.'>index</a>]\n")
+    fo.write("<hr />\n<table border='0'>\n")
     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
+    fo.write("</table>\n<pre>")
     while 1:
         line = fi.readline()
         if not line:
             break
         if line[:1] == "\f":
-            fo.write("\n<hr />\n")
+            fo.write("<hr />")
             # fo.write("\n</pre><hr /><pre>\n")
             title = 1
-        elif title >= 0:
-            line = fixpat.sub(fixanchor, line)
+        else:
+            line = fixpat.sub(lambda x, c=infile: fixanchor(c, x), line)
             if title:
                 if line.strip() == "Local Variables:":
-                    title = -1
-                else:
-                    fo.write("<h3><tt>%s</tt></h3>\n" % line)
-                    # fo.write("</pre><h3><tt>%s</tt></h3><pre>\n" % line)
-                    title = 0
+                    break
+                fo.write("</pre>\n<h3>%s</h3>\n<pre>" % line.strip())
+                # fo.write("</pre><h3><tt>%s</tt></h3><pre>\n" % line)
+                title = 0
             else:
                 fo.write(line)
     fo.write("</pre>\n")
     fo.write("</body>\n")
     fo.write("</html>\n")
 
-for file in glob.glob("pep-*.txt"):
-    print file, "..."
-    fixfile(file, os.path.splitext(file)[0] + ".html")
 
-if len(sys.argv) == 1:
-    username = ""
-elif len(sys.argv) == 2:
-    username = sys.argv[1]+"@"
-else:
-    raise "Syntax: "+sys.argv[0]+" [sf_username]"
-    
-os.system("scp pep-*.html "+username+"shell.sourceforge.net:/home/groups/python/htdocs/peps")
+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:
+        os.system("scp pep-*.html " + username
+                  + "shell.sourceforge.net:/home/groups/python/htdocs/peps")
+
+
+if __name__ == "__main__":
+    main()

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list