summaryrefslogtreecommitdiff
path: root/two_column.py
blob: c3c05fa35caf8904cd0f41b81da33f8a3a4633c2 (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
46
47
48
49
50
51
52
53
54
# This script generates two column format from the script file. 
import re
content = {}
where = 'HEAD'
txt = ''
num = 0

# Parse the file, and get each cell's content.
for line in open('script.rst'):
    if line.startswith('..'):
        loc = re.findall(".. ((L|R)(\d+))", line)
        if loc:
            # Next cell has been found
            # Save previous cell data 
            content[where] = txt

            txt = ''
            where = loc[0][0]
            num = int(loc[0][2]) if int(loc[0][2]) > num else num
            continue
        else:
            pass
    txt += line

# Write the content to the file. 
f = open('script2col.rst', 'w')
f.write('%s' %content['HEAD'])

f.write("\n\n+%s+%s+\n" %('-'*82, '-'*82))
for i in range(1, num+1):
    l = '%s%s' %('L', i)
    r = '%s%s' %('R', i)

    # Split each side text into individual lines
    if l in content:
        ltext = content[l].strip().splitlines()
    else:
        ltext = ['']
    if r in content:
        rtext = content[r].strip().splitlines()
    else:
        rtext = ['']

    # Ensure that both sides have the same number of lines
    ltext.extend(['']*(len(rtext) - len(ltext))), rtext.extend(['']*(len(ltext) - len(rtext)))

    # Write each of the lines in respective columns
    for k in range(len(ltext)):
        f.write("| %-80s | %-80s |\n" %(ltext[k], rtext[k]))

    # Horizontal division
    f.write("+%s+%s+\n" %('-'*82, '-'*82))

f.close()