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 # sanity check for debugging
44 # print ' '*2*node[0] + node[1]
46 def getMenuFor(node_name):
48 while nodes[i][1] != node_name:
51 findType = nodes[i][0] + 1
53 for i in range(startIndex, len(nodes)):
54 currentSectionType = nodes[i][0]
55 currentNodeName = nodes[i][1]
56 if currentSectionType < findType:
58 elif currentSectionType == findType:
59 menu.append(currentNodeName)
64 # rewrite file with new menus from TOC
65 outfile = open(infile, 'w')
69 while line_index < len(lines):
70 line = lines[ line_index ]
71 if line.startswith('@node'):
72 lastNode = line[6:].rstrip()
73 if line.startswith('@menu'):
74 outfile.write('@menu\n')
75 # skip over existing menu
76 # ASSUME: every @menu has a proper @end menu
77 while not lines[line_index].startswith('@end menu'):
80 # write new menu entries
81 menu = getMenuFor(lastNode)
83 node_formatted = '* ' + item + '::\n'
84 outfile.write( node_formatted )
86 line = lines[line_index]
88 # write normal line. Removes tabs and spaces; leaves EOL
89 outfile.write(line.rstrip('\t '))