(read_dict): remove "example file:"
[lilypond.git] / buildscripts / mutopia-index.py
blobca23fe6452fb6b521f61b0e8669bd6e9ec835d4a
1 #!@PYTHON@
2 # mutopia-index.py
4 name = 'mutopia-index'
6 import fnmatch
7 import os
9 _debug = 0
11 _prune = ['(*)']
13 def find(pattern, dir = os.curdir):
14 list = []
15 names = os.listdir(dir)
16 names.sort()
17 for name in names:
18 if name in (os.curdir, os.pardir):
19 continue
20 fullname = os.path.join(dir, name)
21 if fnmatch.fnmatch(name, pattern):
22 list.append(fullname)
23 if os.path.isdir(fullname) and not os.path.islink(fullname):
24 for p in _prune:
25 if fnmatch.fnmatch(name, p):
26 if _debug: print "skip", `fullname`
27 break
28 else:
29 if _debug: print "descend into", `fullname`
30 list = list + find(pattern, fullname)
31 return list
34 import re
35 import os
36 import sys
37 import stat
39 def gulp_file (fn):
40 try:
41 f = open (fn)
42 except:
43 raise 'not there' , fn
44 return f.read ()
46 def file_exist_b (fn):
47 try:
48 f = open (fn)
49 return 1
50 except:
51 return 0
54 headertext= r"""
55 <p>You're looking at a page with some LilyPond samples.
56 These files are also included in the distribution. The output is
57 completely generated by LilyPond, without any touch up by humans.
59 <p>The PostScript files were generated using TeX and dvips at 600 dpi.
60 The pictures are 90 dpi anti-aliased snapshots of the printed output.
61 The images are in PNG format, and should be viewable with any current
62 browser.
64 <p>If you want a better impression of the appearance, do print out one of
65 the postscript files.
66 """
68 headertext_nopics= r"""
69 <p>Nothing to be seen here, move along.
70 """
73 # FIXME breaks on multiple strings.
75 def read_lilypond_header (fn):
76 s = open(fn).read ()
77 s = re.sub('%.*$', '', s)
78 s = re.sub('\n', ' ', s)
80 dict = {}
81 m = re.search (r"""\\header\s*{([^}]*)}""", s)
83 if m:
84 s = m.group(1)
85 else:
86 return dict
88 while s:
89 m = re.search (r"""\s*(\S+)\s*=\s*([^;]+)\s*;""", s)
90 if m == None:
91 s = ''
92 else:
93 s = s[m.end (0):]
94 left = m.group (1)
95 right = m.group (2)
97 left = re.sub ('"', '', left)
98 right = re.sub ('"', '', right)
99 dict[left] = right
101 return dict
103 def help ():
104 sys.stdout.write (r"""Usage: mutopia-index [options] INFILE OUTFILE
105 Generate index for mutopia\n
106 Options:
107 -h, --help print this help
108 -o,-output=FILE write output to file.
109 -s, --subdirs=DIR add subdir
110 --suffix=SUF specify suffix"""
112 sys.exit (0)
114 # ugh.
115 def gen_list(inputs, filename):
116 print "generating HTML list %s\n" % filename
117 if filename:
118 list = open(filename, 'w')
119 else:
120 list = sys.stdout
121 list.write ('<html><title>Rendered Examples</title>\n')
122 list.write ('<body bgcolor=white>\n')
124 if inputs:
125 list.write (headertext)
126 else:
127 list.write (headertext_nopics)
130 for ex in inputs:
131 (base, ext) = os.path.splitext (ex)
132 (base, ext2) = os.path.splitext (base)
133 ext = ext2 + ext
135 print '%s, ' % ex
136 header = read_lilypond_header(ex)
138 def read_dict(s, default, h =header):
139 try:
140 ret = h[s]
141 except KeyError:
142 ret = default
143 return ret
144 head = read_dict('title', os.path.basename (base))
145 composer = read_dict('composer', '')
146 desc = read_dict('description', '')
147 list.write('<hr>\n')
148 list.write('<h1>%s</h1>\n' % head);
149 if composer:
150 list.write('<h2>%s</h2>\n' % composer)
151 if desc:
152 list.write('%s<p>' % desc)
153 list.write ('<ul>\n')
154 def list_item(filename, desc, type, l = list):
155 if file_exist_b(filename):
157 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
159 # todo: include warning if it uses \include
160 # files.
162 size=os.stat(filename)[stat.ST_SIZE]
163 kB=(size + 512) / 1024
164 if kB:
165 l.write (' (%s %d kB)' % (type, kB))
166 else:
167 l.write (' (%s %d characters)' % (type, size))
168 pictures = ['jpeg', 'png', 'xpm']
169 l.write ('\n')
171 list_item(base + ext, 'The input', 'ASCII')
172 for pageno in range(1,100):
173 f = base + '-page%d.png' % pageno
174 if not file_exist_b (f):
175 break
176 list_item(f, 'See a picture of page %d' % pageno, 'png')
177 list_item(base + '.pdf', 'Print', 'PDF')
178 list_item(base + '.ps.gz', 'Print', 'gzipped PostScript')
179 list_item(base + '.midi', 'Listen', 'MIDI')
180 list.write ("</ul>\n");
182 list.write('</body></html>\n');
183 list.close()
185 import getopt
187 (options, files) = getopt.getopt(sys.argv[1:],
188 'ho:', ['help', 'output='])
189 outfile = 'examples.html'
191 subdirs =[]
192 for opt in options:
193 o = opt[0]
194 a = opt[1]
195 if o == '--help' or o == '-h':
196 help()
197 elif o == '--output' or o == '-o':
198 outfile = a
200 dirs = []
201 for f in files:
202 dirs = dirs + find ('out-www', f)
204 if not dirs:
205 dirs = ['.']
207 allfiles = []
209 for d in dirs:
210 allfiles = allfiles + find ('*.ly.txt', d)
212 print allfiles
214 gen_list (allfiles, outfile)