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