scripts/titlegen.py
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 06 Apr 2012 14:51:32 +0200
changeset 60 202688f8024a
parent 42 6df2bf124af4
permissions -rwxr-xr-x
Fix a type in Secion LP. Thanks for Jacint
     1 #! /usr/bin/env python
     2 #
     3 # This file is a part of LEMON, a generic C++ optimization library.
     4 #
     5 # Copyright (C) 2003-2010
     6 # Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7 # (Egervary Research Group on Combinatorial Optimization, EGRES).
     8 #
     9 # Permission to use, modify and distribute this software is granted
    10 # provided that this copyright notice appears in all copies. For
    11 # precise terms see the accompanying LICENSE file.
    12 #
    13 # This software is provided "AS IS" with no warranty of any kind,
    14 # express or implied, and with no claim as to its suitability for any
    15 # purpose.
    16 #
    17 
    18 import sys
    19 import os
    20 import copy
    21 import re
    22 
    23 max_sec_number=100
    24 max_sec_depth=4
    25 
    26 def sec_inc(section, lev):
    27     while len(section)<lev:
    28         section.append(0)
    29     section[lev-1]+=1
    30     section=section[:lev]
    31     return section
    32 
    33 def format_sec(sec):
    34     s=""
    35     for i in sec:
    36         s+=str(i)+'.'
    37     s=s[:-1]
    38     return s
    39 
    40 def compare_sec(id1, id2):
    41     id1=id1.split('.')
    42     c1=0
    43     for s in id1:
    44         c1=c1*max_sec_number+int(s)
    45     for i in range(len(id1), max_sec_depth+1):
    46         c1*=max_sec_number
    47     id2=id2.split('.')
    48     c2=0
    49     for s in id2:
    50         c2=c2*max_sec_number+int(s)
    51     for i in range(len(id2), max_sec_depth+1):
    52         c2*=max_sec_number
    53     return c1-c2
    54 
    55 section = [];
    56 toc={}
    57 ind={}
    58 page_files={}
    59 ordered_pages = []
    60 
    61 for doxfile in os.listdir('.'):
    62     if doxfile[-4:]=='.dox':
    63         for l in open(doxfile).readlines():
    64             gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(.*)$", l).groups()
    65             if gr[0]:
    66                 page=gr[0][6:-6]
    67                 page_files[page]=doxfile
    68 
    69 prev_page=''
    70 for l in open("toc.txt").readlines():
    71     sl = l.split()
    72     if len(sl)==2 and len(sl[0])>0: 
    73         lev=len(sl[0])
    74         section=sec_inc(section,lev)
    75         t_sec=copy.copy(section)
    76         t_link=sl[1];
    77         print format_sec(t_sec),t_link
    78         ind[t_link]=[t_sec,'','']
    79         if lev==1:
    80             ind[t_link][1]=prev_page
    81             if prev_page:
    82                 ind[prev_page][2]=t_link
    83             prev_page=t_link
    84             ordered_pages.append(t_link)
    85         toc[format_sec(t_sec)]=t_link
    86 
    87 for doxfile in os.listdir('.'):
    88     if doxfile[-4:]=='.dox':
    89         print 'Generate ',doxfile
    90         page=''
    91         fo=open(os.path.join("gen-dox",doxfile),"w")
    92         for l in open(doxfile).readlines():
    93             gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(^[[]SEC[]].*[[]SEC[]])?(^[[]TRAILER[]])?(^[[]TOC[]])?(.*)$", l).groups()
    94             if gr[0]:
    95                 page=gr[0][6:-6]
    96                 fo.write("\page %s %s%s\n"%(page,
    97                                             format_sec(ind[page][0]),gr[4]))
    98             elif gr[1]:
    99                 sec=gr[1][5:-5]
   100                 fo.write("\section %s %s%s\n"%(sec,
   101                                                format_sec(ind[sec][0]),gr[4]))
   102             elif gr[2]:
   103                 prev_page=ind[page][1]
   104                 if prev_page:
   105                     prev_str= ( '<< \\ref '+prev_page+' ')
   106                 else:
   107                     prev_str=''
   108                 next_page=ind[page][2]
   109                 if next_page:
   110                     next_str= ( ' \\ref '+next_page+' >>')
   111                 else:
   112                     next_str=''
   113                 fo.write('%s| \\ref sec_toc "Home" |%s\n'%\
   114                              (prev_str,next_str))
   115             elif gr[3]:
   116                 secs = [ x for x in toc ]
   117                 secs.sort(compare_sec)
   118                 for num in secs:
   119                     fo.write("%s - \\ref %s\n"%('  '*((len(ind[toc[num]][0]))),
   120                                                 toc[num]))
   121             else:
   122                 fo.write(gr[4]+'\n')
   123         fo.close()
   124 
   125 fpdf=open(os.path.join("gen-pdf-dox","full.dox"),"w")
   126 for doxfile in [page_files[p] for p in ordered_pages]:
   127     page=''
   128     for l in open(doxfile).readlines():
   129         gr = re.match(r"(^[[]PAGE[]].*[[]PAGE[]])?(^[[]SEC[]].*[[]SEC[]])?(^[[]TRAILER[]])?(^[[]TOC[]])?(.*)$", l).groups()
   130         if gr[0]:
   131             page=gr[0][6:-6]
   132             fpdf.write("\page %s %s\n"%(page,gr[4]))
   133         elif gr[1]:
   134             sec=gr[1][5:-5]
   135             fpdf.write("\section %s %s\n"%(sec,gr[4]))
   136         elif gr[2]:
   137             pass
   138         elif gr[3]:
   139             pass
   140         else:
   141             fpdf.write(gr[4]+'\n')
   142 fpdf.close()