From cc3cb98680f5be51f59a50cbb25f490c9acb3571 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Tue, 25 Jul 2000 03:51:44 +0000 Subject: [PATCH] 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. --- pep2html.py | 83 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/pep2html.py b/pep2html.py index cc4c2fbef..e7d1bb46b 100755 --- 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 @@ import cgi, glob, os, re, sys # this doesn't validate -- you cannot use
and

inside
 # tags.  but if I change that, the result doesn't look very nice...
 
-DTD = ('')
+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 "%s" % (link, cgi.escape(link))
+        return "%s" % (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\n" % DTD)
+    fo.write(DTD + "\n\n\n")
     # head
     header = []
-    fo.write("\n")
     pep = ""
     title = ""
     while 1:
@@ -57,47 +60,59 @@ def fixfile(infile, outfile):
     fo.write("\n")
     # body
     fo.write("\n")
-    fo.write("
\n")
-    fo.write("[home]")
-    if os.path.basename(file) != "pep-0000.txt":
-        fo.write(" [index]")
-    fo.write("\n
\n") - # fo.write("\n

\n")
+    fo.write("[home]\n")
+    if os.path.basename(infile) != "pep-0000.txt":
+        fo.write("[index]\n")
+    fo.write("
\n\n") for k, v in header: - fo.write("%s: %s\n" % (cgi.escape(k), cgi.escape(v))) + fo.write(" \n" + % (cgi.escape(k), cgi.escape(v))) title = 0 + fo.write("
%s:%s
\n
")
     while 1:
         line = fi.readline()
         if not line:
             break
         if line[:1] == "\f":
-            fo.write("\n
\n") + fo.write("
") # fo.write("\n

\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("

%s

\n" % line) - # fo.write("

%s

\n" % line)
-                    title = 0
+                    break
+                fo.write("
\n

%s

\n
" % line.strip())
+                # fo.write("

%s

\n" % line)
+                title = 0
             else:
                 fo.write(line)
     fo.write("
\n") fo.write("\n") fo.write("\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()