[Python-Dev] PEPs on the web

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Thu, 20 Jul 2000 21:46:18 +0200


peter wrote:
> 3) What about making those PEPs xhtml (or something like that)?
>    Pros:
>      - links from pep-000.txt
>      - links from the other peps
>    Cons:
>      - harder to read in checkins
>      - harder to edit
>    (maybe we could use as little markup as possible?)

please stick to plain text sources.

here's a little script that converts text PEP's to HTML.  feel
free to add it to your uploading scripts.

# convert PEP's to (X)HTML

import cgi, glob, os, re

fixpat =3D =
re.compile("((http|ftp):[-_a-zA-Z0-9/.+?~:]+)|(pep-\d+(.txt)?|.")

def fixanchor(match):
    text =3D match.group(0)
    link =3D None
    if text[:5] =3D=3D "http:" or text[:4] =3D=3D "ftp:":
        link =3D text
    elif text[:3] =3D=3D "pep":
        link =3D os.path.splitext(text)[0] + ".html"
    if link:
        return "<a href=3D'%s'>%s</a>" % (link, cgi.escape(text))
    return cgi.escape(match.group(0))

def fixfile(infile, outfile):
    # convert plain text pep to minimal XHTML markup
    fi =3D open(infile)
    fo =3D open(outfile, "w")
    fo.write("<html>\n")
    # head
    header =3D []
    fo.write("<head>\n")
    while 1:
        line =3D fi.readline()
        if not line or ":" not in line:
            break
        key, value =3D line.split(":", 1)
        value =3D value.strip()
        header.append((key, value))
        if key.lower() =3D=3D "title":
            fo.write("<title>%s</title>\n" % cgi.escape(value))
    fo.write("</head>\n")
    # body
    fo.write("<body bgcolor=3D'white'>\n")
    fo.write("<pre>\n")
    for k, v in header:
        fo.write("<b>%s:</b> %s\n" % (cgi.escape(k), cgi.escape(v)))
    title =3D 0
    while 1:
        line =3D fi.readline()
        if not line:
            break
        if line[:1] =3D=3D "\f":
            fo.write("<hr>\n")
            title =3D 1
        else:
            line =3D fixpat.sub(fixanchor, line)
            if title:
                fo.write("<h3>%s</h3>\n" % line)
            else:
                fo.write(line)
            title =3D 0
    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")

cheers /F