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