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