[10] | 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 | return s |
---|
| 20 | |
---|
| 21 | section = []; |
---|
| 22 | toc={} |
---|
| 23 | ind={} |
---|
| 24 | |
---|
| 25 | prev_page='' |
---|
| 26 | for l in open("toc.txt").readlines(): |
---|
| 27 | sl = l.split() |
---|
| 28 | if len(sl)==2 and len(sl[0])>0: |
---|
| 29 | lev=len(sl[0]) |
---|
| 30 | section=sec_inc(section,lev) |
---|
| 31 | t_sec=copy.copy(section) |
---|
| 32 | t_link=sl[1]; |
---|
| 33 | print format_sec(t_sec),t_link |
---|
| 34 | ind[t_link]=[t_sec,'',''] |
---|
| 35 | if lev==1: |
---|
| 36 | ind[t_link][1]=prev_page |
---|
| 37 | if prev_page: |
---|
| 38 | ind[prev_page][2]=t_link |
---|
| 39 | prev_page=t_link |
---|
| 40 | toc[format_sec(t_sec)]=t_link |
---|
| 41 | |
---|
| 42 | for doxfile in os.listdir('.'): |
---|
| 43 | if doxfile[-4:]=='.dox': |
---|
| 44 | print 'Generate ',doxfile |
---|
| 45 | page='' |
---|
| 46 | fo=open(os.path.join("gen-dox",doxfile),"w") |
---|
| 47 | for l in open(doxfile).readlines(): |
---|
| 48 | gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(^[[]SEC[]].*[[]SEC[]])?(^[[]TRAILER[]])?(^[[]TOC[]])?(.*)$", l).groups() |
---|
| 49 | if gr[0]: |
---|
| 50 | page=gr[0][6:-6] |
---|
| 51 | fo.write("\page %s %s%s\n"%(page, |
---|
| 52 | format_sec(ind[page][0]),gr[4])) |
---|
| 53 | elif gr[1]: |
---|
| 54 | sec=gr[1][5:-5] |
---|
| 55 | fo.write("\section %s %s%s\n"%(sec, |
---|
| 56 | format_sec(ind[sec][0]),gr[4])) |
---|
| 57 | elif gr[2]: |
---|
| 58 | prev_page=ind[page][1] |
---|
| 59 | prev_str= ( '\\ref '+prev_page ) if prev_page else '' |
---|
| 60 | next_page=ind[page][2] |
---|
| 61 | next_str= ( '\\ref '+next_page ) if next_page else '' |
---|
| 62 | fo.write('<< %s | \\ref toc "Home" | %s >>\n'%\ |
---|
| 63 | (prev_str,next_str)) |
---|
| 64 | elif gr[3]: |
---|
| 65 | secs = [ x for x in toc] |
---|
| 66 | secs.sort() |
---|
| 67 | for num in secs: |
---|
| 68 | fo.write("%s - \\ref %s\n"%(' '*(len(num)-2),toc[num])) |
---|
| 69 | else: |
---|
| 70 | fo.write(gr[4]+'\n') |
---|
| 71 | fo.close() |
---|