#! /usr/local/bin/python # Convert the Python FAQ to HTML import string import regex import regsub import sys import os FAQ = 'FAQ' chapterprog = regex.compile('^\([1-9][0-9]*\)\. ') questionprog = regex.compile('^\([1-9][0-9]*\)\.\([1-9][0-9]*\)\. ') newquestionprog = regex.compile('^Q\. ') blankprog = regex.compile('^[ \t]*$') indentedorblankprog = regex.compile('^\([ \t]+\|[ \t]*$\)') underlineprog = regex.compile('^==*$') eightblanksprog = regex.compile('^\( \| *\t\)') mailheaderprog = regex.compile('^\(Subject\|Newsgroups\|Followup-To\|From\|Reply-To\|Approved\|Archive-name\|Version\|Last-modified\): +') urlprog = regex.compile(']*\)>') ampprog = regex.compile('&') aprog = regex.compile('^A\. +') qprog = regex.compile('>Q\. +') qrefprog = regex.compile('question +\([0-9]\.[0-9]+\)') versionprog = regex.compile('^Version: ') emailprog = regex.compile('<\([^>@:]+@[^>@:]+\)>') def main(): print 'Reading lines...' lines = open(FAQ, 'r').readlines() print 'Renumbering in memory...' oldlines = lines[:] after_blank = 1 chapter = 0 question = 0 chapters = ['
    '] questions = ['
      '] for i in range(len(lines)): line = lines[i] if after_blank: n = chapterprog.match(line) if n >= 0: chapter = chapter + 1 if chapter != 1: questions.append('\n') question = 0 lines[i] = '

      ' + line[n:-1] + '

      \n' chapters.append('
    1. ' + line[n:]) questions.append('
    2. ' + line[n:]) questions.append('
    \n') for i in range(len(lines)): line = lines[i] if regex.match( '^This FAQ is divided in the following chapters', line) >= 0: i = i+1 while 1: line = lines[i] if indentedorblankprog.match(line) < 0: break del lines[i] lines[i:i] = chapters break else: print '*** Can\'t find header for list of chapters' print '*** Chapters found:' for line in chapters: print line, print 'Inserting list of questions...' questions.append('
\n') for i in range(len(lines)): line = lines[i] if regex.match('^Here.s an overview of the questions', line) >= 0: i = i+1 while 1: line = lines[i] if indentedorblankprog.match(line) < 0: break del lines[i] lines[i:i] = questions break else: print '*** Can\'t find header for list of questions' print '*** Questions found:' for line in questions: print line, # final cleanup print "Final cleanup..." doingpre = 0 for i in range(len(lines)): # set lines indented by >= 8 spaces using PRE # blank lines either terminate PRE or separate paragraphs n = eightblanksprog.match(lines[i]) if n < 0: n = mailheaderprog.match(lines[i]) if n >= 0: if versionprog.match(lines[i]) > 0: version = string.split(lines[i])[1] if doingpre == 0: lines[i] = '
\n' + lines[i]
		doingpre = 1
		continue
	n = blankprog.match(lines[i])
	if n >= 0:
	    # print '*** ', lines[i-1], doingpre
	    if doingpre == 1:
		lines[i] = '

\n' doingpre = 0 else: lines[i] = '

\n' continue # & -> & n = ampprog.search(lines[i]) if n >= 0: lines[i] = regsub.gsub(ampprog, '&', lines[i]) # no continue - there might be other changes to the line... # zap all the 'Q.' and 'A.' leaders - what happened to the # last couple? n = qprog.search(lines[i]) if n >= 0: lines[i] = regsub.sub(qprog, '>', lines[i]) # no continue - there might be other changes to the line... n = aprog.search(lines[i]) if n >= 0: lines[i] = regsub.sub(aprog, '', lines[i]) # no continue - there might be other changes to the line... # patch up hard refs to questions n = qrefprog.search(lines[i]) if n >= 0: lines[i] = regsub.sub(qrefprog, 'question \\1', lines[i]) # no continue - there might be other changes to the line... # make into actual links n = urlprog.search(lines[i]) if n >= 0: lines[i] = regsub.gsub(urlprog, '\\1', lines[i]) # no continue - there might be other changes to the line... # make into links n = emailprog.search(lines[i]) if n >= 0: lines[i] = regsub.gsub(emailprog, '\\1', lines[i]) # no continue - there might be other changes to the line... lines[0:0] = ['Python Frequently Asked Questions v', version, '\n', '\n', '(This file was generated using\n', 'faq2html.py.)

\n'] lines.append('

\n') print 'Writing html file...' f = open(FAQ + '.html', 'w') for line in lines: f.write(line) f.close() print 'Done.' main()