aboutsummaryrefslogtreecommitdiffstats
path: root/admin/bin/faq2ht.py
blob: 29db9ea76789911f1223b4257d0f1419eaf2369f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python

"""Convert the plain text FAQ file to its .ht template.
"""

import sys
import os
import re



def main():
    faqfile = sys.argv[1]
    fp = open(faqfile)
    lines = fp.readlines()
    fp.close()

    outfile = sys.argv[2]
    if outfile == '-':
        closep = 0
        out = sys.stdout
    else:
        closep = 1
        out = open(outfile, 'w')

    # skip over cruft in FAQ file
    lineno = 0
    while not lines[lineno].startswith('FREQUENTLY'):
        lineno += 1
    lineno += 1

    # skip blanks
    while not lines[lineno].strip():
        lineno += 1

    # first print out standard .ht boilerplate
    print >> out, '''\
Title: Mailman Frequently Asked Questions

See also the <a href="http://www.python.org/cgi-bin/faqw-mm.py">Mailman
FAQ Wizard</a> for more information.

 <h3>Mailman Frequently Asked Questions</h3>
'''
    first = 1
    question = []
    answer = []
    faq = []
    while 1:
        line = lines[lineno][:-1]

        if line.startswith('Q.'):
            inquestion = 1
            if not first:
                faq.append((question, answer))
                question = []
                answer = []
            else:
                first = 0
        elif line.startswith('A.'):
            inquestion = 0
        elif line.startswith('\f'):
            break

        if inquestion:
            question.append(line)
        else:
            # watch for lists
            if line.lstrip().startswith('*'):
                answer.append('<li>')
                line = line.replace('*', '', 1)
            # substitute <...>
            line = re.sub(r'<(?P<var>[^>]+)>',
                          '<em>\g<var></em>',
                          line)
            # make links active
            line = re.sub(r'(?P<url>http://\S+)',
                          '<a href="\g<url>">\g<url></a>',
                          line)
            answer.append(line)

        lineno += 1
    faq.append((question, answer))

    for question, answer in faq:
        print >> out, '<b>',
        for line in question:
            print >> out, line
        print >> out, '</b><br>',
        for line in answer:
            if not line:
                print >> out, '<p>',
            else:
                print >> out, line

    if closep:
        out.close()



if __name__ == '__main__':
    main()