6 lines = open(infile).readlines()
8 print "ERROR: need a filename"
14 for i in range(len(lines)):
16 if line.startswith('@node '):
17 node_name = line[6:].rstrip()
18 # ASSUME: the last line of the file is not @node
19 section_line = lines[i+1]
20 if (section_line.startswith('@chapter ') or
21 section_line.startswith('@unnumbered ') or
22 section_line.startswith('@appendix ')):
24 elif (section_line.startswith('@section ') or
25 section_line.startswith('@unnumberedsec ') or
26 section_line.startswith('@appendixsec ')):
28 elif (section_line.startswith('@subsection ') or
29 section_line.startswith('@unnumberedsubsec ') or
30 section_line.startswith('@appendixsubsec ')):
32 elif (section_line.startswith('@subsubsection ') or
33 section_line.startswith('@unnumberedsubsubsec ') or
34 section_line.startswith('@appendixsubsubsec ')):
37 print "ERROR: unknown sectioning command"
40 nodes.append( (section_type, node_name) )
42 # rewrite file with new menus from TOC
43 outfile = open(infile, 'w')
46 while line_index < len(lines):
47 line = lines[ line_index ]
48 if line.startswith('@menu'):
49 outfile.write('@menu\n')
50 # ASSUME: every @menu has a proper @end menu
51 while not lines[line_index].startswith('@end menu'):
54 # write new menu entries
55 menu_type = nodes[toc_index][0]
57 while nodes[i][0] == menu_type:
66 section_type = nodes[i][0]
67 if section_type == menu_type+1:
68 node_name = nodes[i][1]
69 node_formatted = '* ' + node_name + '::\n'
70 outfile.write( node_formatted )
72 if section_type == menu_type:
77 line = lines[line_index]
79 # write normal line. Removes tabs and spaces; leaves EOL
80 outfile.write(line.rstrip('\t '))