]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/mutopia-index.py
optimize width for 800x600, width=760px (continued).
[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 body { width: 760px; }\n
96 hr { border:0; height:1; color: #000000; background-color: #000000; }\n
97 </style>
98 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
99 </head>''')
100
101         list.write ('<body bgcolor=white>\n')
102         
103         if inputs:
104                 list.write (headertext)
105         else:
106                 list.write (headertext_nopics)
107
108         for ex in inputs:
109                 print ex
110                 
111                 (base, ext) = os.path.splitext (ex)
112                 (base, ext2) = os.path.splitext (base)          
113                 ext = ext2 + ext
114                 
115                 header = read_lilypond_header (ex)
116                 def read_dict (s, default, h = header):
117                                 try:
118                                         ret = h[s]
119                                 except KeyError:
120                                         ret = default
121                                 return ret
122                 head = read_dict ('title', os.path.basename (base))
123                 composer = read_dict ('composer', '')
124                 desc = read_dict ('description', '')
125                 list.write ('<hr>\n')
126                 list.write ('<h1>%s</h1>\n' % head);
127                 if composer:
128                         list.write ('<h2>%s</h2>\n' % composer)
129                 if desc:
130                         list.write ('%s<p>' % desc)
131                 list.write ('<ul>\n')
132
133                 def list_item (file_name, desc, type, lst = list):
134                         if os.path.isfile (file_name):
135                                 lst.write ('<li><a href="%s">%s</a>'
136                                            % (file_name, desc))
137
138                                 # FIXME: include warning if it uses \include
139                                 # files.
140                                 
141                                 size = os.stat (file_name)[stat.ST_SIZE]
142                                 kB = (size + 512) / 1024
143                                 if kB:
144                                         lst.write (' (%s %d kB)' % (type, kB))
145                                 else:
146                                         lst.write (' (%s %d characters)'
147                                                    % (type, size))
148                                 pictures = ['jpeg', 'png', 'xpm']
149                                 lst.write ('\n')
150                         else:
151                                 print "can't find" , `file_name`
152
153                 list_item (base + ext, 'The input', 'ASCII')
154
155                 pages_found = 0
156                 for page in range (1, 100):
157                         f = base + '-page%d.png' % page
158                         
159                         if not os.path.isfile (f):
160                                 break
161                         pages_found += 1
162                         list_item (f, 'See a picture of page %d' % page, 'png')
163
164                 if pages_found == 0 and os.path.exists (base + '.png'):
165                         list_item (base + ".png",
166                                    'See a picture', 'png')
167
168                         
169                 list_item (base + '.pdf', 'Print', 'PDF')
170                 list_item (base + '.midi', 'Listen', 'MIDI')
171                 list.write ('</ul>\n');
172
173         list.write ('</body></html>\n');
174         list.close ()
175
176 (options, files) = getopt.getopt (sys.argv[1:], 
177   'ho:', ['help', 'output='])
178 outfile = 'examples.html'
179
180 subdirs = []
181 for opt in options:
182         o = opt[0]
183         a = opt[1]
184         if o == '--help' or o == '-h':
185                 help ()
186         elif o == '--output' or o == '-o':
187                 outfile = a
188
189 dirs  = []
190 for f in files:
191         dirs = dirs + find ('out-www', f)
192
193 if not dirs:
194         dirs = ['.']
195
196 allfiles = []
197
198 for d in dirs:
199         allfiles = allfiles + find ('*.ly.txt', d)
200
201 gen_list (allfiles, outfile)
202