]> git.donarmstrong.com Git - lilypond.git/blob - scripts/auxiliar/node-menuify.py
Add '-dcrop' option to ps and svg backends
[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 # sanity check for debugging
43 #for node in nodes:
44 #    print ' '*2*node[0] + node[1]
45
46 def getMenuFor(node_name):
47     i = 0
48     while nodes[i][1] != node_name:
49         i += 1
50     startIndex = i + 1
51     findType = nodes[i][0] + 1
52     menu = []
53     for i in range(startIndex, len(nodes)):
54         currentSectionType = nodes[i][0]
55         currentNodeName = nodes[i][1]
56         if currentSectionType < findType:
57             break
58         elif currentSectionType == findType:
59             menu.append(currentNodeName)
60         else:
61             pass
62     return menu
63
64 # rewrite file with new menus from TOC
65 outfile = open(infile, 'w')
66
67 lastNode = ''
68 line_index = 0
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'):
78             line_index += 1
79
80         # write new menu entries
81         menu = getMenuFor(lastNode)
82         for item in menu:
83             node_formatted = '* ' + item + '::\n'
84             outfile.write( node_formatted )
85
86         line = lines[line_index]
87     line_index += 1
88     # write normal line.  Removes tabs and spaces; leaves EOL
89     outfile.write(line.rstrip('\t '))
90 outfile.close()
91