]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
release: 1.0.1
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 name = 'mutopia-index'
5
6 import regex
7 import os
8 import sys
9 sys.path.append ('@abs-step-bindir@')
10
11
12
13 header_regex = regex.compile('\\header[ \t\n]*{\([^}]*\)}')
14 header_entry_regex = regex.compile('[\n\t ]*\([^\n\t ]+\)[\n\t ]*=[\n \t]*\([^;]+\)[\n \t]*;')
15
16 #
17 # FIXME breaks on multiple strings.
18 #
19 def read_mudela_header (fn):
20         s = gulp_file(fn)
21         s = regsub.gsub('%.*$', '', s)
22         s = regsub.gsub('\n', ' ', s)   
23
24         dict = {}
25         if header_regex.search(s) <> -1:
26                 h = header_regex.group(1)
27         else:
28                 return dict
29
30         while regex.search('=', h) <> -1: 
31
32                 if header_entry_regex.search (h) == -1:
33
34                         raise 'format error'
35
36                 h = regsub.sub(header_entry_regex, '', h)
37                 left = header_entry_regex.group(1)
38                 right = header_entry_regex.group(2)
39
40                 right = regsub.gsub('\([^\\]\)\"', '\\1', right)
41                 right = regsub.gsub('^"', '', right)            
42                 left = regsub.gsub('\([^\\]\)\"', '', left)
43                 left = regsub.gsub('^"', '', left)
44
45                 dict[left] = right
46
47         return dict
48    
49
50
51
52 def help ():
53     sys.stdout.write ("Usage: " + name + " [options] INFILE OUTFILE\n"
54                  + "Generate index for mutopia\n\n"
55                  + "Options:\n"
56                  + "  -h, --help             print this help\n"
57                  + "  -p, --package=DIR      specify package\n"
58                  + "  --prefix=PRE           specify prefix\n"
59                  + "  -s, --subdirs=DIR      add subdir\n"
60                  + "  --suffix=SUF           specify suffix\n"
61                       )
62     sys.exit (0)
63
64 def gen_list(inputs, subdir, filename):
65     (pre, subdirs, post)=subdir
66     print "generating HTML list %s\n" % filename
67     list = open(filename, 'w')
68     list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
69     list.write ('<body bgcolor=white>')
70     if len(subdirs):
71         list.write  ('<h2>subdirectories</h2>')
72         list.write  ('<ul>')    
73         for ex in subdirs:
74             print 'subdir %s ' % ex
75             list.write ('<li><a href=%s/index.html>Subdirectory: %s</a></li>\n' % (pre + ex + post , ex))
76
77         list.write ('</ul>')
78
79     list.write('<h2>Contents of this directory</h2>\n');
80     list.write ('These example files are taken from the LilyPond distribution.\n'
81      'LilyPond currently only outputs TeX and MIDI. The pictures and\n'
82      'PostScript files were generated using TeX, Ghostscript and some\n'
83      'graphics tools.  The papersize used for these examples is A4.  The GIF\n'
84      'files have been scaled to eliminate aliasing.\n');
85
86
87     for ex in inputs:
88         ex_ext = '.ly'
89         print '%s, ' % ex
90         try:
91             header = read_mudela_header(ex + ex_ext + '.txt')
92         except:
93             ex_ext = '.fly'
94             header = read_mudela_header(ex + ex_ext + '.txt')
95         
96         def read_dict(s, default, h =header):
97                 try:
98                     ret = h[s]
99                 except KeyError:
100                     ret = default
101                 return ret
102         head = read_dict('title', ex)
103         composer = read_dict('composer', '')
104         desc = read_dict('description', '')
105         list.write('<hr>')
106         list.write('<h1>example file: %s</h1>' % head);
107         if composer <> '':
108             list.write('<h2>%s</h2>\n' % composer)
109         if desc <> '':
110             list.write('%s<p>' % desc)
111         list.write ('<ul>')
112         def list_item(filename, desc, l = list):
113             if file_exist_b(filename):
114                 l.write ('<li><a href=%s>%s</a>\n' % (filename, desc))
115             
116         list_item(ex + ex_ext + '.txt', 'The input')
117         for pageno in range(1,100):
118             f  = ex + '-page%d.gif' % pageno
119             if not file_exist_b (f):
120                 break
121             list_item(f, 'The output (picture, page %d)' % pageno)
122         list_item(ex + '.ps.gz', 'The output (gzipped PostScript)')
123         list_item(ex + '.midi', 'The output (MIDI)')
124         list.write ("</ul>");
125
126     list.write( "</BODY></HTML>");
127     list.close()
128
129 import getopt
130
131 (options, files) = getopt.getopt(sys.argv[1:], 
132   'hp:s:', ['help', 'subdirs=', 'suffix=', 'package=', 'prefix='])
133 subdir_pre=''
134 subdir_suf =''
135
136 subdirs =[]
137 for opt in options:
138     o = opt[0]
139     a = opt[1]
140     if o == '--subdirs' or o == '-s':
141         subdirs.append (a)
142     elif o == '--prefix':
143         subdir_pre = a
144     elif o == '-p' or o == '--package':
145         topdir = a
146     elif o == '--suffix':
147         subdir_suf = a
148
149     sys.path.append (topdir + '/stepmake/bin')
150     from packagepython import *
151     package = Package (topdir)
152     packager = Packager ()
153
154     from flower import *
155
156         
157 # huh?
158 allfiles = multiple_find (['*.*ly.txt'], '.')
159
160 gen_list (files, (subdir_pre, subdirs, subdir_suf), 'index.html')
161