]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/node-menuify.py
Script: fix 776: python regeneration of @menu
[lilypond.git] / scripts / auxiliar / node-menuify.py
1 #!/usr/bin/env python
2 import sys
3
4 try:
5     infile = sys.argv[1]
6     lines = open(infile).readlines()
7 except:
8     print "ERROR: need a filename"
9     sys.exit(1)
10
11 nodes = []
12
13 # Generate TOC
14 for i in range(len(lines)):
15     line = lines[i]
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 ')):
23             section_type = 1
24         elif (section_line.startswith('@section ') or
25               section_line.startswith('@unnumberedsec ') or
26               section_line.startswith('@appendixsec ')):
27             section_type = 2
28         elif (section_line.startswith('@subsection ') or
29               section_line.startswith('@unnumberedsubsec ') or
30               section_line.startswith('@appendixsubsec ')):
31             section_type = 3
32         elif (section_line.startswith('@subsubsection ') or
33               section_line.startswith('@unnumberedsubsubsec ') or
34               section_line.startswith('@appendixsubsubsec ')):
35             section_type = 4
36         else:
37             print "ERROR: unknown sectioning command"
38             print section_line
39             sys.exit(1)
40         nodes.append( (section_type, node_name) )
41
42 # rewrite file with new menus from TOC
43 outfile = open(infile, 'w')
44 line_index = 0
45 toc_index = 0
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'):
52             line_index += 1
53
54         # write new menu entries
55         menu_type = nodes[toc_index][0]
56         i = toc_index
57         while nodes[i][0] == menu_type:
58             i += 1
59             if i >= len(nodes):
60                 break
61         added = 0
62         while True:
63             if i >= len(nodes):
64                 added = 1
65                 break
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 )
71                 added += 1
72             if section_type == menu_type:
73                 added += 1
74                 break
75             i += 1
76         toc_index += added
77         line = lines[line_index]
78     line_index += 1
79     # write normal line.  Removes tabs and spaces; leaves EOL
80     outfile.write(line.rstrip('\t '))
81 outfile.close()
82