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