]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
2e5d3b5e35ce558d830ca22bfb8f91932c4c4442
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 name = 'mutopia-index'
5
6 import fnmatch
7 import os
8
9 _debug = 0
10
11 _prune = ['(*)']
12
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
32
33
34 import re
35 import os
36 import sys
37 import stat
38
39 def gulp_file (fn):
40         try:
41                 f = open (fn)
42         except:
43                 raise 'not there' , fn
44         return f.read ()
45
46 def file_exist_b (fn):
47         try:
48                 f = open (fn)
49                 return 1
50         except:
51                 return 0
52
53
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.
58
59 <p>The PostScript files were generated using TeX and dvips at 600 dpi.
60 The pictures are 90dpi anti-aliased snapshots of the printed output.
61 The images are in PNG format, and should be viewable with any current
62 browser.
63
64 <p>If you want a better impression of the appearance, do print out one of
65 the postscript files.
66 """
67
68
69 #
70 # FIXME breaks on multiple strings.
71 #
72 def read_lilypond_header (fn):
73         s = open(fn).read ()
74         s = re.sub('%.*$', '', s)
75         s = re.sub('\n', ' ', s)                
76
77         dict = {}
78         m = re.search (r"""\\header\s*{([^}]*)}""", s)
79
80         if m:
81                         s = m.group(1)
82         else:
83                         return dict
84
85         while s:
86                 m = re.search (r"""\s*(\S+)\s*=\s*([^;]+)\s*;""", s)
87                 if m == None:
88                         s = ''
89                 else:
90                         s = s[m.end (0):]
91                         left  = m.group  (1)
92                         right = m.group (2)
93
94                         left = re.sub ('"', '', left)
95                         right = re.sub ('"', '', right)
96                         dict[left] = right
97
98         return dict
99
100 def help ():
101         sys.stdout.write (r"""Usage: mutopia-index [options] INFILE OUTFILE
102 Generate index for mutopia\n
103 Options:
104   -h, --help                 print this help
105   -o,-output=FILE            write output to file.
106   -s, --subdirs=DIR          add subdir
107   --suffix=SUF                   specify suffix"""
108                                           )
109         sys.exit (0)
110
111 # ugh.
112 def gen_list(inputs, filename):
113         print "generating HTML list %s\n" % filename
114         if filename:
115                 list = open(filename, 'w')
116         else:
117                 list = sys.stdout
118         list.write ('<html><title>Rendered Examples</title>\n')
119         list.write ('<body bgcolor=white>\n')
120         
121         if inputs:
122                         list.write (headertext)
123         else:
124                         list.write (headertext_nopics)
125
126
127         for ex in inputs:
128                 (base, ext) = os.path.splitext (ex)
129                 (base, ext2) = os.path.splitext (base)          
130                 ext = ext2 + ext
131                 
132                 print '%s, ' % ex
133                 header = read_lilypond_header(ex)
134                 
135                 def read_dict(s, default, h =header):
136                                 try:
137                                         ret = h[s]
138                                 except KeyError:
139                                         ret = default
140                                 return ret
141                 head = read_dict('title', os.path.basename (base))
142                 composer = read_dict('composer', '')
143                 desc = read_dict('description', '')
144                 list.write('<hr>\n')
145                 list.write('<h1>example file: %s</h1>\n' % head);
146                 if composer <> '':
147                         list.write('<h2>%s</h2>\n' % composer)
148                 if desc <> '':
149                         list.write('%s<p>' % desc)
150                 list.write ('<ul>\n')
151                 def list_item(filename, desc, type, l = list):
152                         if file_exist_b(filename):
153                                 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
154                                 size=os.stat(filename)[stat.ST_SIZE]
155                                 kB=(size + 512) / 1024
156                                 if kB:
157                                         l.write (' (%s %dkB)' % (type, kB))
158                                 else:
159                                         l.write (' (%s %dcharacters)' % (type, size))
160                                 pictures = ['jpeg', 'png', 'xpm']
161                                 l.write ('\n')
162
163                 list_item(base + ext, 'The input', 'ASCII')
164                 for pageno in range(1,100):
165                         f  = base + '-page%d.png' % pageno
166                         if not file_exist_b (f):
167                                 break
168                         list_item(f, 'See a picture of page %d' % pageno, 'png')
169                 list_item(base + '.pdf', 'Print ', 'PDF')
170                 list_item(base + '.ps.gz', 'Print ', 'gzipped PostScript')
171                 list_item(base + '.midi', 'Listen', 'MIDI')
172                 list.write ("</ul>\n");
173
174         list.write('</body></html>\n');
175         list.close()
176
177 import getopt
178
179 (options, files) = getopt.getopt(sys.argv[1:], 
180   'ho:', ['help', 'output='])
181 outfile = 'examples.html'
182
183 subdirs =[]
184 for opt in options:
185         o = opt[0]
186         a = opt[1]
187         if o == '--help' or o == '-h':
188                 help()
189         elif o == '--output' or o == '-o':
190                 outfile = a
191
192 dirs  = []
193 for f in files:
194         dirs = dirs + find ('out-www', f)
195
196 if not dirs:
197         dirs = ['.']
198
199 allfiles = []
200
201 for d in dirs:
202         allfiles = allfiles + find ('*.ly.txt', d)
203
204 print allfiles
205
206 gen_list (allfiles, outfile)
207