scripts/titlegen.py
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 14 Feb 2010 22:19:50 +0100
changeset 27 b453a59230c8
parent 17 0b3b26cd1cea
child 42 6df2bf124af4
permissions -rwxr-xr-x
Rework and extend the section of basic concepts
     1 #! /usr/bin/env python
     2 
     3 import sys
     4 import os
     5 import copy
     6 import re
     7 
     8 def sec_inc(section, lev):
     9     while len(section)<lev:
    10         section.append(0)
    11     section[lev-1]+=1
    12     section=section[:lev]
    13     return section
    14 
    15 def format_sec(sec):
    16     s=""
    17     for i in sec:
    18         s+=str(i)+'.'
    19     s=s[:-1]
    20     return s
    21 
    22 section = [];
    23 toc={}
    24 ind={}
    25 
    26 prev_page=''
    27 for l in open("toc.txt").readlines():
    28     sl = l.split()
    29     if len(sl)==2 and len(sl[0])>0: 
    30         lev=len(sl[0])
    31         section=sec_inc(section,lev)
    32         t_sec=copy.copy(section)
    33         t_link=sl[1];
    34         print format_sec(t_sec),t_link
    35         ind[t_link]=[t_sec,'','']
    36         if lev==1:
    37             ind[t_link][1]=prev_page
    38             if prev_page:
    39                 ind[prev_page][2]=t_link
    40             prev_page=t_link
    41         toc[format_sec(t_sec)]=t_link
    42 
    43 for doxfile in os.listdir('.'):
    44     if doxfile[-4:]=='.dox':
    45         print 'Generate ',doxfile
    46         page=''
    47         fo=open(os.path.join("gen-dox",doxfile),"w")
    48         for l in open(doxfile).readlines():
    49             gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(^[[]SEC[]].*[[]SEC[]])?(^[[]TRAILER[]])?(^[[]TOC[]])?(.*)$", l).groups()
    50             if gr[0]:
    51                 page=gr[0][6:-6]
    52                 fo.write("\page %s %s%s\n"%(page,
    53                                             format_sec(ind[page][0]),gr[4]))
    54             elif gr[1]:
    55                 sec=gr[1][5:-5]
    56                 fo.write("\section %s %s%s\n"%(sec,
    57                                                format_sec(ind[sec][0]),gr[4]))
    58             elif gr[2]:
    59                 prev_page=ind[page][1]
    60                 if prev_page:
    61                     prev_str= ( '<< \\ref '+prev_page+' ')
    62                 else:
    63                     prev_str=''
    64                 next_page=ind[page][2]
    65                 if next_page:
    66                     next_str= ( ' \\ref '+next_page+' >>')
    67                 else:
    68                     next_str=''
    69                 fo.write('%s| \\ref sec_toc "Home" |%s\n'%\
    70                              (prev_str,next_str))
    71             elif gr[3]:
    72                 secs = [ x for x in toc]
    73                 secs.sort()
    74                 for num in secs:
    75                     fo.write("%s - \\ref %s\n"%('  '*((len(ind[toc[num]][0]))),
    76                                                 toc[num]))
    77             else:
    78                 fo.write(gr[4]+'\n')
    79         fo.close()