]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
* buildscripts/mutopia-index.py (headertext): add h1 header and
[lilypond.git] / buildscripts / mutopia-index.py
1 #!@PYTHON@
2 # mutopia-index.py
3
4 name = 'mutopia-index'
5
6 import fnmatch
7 import os
8
9 _debug = 0
10
11 _prune = ['(*)']
12
13 def find(pattern, dir = os.curdir):
14         list = []
15         names = os.listdir(dir)
16         names.sort()
17         for name in names:
18                 if name in (os.curdir, os.pardir):
19                         continue
20                 fullname = os.path.join(dir, name)
21                 if fnmatch.fnmatch(name, pattern):
22                         list.append(fullname)
23                 if os.path.isdir(fullname) and not os.path.islink(fullname):
24                         for p in _prune:
25                                 if fnmatch.fnmatch(name, p):
26                                         if _debug: print "skip", `fullname`
27                                         break
28                         else:
29                                 if _debug: print "descend into", `fullname`
30                                 list = list + find(pattern, fullname)
31         return list
32
33
34 import re
35 import os
36 import sys
37 import stat
38
39 def gulp_file (fn):
40         try:
41                 f = open (fn)
42         except:
43                 raise 'not there' , fn
44         return f.read ()
45
46 def file_exist_b (fn):
47         try:
48                 f = open (fn)
49                 return 1
50         except:
51                 return 0
52
53
54 headertext= r"""
55
56 <h1>LilyPond samples</h1>
57  
58
59 <p>You're looking at a page with some LilyPond samples.  These files
60 are also included in the distribution. The output is completely
61 generated from the <tt>.ly</tt> source file, without any further touch
62 up.
63
64 <p>
65
66 The pictures are 90 dpi anti-aliased snapshots of the printed output.
67 If you want a better impression of the appearance, do print out one of
68 the PDF or PostScript files; they use scalable fonts, and should look
69 good at any resolution.
70
71 """
72
73 headertext_nopics= r"""
74 <p>Nothing to be seen here, move along.
75 """
76
77 #
78 # FIXME breaks on multiple strings.
79 #
80 def read_lilypond_header (fn):
81         s = open(fn).read ()
82         s = re.sub('%.*$', '', s)
83         s = re.sub('\n', ' ', s)                
84
85         dict = {}
86         m = re.search (r"""\\header\s*{([^}]*)}""", s)
87
88         if m:
89                         s = m.group(1)
90         else:
91                         return dict
92
93         while s:
94                 m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
95                 if m == None:
96                         s = ''
97                 else:
98                         s = s[m.end (0):]
99                         left  = m.group  (1)
100                         right = m.group (2)
101
102                         left = re.sub ('"', '', left)
103                         right = re.sub ('"', '', right)
104                         dict[left] = right
105
106         return dict
107
108 def help ():
109         sys.stdout.write (r"""Usage: mutopia-index [OPTIONS] INFILE OUTFILE
110 Generate index for mutopia.
111
112 Options:
113   -h, --help                 print this help
114   -o, --output=FILE          write output to file
115   -s, --subdirs=DIR          add subdir
116       --suffix=SUF           specify suffix
117       
118 """
119                                           )
120         sys.exit (0)
121
122 # ugh.
123 def gen_list(inputs, filename):
124         print "generating HTML list %s\n" % filename
125         if filename:
126                 list = open(filename, 'w')
127         else:
128                 list = sys.stdout
129         list.write ('''<html><head><title>Rendered Examples</title>
130 <style type="text/css">
131 hr { border:0; height:1; color: #000000; background-color: #000000; }\n
132 </style>
133 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
134 </head>''')
135
136         list.write ('<body bgcolor=white>\n')
137         
138         if inputs:
139                         list.write (headertext)
140         else:
141                         list.write (headertext_nopics)
142
143
144         for ex in inputs:
145                 (base, ext) = os.path.splitext (ex)
146                 (base, ext2) = os.path.splitext (base)          
147                 ext = ext2 + ext
148                 
149                 header = read_lilypond_header(ex)
150                 def read_dict(s, default, h =header):
151                                 try:
152                                         ret = h[s]
153                                 except KeyError:
154                                         ret = default
155                                 return ret
156                 head = read_dict('title', os.path.basename (base))
157                 composer = read_dict('composer', '')
158                 desc = read_dict('description', '')
159                 list.write('<hr>\n')
160                 list.write('<h1>%s</h1>\n' % head);
161                 if composer:
162                         list.write('<h2>%s</h2>\n' % composer)
163                 if desc:
164                         list.write('%s<p>' % desc)
165                 list.write ('<ul>\n')
166                 def list_item(filename, desc, type, l = list):
167                         if file_exist_b(filename):
168                                 
169                                 l.write ('<li><a href="%s">%s</a>' % (filename, desc))
170
171                                 # todo: include warning if it uses \include
172                                 # files.
173                                 
174                                 size=os.stat(filename)[stat.ST_SIZE]
175                                 kB=(size + 512) / 1024
176                                 if kB:
177                                         l.write (' (%s %d kB)' % (type, kB))
178                                 else:
179                                         l.write (' (%s %d characters)' % (type, size))
180                                 pictures = ['jpeg', 'png', 'xpm']
181                                 l.write ('\n')
182
183                 list_item(base + ext, 'The input', 'ASCII')
184                 for pageno in range(1,100):
185                         f  = base + '-page%d.png' % pageno
186                         if not file_exist_b (f):
187                                 break
188                         list_item(f, 'See a picture of page %d' % pageno, 'png')
189                 list_item(base + '.pdf', 'Print', 'PDF')
190                 list_item(base + '.midi', 'Listen', 'MIDI')
191                 list.write ("</ul>\n");
192
193         list.write('</body></html>\n');
194         list.close()
195
196 import getopt
197
198 (options, files) = getopt.getopt(sys.argv[1:], 
199   'ho:', ['help', 'output='])
200 outfile = 'examples.html'
201
202 subdirs =[]
203 for opt in options:
204         o = opt[0]
205         a = opt[1]
206         if o == '--help' or o == '-h':
207                 help()
208         elif o == '--output' or o == '-o':
209                 outfile = a
210
211 dirs  = []
212 for f in files:
213         dirs = dirs + find ('out-www', f)
214
215 if not dirs:
216         dirs = ['.']
217
218 allfiles = []
219
220 for d in dirs:
221         allfiles = allfiles + find ('*.ly.txt', d)
222
223 gen_list (allfiles, outfile)
224