]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
release: 1.3.28
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 name = 'mutopia-index'
5
6 import find
7 import re
8 import os
9 import sys
10 import stat
11
12 def gulp_file (fn):
13         try:
14                 f = open (fn)
15         except:
16                 raise 'not there' , fn
17         return f.read ()
18
19 def file_exist_b (fn):
20         try:
21                 f = open (fn)
22                 return 1
23         except:
24                 return 0
25
26
27 headertext= r"""
28 These example files are taken from the LilyPond distribution. 
29 LilyPond currently only outputs TeX and MIDI.  The pictures and 
30 PostScript files were generated using TeX, Ghostscript and some 
31 graphics tools.  The papersize used for these examples is A4. 
32 The  images are in PNG format, and should be viewable with any current browser.
33 We don't use GIFS due to <a href="http://www.gnu.org/philosophy/gif.html">patent problems</a>.
34 <p>
35 If you want an accurate impression of the output quality please <em>print
36 out</em> the samples first.
37 """
38
39 headertext_nopics = r"""This is a subdirectory of the LilyPond example
40 set.  We decided not to show any examples from this directory.  If you
41 want to view them, then you have to download LilyPond and compile them
42 yourself."""
43
44
45 #
46 # FIXME breaks on multiple strings.
47 #
48 def read_mudela_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*;""", 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\n
79 Options:
80   -h, --help                 print this help
81   -o,-output=FILE            write output to file.
82   -s, --subdirs=DIR          add subdir
83   --suffix=SUF                   specify suffix"""
84                                           )
85         sys.exit (0)
86
87 # ugh.
88 def gen_list(inputs, filename):
89         print "generating HTML list %s\n" % filename
90         if filename:
91                 list = open(filename, 'w')
92         else:
93                 list = sys.stdout
94         list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
95         list.write ('<body bgcolor=white>')
96         
97         if inputs:
98                         list.write (headertext)
99         else:
100                         list.write (headertext_nopics)
101
102
103         for ex in inputs:
104                 (base, ext) = os.path.splitext (ex)
105                 (base, ext2) = os.path.splitext (base)          
106                 ext = ext2 + ext
107                 
108                 print '%s, ' % ex
109                 header = read_mudela_header(ex)
110                 
111                 def read_dict(s, default, h =header):
112                                 try:
113                                         ret = h[s]
114                                 except KeyError:
115                                         ret = default
116                                 return ret
117                 head = read_dict('title', os.path.basename (base))
118                 composer = read_dict('composer', '')
119                 desc = read_dict('description', '')
120                 list.write('<hr>')
121                 list.write('<h1>example file: %s</h1>' % head);
122                 if composer <> '':
123                         list.write('<h2>%s</h2>\n' % composer)
124                 if desc <> '':
125                         list.write('%s<p>' % desc)
126                 list.write ('<ul>')
127                 def list_item(filename, desc, type, l = list):
128                         if file_exist_b(filename):
129                                 l.write ('<li><a href=%s>%s</a>' % (filename, desc))
130                                 size=os.stat(filename)[stat.ST_SIZE]
131                                 l.write (' (%s %dk)' % (type, (size + 512) / 1024))
132                                 pictures = ['jpeg', 'png', 'xpm']
133                                 l.write ('\n')
134
135                 list_item(base + ext, 'The input', 'ASCII')
136                 for pageno in range(1,100):
137                         f  = base + '-page%d.png' % pageno
138                         if not file_exist_b (f):
139                                 break
140                         list_item(f, 'The output, page %d' % pageno, 'png')
141                 list_item(base + '.ps.gz', 'The output', 'gzipped PostScript')
142                 list_item(base + '.midi', 'The output', 'MIDI')
143                 list.write ("</ul>");
144
145         list.write( "</BODY></HTML>");
146         list.close()
147
148 import getopt
149
150 (options, files) = getopt.getopt(sys.argv[1:], 
151   'ho:', ['help', 'output='])
152 outfile = 'examples.html'
153
154 subdirs =[]
155 for opt in options:
156         o = opt[0]
157         a = opt[1]
158         if o == '--help' or o == '-h':
159                 help()
160         elif o == '--output' or o == '-o':
161                 outfile = a
162
163 dirs  = []
164 for f in files:
165         dirs = dirs + find.find ('out-www', f);
166
167 if not dirs:
168         dirs = ['.']
169
170 allfiles = []
171
172 for d in dirs:
173         allfiles = allfiles + find.find ('*.ly.txt', d)
174
175 print allfiles
176
177 gen_list (allfiles, outfile)
178