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