#!/usr/bin/env python """Convert PEPs to (X)HTML - courtesy of /F Usage: %(PROGRAM)s [options] [ ...] Options: -u, --user python.org username -b, --browse After generating the HTML, direct your web browser to view it (using the Python webbrowser module). If both -i and -b are given, this will browse the on-line HTML; otherwise it will browse the local HTML. If no pep arguments are given, this will browse PEP 0. -i, --install After generating the HTML, install it and the plaintext source file (.txt) on python.org. In that case the user's name is used in the scp and ssh commands, unless "-u username" is given (in which case, it is used instead). Without -i, -u is ignored. -l, --local Same as -i/--install, except install on the local machine. Use this when logged in to the python.org machine (dinsdale). -q, --quiet Turn off verbose messages. -h, --help Print this help message and exit. The optional arguments ``peps`` are either pep numbers or .txt files. """ destDirBase = '/home/jafo/cvs/beta.python.org/build/data/doc/peps/' import sys import os import re import cgi import glob import getopt import errno import random import time import shutil REQUIRES = {'python': '2.2', 'docutils': '0.2.7'} PROGRAM = sys.argv[0] RFCURL = 'http://www.faqs.org/rfcs/rfc%d.html' PEPURL = 'pep-%04d.html' PEPCVSURL = ('http://svn.python.org/view/peps/trunk/pep-%04d.txt') PEPDIRRUL = 'http://www.python.org/peps/' HOST = "dinsdale.python.org" # host for update HDIR = "/data/ftp.python.org/pub/www.python.org/peps" # target host directory LOCALVARS = "Local Variables:" COMMENT = """""" # The generated HTML doesn't validate -- you cannot use
and

inside #
 tags.  But if I change that, the result doesn't look very nice...
DTD = ('')

fixpat = re.compile("((https?|ftp):[-_a-zA-Z0-9/.+~:?#$=&,]+)|(pep-\d+(.txt)?)|"
                    "(RFC[- ]?(?P\d+))|"
                    "(PEP\s+(?P\d+))|"
                    ".")

EMPTYSTRING = ''
SPACE = ' '
COMMASPACE = ', '



def usage(code, msg=''):
    """Print usage message and exit.  Uses stderr if code != 0."""
    if code == 0:
        out = sys.stdout
    else:
        out = sys.stderr
    print >> out, __doc__ % globals()
    if msg:
        print >> out, msg
    sys.exit(code)



def fixanchor(current, match):
    text = match.group(0)
    link = None
    if (text.startswith('http:') or text.startswith('https:')
        or text.startswith('ftp:')):
        # Strip off trailing punctuation.  Pattern taken from faqwiz.
        ltext = list(text)
        while ltext:
            c = ltext.pop()
            if c not in '();:,.?\'"<>':
                ltext.append(c)
                break
        link = EMPTYSTRING.join(ltext)
    elif text.startswith('pep-') and text <> current:
        link = os.path.splitext(text)[0] + ".html"
    elif text.startswith('PEP'):
        pepnum = int(match.group('pepnum'))
        link = PEPURL % pepnum
    elif text.startswith('RFC'):
        rfcnum = int(match.group('rfcnum'))
        link = RFCURL % rfcnum
    if link:
        return '%s' % (cgi.escape(link), cgi.escape(text))
    return cgi.escape(match.group(0)) # really slow, but it works...



NON_MASKED_EMAILS = [
    'peps@python.org',
    'python-list@python.org',
    'python-dev@python.org',
    ]

def fixemail(address, pepno):
    if address.lower() in NON_MASKED_EMAILS:
        # return hyperlinked version of email address
        return linkemail(address, pepno)
    else:
        # return masked version of email address
        parts = address.split('@', 1)
        return '%s at %s' % (parts[0], parts[1])


def linkemail(address, pepno):
    parts = address.split('@', 1)
    return (''
            '%s at %s'
            % (parts[0], parts[1], pepno, parts[0], parts[1]))


def fixfile(inpath, input_lines, outfile):
    from email.Utils import parseaddr
    basename = os.path.basename(inpath)
    infile = iter(input_lines)
    # head
    header = []
    pep = ""
    title = ""
    for line in infile:
        if not line.strip():
            break
        if line[0].strip():
            if ":" not in line:
                break
            key, value = line.split(":", 1)
            value = value.strip()
            header.append((key, value))
        else:
            # continuation line
            key, value = header[-1]
            value = value + line
            header[-1] = key, value
        if key.lower() == "title":
            title = value
        elif key.lower() == "pep":
            pep = value

    if pep:
        title = "PEP " + pep + " -- " + title
    if title:
        print >> outfile, '  %s' % cgi.escape(title)
    r = random.choice(range(64))
    print >> outfile, '
\n' for k, v in header: if k.lower() in ('author', 'discussions-to'): mailtos = [] for part in re.split(',\s*', v): if '@' in part: realname, addr = parseaddr(part) if k.lower() == 'discussions-to': m = linkemail(addr, pep) else: m = fixemail(addr, pep) mailtos.append('%s <%s>' % (realname, m)) elif part.startswith('http:'): mailtos.append( '%s' % (part, part)) else: mailtos.append(part) v = COMMASPACE.join(mailtos) elif k.lower() in ('replaces', 'replaced-by', 'requires'): otherpeps = '' for otherpep in re.split(',?\s+', v): otherpep = int(otherpep) otherpeps += '%i ' % (otherpep, otherpep) v = otherpeps elif k.lower() in ('last-modified',): date = v or time.strftime('%d-%b-%Y', time.localtime(os.stat(inpath)[8])) try: url = PEPCVSURL % int(pep) v = '%s ' % (url, cgi.escape(date)) except ValueError, error: v = date elif k.lower() in ('content-type',): url = PEPURL % 9 pep_type = v or 'text/plain' v = '%s ' % (url, cgi.escape(pep_type)) else: v = cgi.escape(v) print >> outfile, ' ' \ % (cgi.escape(k), v) print >> outfile, '
%s: %s
' need_pre = 1 for line in infile: if line[0] == '\f': continue if line.strip() == LOCALVARS: break if line[0].strip(): if not need_pre: print >> outfile, '
' print >> outfile, '

%s

' % line.strip() need_pre = 1 elif not line.strip() and need_pre: continue else: # PEP 0 has some special treatment if basename == 'pep-0000.txt': parts = line.split() if len(parts) > 1 and re.match(r'\s*\d{1,4}', parts[1]): # This is a PEP summary line, which we need to hyperlink url = PEPURL % int(parts[1]) if need_pre: print >> outfile, '
'
                        need_pre = 0
                    print >> outfile, re.sub(
                        parts[1],
                        '%s' % (int(parts[1]),
                            parts[1]), line, 1),
                    continue
                elif parts and '@' in parts[-1]:
                    # This is a pep email address line, so filter it.
                    url = fixemail(parts[-1], pep)
                    if need_pre:
                        print >> outfile, '
'
                        need_pre = 0
                    print >> outfile, re.sub(
                        parts[-1], url, line, 1),
                    continue
            line = fixpat.sub(lambda x, c=inpath: fixanchor(c, x), line)
            if need_pre:
                print >> outfile, '
'
                need_pre = 0
            outfile.write(line)
    if not need_pre:
        print >> outfile, '
' docutils_settings = None """Runtime settings object used by Docutils. Can be set by the client application when this module is imported.""" def fix_rst_pep(inpath, input_lines, outfile): from docutils import core output = core.publish_string( source=''.join(input_lines), source_path=inpath, destination_path=outfile.name, reader_name='pep', parser_name='restructuredtext', writer_name='pep_html', settings=docutils_settings, # Allow Docutils traceback if there's an exception: settings_overrides={'traceback': 1}) outfile.write(output) def get_pep_type(input_lines): """ Return the Content-Type of the input. "text/plain" is the default. Return ``None`` if the input is not a PEP. """ pep_type = None for line in input_lines: line = line.rstrip().lower() if not line: # End of the RFC 2822 header (first blank line). break elif line.startswith('content-type: '): pep_type = line.split()[1] or 'text/plain' break elif line.startswith('pep: '): # Default PEP type, used if no explicit content-type specified: pep_type = 'text/plain' return pep_type def get_input_lines(inpath): try: infile = open(inpath) except IOError, e: if e.errno <> errno.ENOENT: raise print >> sys.stderr, 'Error: Skipping missing PEP file:', e.filename sys.stderr.flush() return None, None lines = infile.read().splitlines(1) # handles x-platform line endings infile.close() return lines def find_pep(pep_str): """Find the .txt file indicated by a cmd line argument""" if os.path.exists(pep_str): return pep_str num = int(pep_str) return "pep-%04d.txt" % num def make_html(inpath, verbose=0): input_lines = get_input_lines(inpath) pep_type = get_pep_type(input_lines) if pep_type is None: print >> sys.stderr, 'Error: Input file %s is not a PEP.' % inpath sys.stdout.flush() return None elif not PEP_TYPE_DISPATCH.has_key(pep_type): print >> sys.stderr, ('Error: Unknown PEP type for input file %s: %s' % (inpath, pep_type)) sys.stdout.flush() return None elif PEP_TYPE_DISPATCH[pep_type] == None: pep_type_error(inpath, pep_type) return None outpath = os.path.splitext(inpath)[0] + ".html" if verbose: print inpath, "(%s)" % pep_type, "->", outpath sys.stdout.flush() # set up pyramid stuff m = re.search(r'pep-(\d+)\.', inpath) if not m: print "Ugh, can't find PEP number in file name." sys.exit(1) pepIn = m.group(1) destDir = os.path.join(destDirBase, 'pep-%s' % pepIn) needSvn = 0 if not os.path.exists(destDir): needSvn = 1 os.mkdir(destDir) # write content.html foofilename = os.path.join(destDir, 'content.html') fp = open(foofilename, 'w') fp.write('\n') fp.write('