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