]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
use thin black <hr>.
[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
56 <p>You're looking at a page with some LilyPond samples.  These files
57 are also included in the distribution. The output is completely
58 generated from the <tt>.ly</tt> source file, without any further touch
59 up.
60
61 <p>
62
63 The pictures are 90 dpi anti-aliased snapshots of the printed output.
64 If you want a better impression of the appearance, do print out one of
65 the PDF or PostScript files; they use scalable fonts, and should look
66 good at any resolution.
67
68 """
69
70 headertext_nopics= r"""
71 <p>Nothing to be seen here, move along.
72 """
73
74 #
75 # FIXME breaks on multiple strings.
76 #
77 def read_lilypond_header (fn):
78         s = open(fn).read ()
79         s = re.sub('%.*$', '', s)
80         s = re.sub('\n', ' ', s)                
81
82         dict = {}
83         m = re.search (r"""\\header\s*{([^}]*)}""", s)
84
85         if m:
86                         s = m.group(1)
87         else:
88                         return dict
89
90         while s:
91                 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
92                 if m == None:
93                         s = ''
94                 else:
95                         s = s[m.end (0):]
96                         left  = m.group  (1)
97                         right = m.group (2)
98
99                         left = re.sub ('"', '', left)
100                         right = re.sub ('"', '', right)
101                         dict[left] = right
102
103         return dict
104
105 def help ():
106         sys.stdout.write (r"""Usage: mutopia-index [OPTIONS] INFILE OUTFILE
107 Generate index for mutopia.
108
109 Options:
110   -h, --help                 print this help
111   -o, --output=FILE          write output to file
112   -s, --subdirs=DIR          add subdir
113       --suffix=SUF           specify suffix
114       
115 """
116                                           )
117         sys.exit (0)
118
119 # ugh.
120 def gen_list(inputs, filename):
121         print "generating HTML list %s\n" % filename
122         if filename:
123                 list = open(filename, 'w')
124         else:
125                 list = sys.stdout
126         list.write ('<html><head><title>Rendered Examples</title>\n')
127         list.write ('<style type="text/css">\n')
128         list.write ('hr { border:0; height:1; color: #000000; background-color: #000000; }\n')
129         list.write ('</style></head>\n')
130
131         list.write ('<body bgcolor=white>\n')
132         
133         if inputs:
134                         list.write (headertext)
135         else:
136                         list.write (headertext_nopics)
137
138
139         for ex in inputs:
140                 (base, ext) = os.path.splitext (ex)
141                 (base, ext2) = os.path.splitext (base)          
142                 ext = ext2 + ext
143                 
144                 header = read_lilypond_header(ex)
145                 def read_dict(s, default, h =header):
146                                 try:
147                                         ret = h[s]
148                                 except KeyError:
149                                         ret = default
150                                 return ret
151                 head = read_dict('title', os.path.basename (base))
152                 composer = read_dict('composer', '')
153                 desc = read_dict('description', '')
154                 list.write('<hr>\n')
155                 list.write('<h1>%s</h1>\n' % head);
156                 if composer:
157                         list.write('<h2>%s</h2>\n' % composer)
158                 if desc:
159                         list.write('%s<p>' % desc)
160                 list.write ('<ul>\n')
161                 def list_item(filename, desc, type, l = list):
162                         if file_exist_b(filename):
163                                 
164                                 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
165
166                                 # todo: include warning if it uses \include
167                                 # files.
168                                 
169                                 size=os.stat(filename)[stat.ST_SIZE]
170                                 kB=(size + 512) / 1024
171                                 if kB:
172                                         l.write (' (%s %d kB)' % (type, kB))
173                                 else:
174                                         l.write (' (%s %d characters)' % (type, size))
175                                 pictures = ['jpeg', 'png', 'xpm']
176                                 l.write ('\n')
177
178                 list_item(base + ext, 'The input', 'ASCII')
179                 for pageno in range(1,100):
180                         f  = base + '-page%d.png' % pageno
181                         if not file_exist_b (f):
182                                 break
183                         list_item(f, 'See a picture of page %d' % pageno, 'png')
184                 list_item(base + '.pdf', 'Print', 'PDF')
185                 list_item(base + '.ps.gz', 'Print', 'gzipped PostScript')
186                 list_item(base + '.midi', 'Listen', 'MIDI')
187                 list.write ("</ul>\n");
188
189         list.write('</body></html>\n');
190         list.close()
191
192 import getopt
193
194 (options, files) = getopt.getopt(sys.argv[1:], 
195   'ho:', ['help', 'output='])
196 outfile = 'examples.html'
197
198 subdirs =[]
199 for opt in options:
200         o = opt[0]
201         a = opt[1]
202         if o == '--help' or o == '-h':
203                 help()
204         elif o == '--output' or o == '-o':
205                 outfile = a
206
207 dirs  = []
208 for f in files:
209         dirs = dirs + find ('out-www', f)
210
211 if not dirs:
212         dirs = ['.']
213
214 allfiles = []
215
216 for d in dirs:
217         allfiles = allfiles + find ('*.ly.txt', d)
218
219 gen_list (allfiles, outfile)
220