]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
*** empty log message ***
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 import fnmatch
5 import getopt
6 import os
7 import os
8 import re
9 import stat
10 import sys
11
12 _debug = 0
13 _prune = ['(*)']
14
15 def find (pattern, dir = os.curdir):
16         lst = []
17         names = os.listdir (dir)
18         names.sort ()
19         for name in names:
20                 if name in (os.curdir, os.pardir):
21                         continue
22                 fullname = os.path.join (dir, name)
23                 if fnmatch.fnmatch (name, pattern):
24                         lst.append (fullname)
25                 if os.path.isdir (fullname) and not os.path.islink (fullname):
26                         for p in _prune:
27                                 if fnmatch.fnmatch (name, p):
28                                         if _debug:
29                                                 print "skip", `fullname`
30                                         break
31                         else:
32                                 if _debug:
33                                         print "descend into", `fullname`
34                                 lst = lst + find (pattern, fullname)
35         return lst
36
37 headertext= r"""
38
39 <h1>LilyPond samples</h1>
40  
41
42 <p>You're looking at a page with some LilyPond samples.  These files
43 are also included in the distribution. The output is completely
44 generated from the <tt>.ly</tt> source file, without any further touch
45 up.
46
47 <p>
48
49 The pictures are 90 dpi anti-aliased snapshots of the printed output.
50 If you want a better impression of the appearance, do print out one of
51 the PDF or PostScript files; they use scalable fonts, and should look
52 good at any resolution.
53
54 """
55
56 headertext_nopics= r"""
57 <p>Nothing to be seen here, move along.
58 """
59
60 #
61 # FIXME breaks on multiple strings.
62 #
63 def read_lilypond_header (fn):
64         s = open (fn).read ()
65         s = re.sub ('%.*$', '', s)
66         s = re.sub ('\n', ' ', s)               
67
68         dict = {}
69         m = re.search (r"""\\header\s*{([^}]*)}""", s)
70
71         if m:
72                         s = m.group (1)
73         else:
74                         return dict
75
76         while s:
77                 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
78                 if m == None:
79                         s = ''
80                 else:
81                         s = s[m.end (0):]
82                         left  = m.group  (1)
83                         right = m.group (2)
84
85                         left = re.sub ('"', '', left)
86                         right = re.sub ('"', '', right)
87                         dict[left] = right
88
89         return dict
90
91 def help ():
92         sys.stdout.write (r'''Usage: mutopia-index [OPTIONS] INFILE OUTFILE
93 Generate index for mutopia.
94
95 Options:
96   -h, --help                 print this help
97   -o, --output=FILE          write output to file
98   -s, --subdirs=DIR          add subdir
99       --suffix=SUF           specify suffix
100       
101 ''')
102         sys.exit (0)
103
104 # ugh.
105 def gen_list (inputs, file_name):
106         sys.stderr.write ("generating HTML list %s" % file_name)
107         sys.stderr.write ('\n')
108         if file_name:
109                 list = open (file_name, 'w')
110         else:
111                 list = sys.stdout
112         list.write ('''<html><head><title>Rendered Examples</title>
113 <style type="text/css">
114 hr { border:0; height:1; color: #000000; background-color: #000000; }\n
115 </style>
116 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
117 </head>''')
118
119         list.write ('<body bgcolor=white>\n')
120         
121         if inputs:
122                 list.write (headertext)
123         else:
124                 list.write (headertext_nopics)
125
126         for ex in inputs:
127                 (base, ext) = os.path.splitext (ex)
128                 (base, ext2) = os.path.splitext (base)          
129                 ext = ext2 + ext
130                 
131                 header = read_lilypond_header (ex)
132                 def read_dict (s, default, h = header):
133                                 try:
134                                         ret = h[s]
135                                 except KeyError:
136                                         ret = default
137                                 return ret
138                 head = read_dict ('title', os.path.basename (base))
139                 composer = read_dict ('composer', '')
140                 desc = read_dict ('description', '')
141                 list.write ('<hr>\n')
142                 list.write ('<h1>%s</h1>\n' % head);
143                 if composer:
144                         list.write ('<h2>%s</h2>\n' % composer)
145                 if desc:
146                         list.write ('%s<p>' % desc)
147                 list.write ('<ul>\n')
148
149                 def list_item (file_name, desc, type, lst = list):
150                         if os.path.isfile (file_name):
151                                 lst.write ('<li><a href="%s">%s</a>'
152                                            % (file_name, desc))
153
154                                 # FIXME: include warning if it uses \include
155                                 # files.
156                                 
157                                 size = os.stat (file_name)[stat.ST_SIZE]
158                                 kB = (size + 512) / 1024
159                                 if kB:
160                                         lst.write (' (%s %d kB)' % (type, kB))
161                                 else:
162                                         lst.write (' (%s %d characters)'
163                                                    % (type, size))
164                                 pictures = ['jpeg', 'png', 'xpm']
165                                 lst.write ('\n')
166
167                 list_item (base + ext, 'The input', 'ASCII')
168                 for page in range (1, 100):
169                         f = base + '-page%d.png' % page
170                         if not os.path.isfile (f):
171                                 break
172                         list_item (f, 'See a picture of page %d' % page, 'png')
173                 list_item (base + '.pdf', 'Print', 'PDF')
174                 list_item (base + '.midi', 'Listen', 'MIDI')
175                 list.write ('</ul>\n');
176
177         list.write ('</body></html>\n');
178         list.close ()
179
180 (options, files) = getopt.getopt (sys.argv[1:], 
181   'ho:', ['help', 'output='])
182 outfile = 'examples.html'
183
184 subdirs = []
185 for opt in options:
186         o = opt[0]
187         a = opt[1]
188         if o == '--help' or o == '-h':
189                 help ()
190         elif o == '--output' or o == '-o':
191                 outfile = a
192
193 dirs  = []
194 for f in files:
195         dirs = dirs + find ('out-www', f)
196
197 if not dirs:
198         dirs = ['.']
199
200 allfiles = []
201
202 for d in dirs:
203         allfiles = allfiles + find ('*.ly.txt', d)
204
205 gen_list (allfiles, outfile)
206