scripts/titlegen.py
author Peter Kovacs <kpeter@inf.elte.hu>
Mon, 01 Mar 2010 02:30:00 +0100
changeset 58 10b6a5b7d4c0
parent 26 a40eafb6066d
child 59 5d9170b19285
permissions -rwxr-xr-x
Improve Algorithms section (it is still under construction)
alpar@10
     1
#! /usr/bin/env python
alpar@10
     2
alpar@10
     3
import sys
alpar@10
     4
import os
alpar@10
     5
import copy
alpar@10
     6
import re
alpar@10
     7
kpeter@42
     8
max_sec_number=100
kpeter@42
     9
max_sec_depth=4
kpeter@42
    10
alpar@10
    11
def sec_inc(section, lev):
alpar@10
    12
    while len(section)<lev:
alpar@10
    13
        section.append(0)
alpar@10
    14
    section[lev-1]+=1
alpar@10
    15
    section=section[:lev]
alpar@10
    16
    return section
alpar@10
    17
alpar@10
    18
def format_sec(sec):
alpar@10
    19
    s=""
alpar@10
    20
    for i in sec:
alpar@10
    21
        s+=str(i)+'.'
alpar@15
    22
    s=s[:-1]
alpar@10
    23
    return s
alpar@10
    24
kpeter@42
    25
def compare_sec(id1, id2):
kpeter@42
    26
    id1=id1.split('.')
kpeter@42
    27
    c1=0
kpeter@42
    28
    for s in id1:
kpeter@42
    29
        c1=c1*max_sec_number+int(s)
kpeter@42
    30
    for i in range(len(id1), max_sec_depth+1):
kpeter@42
    31
        c1*=max_sec_number
kpeter@42
    32
    id2=id2.split('.')
kpeter@42
    33
    c2=0
kpeter@42
    34
    for s in id2:
kpeter@42
    35
        c2=c2*max_sec_number+int(s)
kpeter@42
    36
    for i in range(len(id2), max_sec_depth+1):
kpeter@42
    37
        c2*=max_sec_number
kpeter@42
    38
    return c1-c2
kpeter@42
    39
alpar@10
    40
section = [];
alpar@10
    41
toc={}
alpar@10
    42
ind={}
alpar@10
    43
alpar@10
    44
prev_page=''
alpar@10
    45
for l in open("toc.txt").readlines():
alpar@10
    46
    sl = l.split()
alpar@10
    47
    if len(sl)==2 and len(sl[0])>0: 
alpar@10
    48
        lev=len(sl[0])
alpar@10
    49
        section=sec_inc(section,lev)
alpar@10
    50
        t_sec=copy.copy(section)
alpar@10
    51
        t_link=sl[1];
alpar@10
    52
        print format_sec(t_sec),t_link
alpar@10
    53
        ind[t_link]=[t_sec,'','']
alpar@10
    54
        if lev==1:
alpar@10
    55
            ind[t_link][1]=prev_page
alpar@10
    56
            if prev_page:
alpar@10
    57
                ind[prev_page][2]=t_link
alpar@10
    58
            prev_page=t_link
alpar@10
    59
        toc[format_sec(t_sec)]=t_link
alpar@10
    60
alpar@10
    61
for doxfile in os.listdir('.'):
alpar@10
    62
    if doxfile[-4:]=='.dox':
alpar@10
    63
        print 'Generate ',doxfile
alpar@10
    64
        page=''
alpar@10
    65
        fo=open(os.path.join("gen-dox",doxfile),"w")
alpar@10
    66
        for l in open(doxfile).readlines():
alpar@10
    67
            gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(^[[]SEC[]].*[[]SEC[]])?(^[[]TRAILER[]])?(^[[]TOC[]])?(.*)$", l).groups()
alpar@10
    68
            if gr[0]:
alpar@10
    69
                page=gr[0][6:-6]
alpar@10
    70
                fo.write("\page %s %s%s\n"%(page,
alpar@10
    71
                                            format_sec(ind[page][0]),gr[4]))
alpar@10
    72
            elif gr[1]:
alpar@10
    73
                sec=gr[1][5:-5]
alpar@10
    74
                fo.write("\section %s %s%s\n"%(sec,
alpar@10
    75
                                               format_sec(ind[sec][0]),gr[4]))
alpar@10
    76
            elif gr[2]:
alpar@10
    77
                prev_page=ind[page][1]
alpar@13
    78
                if prev_page:
alpar@14
    79
                    prev_str= ( '<< \\ref '+prev_page+' ')
alpar@13
    80
                else:
alpar@13
    81
                    prev_str=''
alpar@10
    82
                next_page=ind[page][2]
alpar@13
    83
                if next_page:
alpar@14
    84
                    next_str= ( ' \\ref '+next_page+' >>')
alpar@13
    85
                else:
alpar@13
    86
                    next_str=''
kpeter@26
    87
                fo.write('%s| \\ref sec_toc "Home" |%s\n'%\
alpar@10
    88
                             (prev_str,next_str))
alpar@10
    89
            elif gr[3]:
kpeter@42
    90
                secs = [ x for x in toc ]
kpeter@42
    91
                secs.sort(compare_sec)
alpar@10
    92
                for num in secs:
alpar@15
    93
                    fo.write("%s - \\ref %s\n"%('  '*((len(ind[toc[num]][0]))),
alpar@15
    94
                                                toc[num]))
alpar@10
    95
            else:
alpar@10
    96
                fo.write(gr[4]+'\n')
alpar@10
    97
        fo.close()