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