]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
patch::: 1.3.63.uu1: Re: Lilypond?
[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 <p>
34 These images are generated at approximately 180dpi. If you want a better impression of the appearance do print out one the postscript version of the samples."""
35
36
37 #
38 # FIXME breaks on multiple strings.
39 #
40 def read_mudela_header (fn):
41         s = open(fn).read ()
42         s = re.sub('%.*$', '', s)
43         s = re.sub('\n', ' ', s)                
44
45         dict = {}
46         m = re.search (r"""\\header\s*{([^}]*)}""", s)
47
48         if m:
49                         s = m.group(1)
50         else:
51                         return dict
52
53         while s:
54                 m = re.search (r"""\s*(\S+)\s*=\s*([^;]+)\s*;""", s)
55                 if m == None:
56                         s = ''
57                 else:
58                         s = s[m.end (0):]
59                         left  = m.group  (1)
60                         right = m.group (2)
61
62                         left = re.sub ('"', '', left)
63                         right = re.sub ('"', '', right)
64                         dict[left] = right
65
66         return dict
67
68 def help ():
69         sys.stdout.write (r"""Usage: mutopia-index [options] INFILE OUTFILE
70 Generate index for mutopia\n
71 Options:
72   -h, --help                 print this help
73   -o,-output=FILE            write output to file.
74   -s, --subdirs=DIR          add subdir
75   --suffix=SUF                   specify suffix"""
76                                           )
77         sys.exit (0)
78
79 # ugh.
80 def gen_list(inputs, filename):
81         print "generating HTML list %s\n" % filename
82         if filename:
83                 list = open(filename, 'w')
84         else:
85                 list = sys.stdout
86         list.write ('<html><TITLE>Rendered Examples</TITLE>\n')
87         list.write ('<body bgcolor=white>')
88         
89         if inputs:
90                         list.write (headertext)
91         else:
92                         list.write (headertext_nopics)
93
94
95         for ex in inputs:
96                 (base, ext) = os.path.splitext (ex)
97                 (base, ext2) = os.path.splitext (base)          
98                 ext = ext2 + ext
99                 
100                 print '%s, ' % ex
101                 header = read_mudela_header(ex)
102                 
103                 def read_dict(s, default, h =header):
104                                 try:
105                                         ret = h[s]
106                                 except KeyError:
107                                         ret = default
108                                 return ret
109                 head = read_dict('title', os.path.basename (base))
110                 composer = read_dict('composer', '')
111                 desc = read_dict('description', '')
112                 list.write('<hr>')
113                 list.write('<h1>example file: %s</h1>' % head);
114                 if composer <> '':
115                         list.write('<h2>%s</h2>\n' % composer)
116                 if desc <> '':
117                         list.write('%s<p>' % desc)
118                 list.write ('<ul>')
119                 def list_item(filename, desc, type, l = list):
120                         if file_exist_b(filename):
121                                 l.write ('<li><a href=%s>%s</a>' % (filename, desc))
122                                 size=os.stat(filename)[stat.ST_SIZE]
123                                 l.write (' (%s %dk)' % (type, (size + 512) / 1024))
124                                 pictures = ['jpeg', 'png', 'xpm']
125                                 l.write ('\n')
126
127                 list_item(base + ext, 'The input', 'ASCII')
128                 for pageno in range(1,100):
129                         f  = base + '-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(base + '.ps.gz', 'The output', 'gzipped PostScript')
134                 list_item(base + '.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   'ho:', ['help', 'output='])
144 outfile = 'examples.html'
145
146 subdirs =[]
147 for opt in options:
148         o = opt[0]
149         a = opt[1]
150         if o == '--help' or o == '-h':
151                 help()
152         elif o == '--output' or o == '-o':
153                 outfile = a
154
155 dirs  = []
156 for f in files:
157         dirs = dirs + find.find ('out-www', f);
158
159 if not dirs:
160         dirs = ['.']
161
162 allfiles = []
163
164 for d in dirs:
165         allfiles = allfiles + find.find ('*.ly.txt', d)
166
167 print allfiles
168
169 gen_list (allfiles, outfile)
170