lilypond-1.1.67
[lilypond.git] / buildscripts / mutopia-index.py
blob11a12210596085ad88e8b4424edcb71e377a2c0c
1 #!@PYTHON@
2 # mutopia-index.py
4 name = 'mutopia-index'
6 import regex
7 import os
8 import sys
9 import stat
10 sys.path.append ('@abs-step-bindir@')
14 header_regex = regex.compile('\\header[ \t\n]*{\([^}]*\)}')
15 header_entry_regex = regex.compile('[\n\t ]*\([^\n\t ]+\)[\n\t ]*=[\n \t]*\([^;]+\)[\n \t]*;')
17 headertext= r"""
18 These example files are taken from the LilyPond distribution.
19 LilyPond currently only outputs TeX and MIDI. The pictures and
20 PostScript files were generated using TeX, Ghostscript and some
21 graphics tools. The papersize used for these examples is A4.
22 The images are in PNG format, and should be viewable with any current browser.
23 We don't use GIFS due to <a href="http://www.gnu.org/philosophy/gif.html">patent problems</a>.
24 <p>
25 If you want an accurate impression of the output quality please <em>print
26 out</em> the samples first.
27 """
29 headertext_nopics = r"""This is a subdirectory of the LilyPond example
30 set. We decided not to show any examples from this directory. If you
31 want to view them, then you have to download LilyPond and compile them
32 yourself."""
36 # FIXME breaks on multiple strings.
38 def read_mudela_header (fn):
39 s = gulp_file(fn)
40 s = regsub.gsub('%.*$', '', s)
41 s = regsub.gsub('\n', ' ', s)
43 dict = {}
44 if header_regex.search(s) <> -1:
45 h = header_regex.group(1)
46 else:
47 return dict
49 while regex.search('=', h) <> -1:
51 if header_entry_regex.search (h) == -1:
53 raise 'format error'
55 h = regsub.sub(header_entry_regex, '', h)
56 left = header_entry_regex.group(1)
57 right = header_entry_regex.group(2)
59 right = regsub.gsub('\([^\\]\)\"', '\\1', right)
60 right = regsub.gsub('^"', '', right)
61 left = regsub.gsub('\([^\\]\)\"', '', left)
62 left = regsub.gsub('^"', '', left)
64 dict[left] = right
66 return dict
71 def help ():
72 sys.stdout.write ("Usage: " + name + " [options] INFILE OUTFILE\n"
73 + "Generate index for mutopia\n\n"
74 + "Options:\n"
75 + " -h, --help print this help\n"
76 + " -p, --package=DIR specify package\n"
77 + " --prefix=PRE specify prefix\n"
78 + " -s, --subdirs=DIR add subdir\n"
79 + " --suffix=SUF specify suffix\n"
81 sys.exit (0)
83 def gen_list(inputs, subdir, filename):
84 (pre, subdirs, post)=subdir
85 print "generating HTML list %s\n" % filename
86 list = open(filename, 'w')
87 list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
88 list.write ('<body bgcolor=white>')
89 if subdirs:
90 list.write ('<h2>subdirectories</h2>')
91 list.write ('<ul>')
92 for ex in subdirs:
93 print 'subdir %s ' % ex
94 list.write ('<li><a href=%s/index.html>Subdirectory: %s</a></li>\n' % (pre + ex + post , ex))
96 list.write ('</ul>')
100 if inputs:
101 list.write('<h2>Contents of this directory</h2>\n');
103 list.write (headertext)
104 else:
105 list.write (headertext_nopics)
108 for ex in inputs:
109 ex_ext = '.ly'
110 print '%s, ' % ex
111 try:
112 header = read_mudela_header(ex + ex_ext + '.txt')
113 except:
114 ex_ext = '.fly'
115 header = read_mudela_header(ex + ex_ext + '.txt')
117 def read_dict(s, default, h =header):
118 try:
119 ret = h[s]
120 except KeyError:
121 ret = default
122 return ret
123 head = read_dict('title', ex)
124 composer = read_dict('composer', '')
125 desc = read_dict('description', '')
126 list.write('<hr>')
127 list.write('<h1>example file: %s</h1>' % head);
128 if composer <> '':
129 list.write('<h2>%s</h2>\n' % composer)
130 if desc <> '':
131 list.write('%s<p>' % desc)
132 list.write ('<ul>')
133 def list_item(filename, desc, type, l = list):
134 if file_exist_b(filename):
135 l.write ('<li><a href=%s>%s</a>' % (filename, desc))
136 size=os.stat(filename)[stat.ST_SIZE]
137 l.write (' (%s %dk)' % (type, (size + 512) / 1024))
138 pictures = ['jpeg', 'png', 'xpm']
139 # silly, no?
140 if 0 and type in pictures:
141 l.write (' <a href="http://www.gnu.org/philosophy/gif.html">no gifs due to patent problems</a>')
142 l.write ('\n')
143 list_item(ex + ex_ext + '.txt', 'The input', 'ASCII')
144 for pageno in range(1,100):
145 f = ex + '-page%d.png' % pageno
146 if not file_exist_b (f):
147 break
148 list_item(f, 'The output, page %d' % pageno, 'png')
149 list_item(ex + '.ps.gz', 'The output', 'gzipped PostScript')
150 list_item(ex + '.midi', 'The output', 'MIDI')
151 list.write ("</ul>");
153 list.write( "</BODY></HTML>");
154 list.close()
156 import getopt
158 (options, files) = getopt.getopt(sys.argv[1:],
159 'hp:s:', ['help', 'subdirs=', 'suffix=', 'package=', 'prefix='])
160 subdir_pre=''
161 subdir_suf =''
163 subdirs =[]
164 for opt in options:
165 o = opt[0]
166 a = opt[1]
167 if o == '--subdirs' or o == '-s':
168 subdirs.append (a)
169 elif o == '--prefix':
170 subdir_pre = a
171 elif o == '-p' or o == '--package':
172 topdir = a
173 elif o == '--suffix':
174 subdir_suf = a
176 sys.path.append (topdir + '/stepmake/bin')
177 from packagepython import *
178 package = Package (topdir)
179 packager = Packager ()
181 from flower import *
184 # huh?
185 allfiles = multiple_find (['*.*ly.txt'], '.')
187 gen_list (files, (subdir_pre, subdirs, subdir_suf), 'index.html')