summaryrefslogtreecommitdiff
path: root/docs/howto-write-a-block/make_numbered_listing.py
blob: 889c2d78d4584370d5f8e12e8ce4f15d780ac603 (plain)
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
#!/usr/bin/env python

import sys
import os, os.path
from optparse import OptionParser

def quote_line (line):
    line = line.replace ('&', '&')
    line = line.replace ('<', '&lt;')
    line = line.replace ('>', '&gt;')
    line = line.replace ("'", '&apos;')
    line = line.replace ('"', '&quot;')
    return line

def generate_listing (input_filename, title=None):
    inf = open (input_filename, "r")
    output_filename = os.path.basename (input_filename) + '.xml'
    outf = open (output_filename, "w")
    outf.write ('<?xml version="1.0" encoding="ISO-8859-1"?>\n')
    # outf.write ('<example id="%s">\n' % (input_filename,))
    # if not title:
    #     title = input_filename
    # outf.write ('<title>')
    # outf.write (title)
    # outf.write ('</title>\n')
    outf.write ('<programlisting>\n');

    lineno = 0
    for line in inf:
        line = line.expandtabs (8)
        line = quote_line (line)
        lineno = lineno + 1
        outf.write ('%3d  %s' % (lineno, line))

    outf.write ('</programlisting>\n')
    # outf.write ('</example>\n')


def main ():
    for file in sys.argv[1:]:
        generate_listing (file)

if __name__ == '__main__':
    main ()