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