LSR: Update.
[lilypond.git] / buildscripts / mutopia-index.py
blob50e4ebbf29b5b9355694937acbf228dde47fdd1e
1 #!/usr/bin/env python
2 # mutopia-index.py
4 import fnmatch
5 import getopt
6 import os
7 import re
8 import stat
9 import sys
11 def find (pat, dir):
12 f = os.popen ('find %s -name "%s"'% (dir, pat))
13 lst = []
14 for a in f.readlines():
15 a = a[:-1]
16 lst.append (a)
17 return lst
20 junk_prefix = 'out-www/'
22 headertext= r"""
24 <h1>LilyPond samples</h1>
27 <p>You are looking at a page with some LilyPond samples. These files
28 are also included in the distribution. The output is completely
29 generated from the source file, without any further touch up.
31 <p>
33 The pictures are 90 dpi anti-aliased snapshots of the printed output.
34 For a good impression of the quality print out the PDF file.
35 """
37 headertext_nopics= r"""
38 <p>No examples were found in this directory.
39 """
42 # FIXME breaks on multiple strings.
44 def read_lilypond_header (fn):
45 s = open (fn).read ()
46 s = re.sub ('%.*$', '', s)
47 s = re.sub ('\n', ' ', s)
49 dict = {}
50 m = re.search (r"""\\header\s*{([^}]*)}""", s)
52 if m:
53 s = m.group (1)
54 else:
55 return dict
57 while s:
58 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
59 if m == None:
60 s = ''
61 else:
62 s = s[m.end (0):]
63 left = m.group (1)
64 right = m.group (2)
66 left = re.sub ('"', '', left)
67 right = re.sub ('"', '', right)
68 dict[left] = right
70 return dict
72 def help ():
73 sys.stdout.write (r'''Usage: mutopia-index [OPTIONS] INFILE OUTFILE
74 Generate index for mutopia.
76 Options:
77 -h, --help print this help
78 -o, --output=FILE write output to file
79 -s, --subdirs=DIR add subdir
80 --suffix=SUF specify suffix
82 ''')
83 sys.exit (0)
85 # ugh.
86 def gen_list (inputs, file_name):
87 sys.stderr.write ("generating HTML list %s" % file_name)
88 sys.stderr.write ('\n')
89 if file_name:
90 list = open (file_name, 'w')
91 else:
92 list = sys.stdout
93 list.write ('''<html><head><title>Rendered Examples</title>
94 <style type="text/css">
95 hr { border:0; height:1; color: #000000; background-color: #000000; }\n
96 </style>
97 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
98 </head>''')
100 list.write ('<body bgcolor=white>\n')
102 if inputs:
103 list.write (headertext)
104 else:
105 list.write (headertext_nopics)
107 for ex in inputs:
108 print ex
110 (base, ext) = os.path.splitext (ex)
111 (base, ext2) = os.path.splitext (base)
112 ext = ext2 + ext
114 header = read_lilypond_header (ex)
115 head = header.get ('title', os.path.basename (base))
116 composer = header.get ('composer', '')
117 desc = header.get ('description', '')
118 list.write ('<hr>\n')
119 list.write ('<h1>%s</h1>\n' % head);
120 if composer:
121 list.write ('<h2>%s</h2>\n' % composer)
122 if desc:
123 list.write ('%s<p>' % desc)
124 list.write ('<ul>\n')
126 def list_item (file_name, desc, type, lst = list):
127 if os.path.isfile (file_name):
128 lst.write ('<li><a href="%s">%s</a>'
129 % (re.sub (junk_prefix, '', file_name), desc))
131 # FIXME: include warning if it uses \include
132 # files.
134 size = os.stat (file_name)[stat.ST_SIZE]
135 kB = (size + 512) / 1024
136 if kB:
137 lst.write (' (%s %d kB)' % (type, kB))
138 else:
139 lst.write (' (%s %d characters)'
140 % (type, size))
141 pictures = ['jpeg', 'png', 'xpm']
142 lst.write ('\n')
143 else:
144 print "cannot find" , `file_name`
146 list_item (base + ext, 'The input', 'ASCII')
148 pages_found = 0
149 for page in range (1, 100):
150 f = base + '-page%d.png' % page
152 if not os.path.isfile (f):
153 break
154 pages_found += 1
155 list_item (f, 'See a picture of page %d' % page, 'png')
157 if pages_found == 0 and os.path.exists (base + '.png'):
158 list_item (base + ".png",
159 'See a picture', 'png')
162 list_item (base + '.pdf', 'Print', 'PDF')
163 list_item (base + '.midi', 'Listen', 'MIDI')
164 list.write ('</ul>\n');
166 list.write ('</body></html>\n');
167 list.close ()
169 (options, files) = getopt.getopt (sys.argv[1:],
170 'ho:', ['help', 'output='])
171 outfile = 'examples.html'
173 subdirs = []
174 for (o, a) in options:
175 if o == '--help' or o == '-h':
176 help ()
177 elif o == '--output' or o == '-o':
178 outfile = a
180 dirs = []
181 for f in files:
182 dirs += find ('out-www', f)
184 if not dirs:
185 dirs = ['.']
187 allfiles = []
189 for d in dirs:
190 allfiles += find ('*.ly', d)
192 allfiles = [f for f in allfiles
193 if not f.endswith ('snippet-map.ly')
194 and not re.search ('lily-[0-9a-f]+', f)
195 and 'musicxml' not in f]
197 gen_list (allfiles, outfile)