]> git.donarmstrong.com Git - lilypond.git/blob - scripts/filter-lilypond-book.py
18d2ddc9fefa136731d9d79c0cbbefd7b0a3d88c
[lilypond.git] / scripts / filter-lilypond-book.py
1 #!@PYTHON@
2
3 '''
4 TODO:
5       ly-options: intertext, quote ?
6       --linewidth?
7       eps in latex?
8       check latex parameters, twocolumn
9       multicolumn?
10       papersizes?
11       ly2dvi/notexidoc?
12       
13 Example usage:
14
15 test:
16      filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
17           
18 convert-ly on book:
19      filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
20
21 classic lilypond-book:
22      filter-lilypond-book --process="lilypond-bin" BOOK.tely
23
24    must substitute:
25      @mbinclude foo.itely -> @include foo.itely
26      \mbinput -> \input
27      
28 '''
29
30 import string
31 import __main__
32
33 ################################################################
34 # Users of python modules should include this snippet
35 # and customize variables below.
36
37 # We'll suffer this path init stuff as long as we don't install our
38 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
39 # it as we do with teTeX on Red Hat Linux: set some environment var
40 # (PYTHONPATH) in profile)
41
42 # If set, LILYPONDPREFIX must take prevalence
43 # if datadir is not set, we're doing a build and LILYPONDPREFIX
44 import getopt, os, sys
45 datadir = '@local_lilypond_datadir@'
46 if not os.path.isdir (datadir):
47         datadir = '@lilypond_datadir@'
48 if os.environ.has_key ('LILYPONDPREFIX') :
49         datadir = os.environ['LILYPONDPREFIX']
50         while datadir[-1] == os.sep:
51                 datadir= datadir[:-1]
52
53 sys.path.insert (0, os.path.join (datadir, 'python'))
54
55 # Customize these
56 #if __name__ == '__main__':
57
58 import lilylib as ly
59 global _;_=ly._
60 global re;re = ly.re
61
62 # lilylib globals
63 program_version = '@TOPLEVEL_VERSION@'
64 #program_name = 'new-book'
65 program_name = 'filter-lilypond-book'
66 verbose_p = 0
67 pseudo_filter_p = 0
68 original_dir = os.getcwd ()
69
70
71 # help_summary = _ ("Process LilyPond snippets in hybrid html, LaTeX or texinfo document")
72 help_summary = _ ("""Process ly snippets from lilypond-book source.  Example usage:
73
74    filter-lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
75    filter-lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
76
77 """)
78 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>>',
79              'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
80
81 option_definitions = [
82         (_ ("EXT"), 'f', 'format', _ ("use output format EXT (texi [default], texi-html, latex, html)")),
83         (_ ("FILTER"), 'F', 'filter', _ ("pipe snippets through FILTER [convert-ly -n -]")),
84         ('', 'h', 'help', _ ("print this help")),
85         (_ ("DIR"), 'I', 'include', _ ("add DIR to include path")),
86         (_ ("COMMAND"), 'P', 'process', _ ("process ly_files using COMMAND FILE...")),
87         (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
88         ('', 'V', 'verbose', _ ("be verbose")),
89         ('', 'v', 'version', _ ("print version information")),
90         ('', 'w', 'warranty', _ ("show warranty and copyright")),
91         ('', 'M', 'dependencies', _ ("TODO")),
92         (_ ("DIR"), '', 'outdir', _ ("DEPRECATED, use --output")),
93         ]
94
95 include_path = [ly.abspath (os.getcwd ())]
96
97 lilypond_binary = os.path.join ('@bindir@', 'lilypond-bin')
98
99 # only use installed binary  when we're installed too.
100 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
101         lilypond_binary = 'lilypond-bin'
102
103
104 use_hash_p = 1
105 format = 0
106 output_name = 0
107 latex_filter_cmd = 'latex "\\nonstopmode \input /dev/stdin"'
108 ##filter_cmd = 'convert-ly --no-version --from=2.0.0 -'
109 filter_cmd = 0
110
111 #process_cmd = 'convert-ly --no-version --from=2.0.0'
112 #process_cmd = 0
113 # process_cmd = 'lilypond-bin'
114 process_cmd = 'lilypond-bin -I ../../../input/test'
115 default_ly_options = { }
116
117 LATEX = 'latex'
118 HTML = 'html'
119 TEXINFO = 'texinfo'
120 BEFORE = 'before'
121 OUTPUT = 'output'
122 AFTER = 'after'
123 VERBATIM = 'verbatim'
124 LINEWIDTH = 'linewidth'
125
126 ## lilypond-book heritage.  to be cleaned
127
128 ################################################################
129 # Recognize special sequences in the input 
130
131
132 # Warning: This uses extended regular expressions.  Tread with care.
133 #
134 # legenda
135 #
136 # (?P<name>regex) -- assign result of REGEX to NAME
137 # *? -- match non-greedily.
138 # (?m) -- multiline regex: make ^ and $ match at each line
139 # (?s) -- make the dot match all characters including newline
140 no_match = 'a\ba'
141 snippet_res = {
142         HTML: {
143         #'header': no_match,
144         #'input': no_match,
145         #'landscape': no_match,
146         #'ly2dvi': r'(?m)(?P<match><ly2dvifile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</ly2dvifile>)',
147         #'multicols': no_match,
148         #'numcols': no_match,
149         'include':  no_match,
150         'lilypond' : '(?m)(?P<match><lilypond((?P<options>[^:]*):)(?P<code>.*?)/>)',
151         'lilypond-block': r'''(?ms)(?P<match><lilypond(?P<options>[^>]+)?>(?P<code>.*?)</lilypond>)''',
152         'lilypond-file': r'(?m)(?P<match><lilypondfile(?P<options>[^>]+)?>\s*(?P<filename>[^<]+)\s*</lilypondfile>)',
153         'multiline-comment': r"(?sm)\s*(?!@c\s+)(?P<code><!--\s.*?!-->)\s",
154         'singleline-comment': no_match,
155         'verb': r'''(?P<code><pre>.*?</pre>)''',
156         'verbatim': r'''(?s)(?P<code><pre>\s.*?</pre>\s)''',
157         },
158
159         LATEX: {
160         #'def-post-re': r"\\def\\postLilyPondExample",
161         #'def-pre-re': r"\\def\\preLilyPondExample",
162         #'header': r"\n*\\documentclass\s*(\[.*?\])?",
163         #'ly2dvi': no_match,
164         #'multicols': r"(?P<code>\\(?P<be>begin|end)\s*{multicols}({(?P<num>\d+)?})?)",
165         #'numcols': r"(?P<code>\\(?P<num>one|two)column)",
166         #'usepackage-graphics': r"\usepackage\s*{graphics}",
167         'include': r'(?m)^[^%\n]*?(?P<match>\\input{(?P<filename>[^}]+)})',
168         'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
169         'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
170         'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
171         'multiline-comment': no_match,
172         'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>^%.*$\n+))",
173         'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
174         'verbatim': r"(?s)(?P<code>\\begin\s*{verbatim}.*?\\end{verbatim})",
175         },
176
177         TEXINFO: {
178         #'header': no_match,
179         #'landscape': no_match,
180         #'multicols': no_match,
181         #'numcols': no_match,
182         'include':  '(?m)^[^%\n]*?(?P<match>@include\s+(?P<filename>\S*))',
183         'lilypond' : '(?m)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?{(?P<code>.*?)})',
184         'lilypond-block': r'''(?ms)^(?P<match>@lilypond(\[(?P<options>[^]]*)\])?\s(?P<code>.*?)@end lilypond)\s''',
185         'lilypond-file': '(?m)^(?P<match>@lilypondfile(\[(?P<options>[^]]*)\])?{(?P<filename>[^}]+)})',
186         'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
187         'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c([ \t][^\n]*|)\n))",
188         'verb': r'''(?P<code>@code{.*?})''',
189         'verbatim': r'''(?s)(?P<code>@example\s.*?@end example\s)''',
190         },
191         }
192
193 format_res = {
194         HTML: {
195         'option-sep' : '\s*',
196         'intertext': r',?\s*intertext=\".*?\"',
197         },
198         LATEX: {
199         'intertext': r',?\s*intertext=\".*?\"',
200         'option-sep' : ',\s*',
201         },
202         TEXINFO: {
203         'intertext': r',?\s*intertext=\".*?\"',
204         'option-sep' : ',\s*',
205         },
206         }
207
208 NOTES = 'body'
209 PREAMBLE = 'preamble'
210 PAPER = 'paper'
211
212 ly_options = {
213         NOTES: {
214         #'xrelative': r'''\relative #(ly:make-pitch %(relative)s 0 0)''',
215         'relative': r'''\relative c%(relative_quotes)s''',
216         },
217         PAPER: {
218         'indent' : r'''
219     indent = %(indent)s''',
220         'linewidth' : r'''
221     linewidth = %(linewidth)s''',
222         'noindent' : r'''
223     indent = 0.0\mm''',
224         'notime' : r'''
225     \translator {
226         \StaffContext
227         \remove Time_signature_engraver
228     }''',
229         'raggedright' : r'''
230     indent = 0.0\mm
231     raggedright = ##t''',
232         },
233         PREAMBLE: {
234         'staffsize': r'''
235 #(set-global-staff-size %(staffsize)s)''',
236         },
237         }
238
239 output = {
240         HTML : {
241         AFTER: '',
242         BEFORE: '',
243         OUTPUT: r'''<img align="center" valign="center"
244 border="0" src="%(base)s.png" alt="[picture of music]">''',
245         VERBATIM: r'''<pre>
246 %(verb)s</pre>''',
247         },
248         
249         LATEX : {
250         AFTER: '',
251         BEFORE: '',
252         OUTPUT: r'''{\parindent 0pt
253 \catcode`\@=12
254 \ifx\preLilyPondExample\preLilyPondExample\fi
255 \def\lilypondbook{}
256 \input %(base)s.tex
257 \ifx\preLilyPondExample\postLilyPondExample\fi
258 \catcode`\@=0}''',
259         VERBATIM: r'''\begin{verbatim}
260 %(verb)s\end{verbatim}
261 ''',
262         },
263         
264         TEXINFO :       {
265         BEFORE: '',
266         AFTER: '',
267         VERBATIM: r'''@example
268 %(verb)s@end example''',
269         
270         },
271         
272         }
273
274 PREAMBLE_LY = r'''%% Generated by %(program_name)s
275 %% Options: [%(option_string)s]
276 %(preamble_string)s
277 \paper {%(paper_string)s
278 }
279 ''' 
280
281 FRAGMENT_LY = r'''\score{
282     \notes%(notes_string)s{
283         %(code)s    }
284 }'''
285 FULL_LY = '%(code)s'
286
287 def classic_lilypond_book_compatibility (o):
288         if o == 'singleline':
289                 return 'raggedright'
290         m = re.search ('relative\s*([-0-9])', o)
291         if m:
292                 return 'relative=%s' % m.group (1)
293         m = re.match ('([0-9]+)pt', o)
294         if m:
295                 return 'staffsize=%s' % m.group (1)
296         m = re.match ('indent=([-.0-9]+)(cm|in|mm|pt)', o)
297         if m:
298                 f = float (m.group (1))
299                 return 'indent=%f\\%s' % (f, m.group (2))
300         m = re.match ('linewidth=([-.0-9]+)(cm|in|mm|pt)', o)
301         if m:
302                 f = float (m.group (1))
303                 return 'linewidth=%f\\%s' % (f, m.group (2))
304         return None
305
306 def compose_ly (code, option_string):
307         options = []
308         # urg
309         for i in default_ly_options.keys ():
310                 options.append (i)
311                 vars ()[i] = default_ly_options[i]
312
313         if option_string:
314                 options = options + split_options (option_string)
315         
316         m = re.search (r'''\\score''', code)
317         if not m and (not options \
318                       or not 'nofragment' in options \
319                       or 'fragment' in options):
320                 options.append ('raggedright')
321                 body = FRAGMENT_LY
322         else:
323                 body = FULL_LY
324
325         # defaults
326         relative = 0
327         staffsize = '16'
328
329         notes_options = []
330         paper_options = []
331         preamble_options = []
332         for i in options:
333                 c = classic_lilypond_book_compatibility (i)
334                 if c:
335                         ly.warning (_ ("deprecated ly-option used: %s" % i))
336                         ly.warning (_ ("compatibility mode translation: %s" \
337                                        % c))
338                         i = c
339                 
340                 if string.find (i, '=') > 0:
341                         key, value = string.split (i, '=')
342                         # hmm
343                         vars ()[key] = value
344                 else:
345                         key = i
346
347                 if key in ly_options[NOTES].keys ():
348                         notes_options.append (ly_options[NOTES][key])
349                 elif key in ly_options[PREAMBLE].keys ():
350                         preamble_options.append (ly_options[PREAMBLE][key])
351                 elif key in ly_options[PAPER].keys ():
352                         paper_options.append (ly_options[PAPER][key])
353                 elif key not in ('fragment', 'nofragment',
354                                  'relative', 'verbatim'):
355                         ly.warning (_("ignoring unknown ly option: %s") % i)
356
357         relative_quotes = (",,,", ",,", ",", "", "'", "''", "'''")[relative-3]
358         program_name = __main__.program_name
359         notes_string = string.join (notes_options, '\n    ') % vars ()
360         paper_string = string.join (paper_options, '\n    ') % vars ()
361         preamble_string = string.join (preamble_options, '\n    ') % vars ()
362         return (PREAMBLE_LY + body) % vars ()
363
364
365 # BARF
366 # use lilypond-bin for latex (.lytex) books,
367 # and lilypond --preview for html, texinfo books?
368 def to_eps (file):
369         cmd = r'latex "\nonstopmode \input %s"' % file
370         # Ugh.  (La)TeX writes progress and error messages on stdout
371         # Redirect to stderr
372         cmd = '(( %s  >&2 ) >&- )' % cmd
373         ly.system (cmd)
374         ly.system ('dvips -Ppdf -u+lilypond.map -E -o %s.eps %s' \
375                    % (file, file))
376
377 def find_file (name):
378         for i in include_path:
379                 full = os.path.join (i, name)
380                 if os.path.exists (full):
381                         return full
382         ly.error (_ ('file not found: %s\n' % name))
383         ly.exit (1)
384         return ''
385         
386 def verbatim_html (s):
387         return re.sub ('>', '&gt;',
388                        re.sub ('<', '&lt;',
389                                re.sub ('&', '&amp;', s)))
390
391 def verbatim_texinfo (s):
392         return re.sub ('{', '@{',
393                        re.sub ('}', '@}',
394                                re.sub ('@', '@@', s)))
395
396 def split_options (option_string):
397         return re.split (format_res[format]['option-sep'], option_string)
398
399
400 ## make index static of Snippet?
401 index = 0
402
403 class Snippet:
404         def __init__ (self, type, source, index, match):
405                 self.type = type
406                 self.source = source
407                 self.index = index
408                 self.match = match
409                 self.hash = 0
410
411         def start (self, s):
412                 return self.index + self.match.start (s)
413
414         def end (self, s):
415                 return self.index + self.match.end (s)
416
417         def substring (self, s):
418                 return self.source[self.start (s):self.end (s)]
419
420         def ly (self):
421                 s = ''
422                 if self.type == 'lilypond-block' or self.type == 'lilypond':
423                         s = self.substring ('code')
424                 elif self.type == 'lilypond-file':
425                         name = self.substring ('filename')
426                         s = open (find_file (name)).read ()
427                 return s
428                 
429         def full_ly (self):
430                 s = self.ly ()
431                 if s:
432                         return compose_ly (s, self.match.group ('options'))
433                 return ''
434         
435         def get_hash (self):
436                 if not self.hash:
437                         self.hash = abs (hash (self.ly ()))
438                 if not self.hash:
439                         print 'TYPE:: ' + self.type
440                         print 'CODE:: ' + self.substring (0)
441                         raise 'URG'
442                 return self.hash
443
444         def basename (self):
445                 if use_hash_p:
446                         return 'lily-%d' % self.get_hash ()
447                 raise 'to be done'
448
449         def write_ly (self):
450                 if self.type == 'lilypond-block' or self.type == 'lilypond'\
451                        or self.type == 'lilypond-file':
452                         h = open (self.basename () + '.ly', 'w')
453                         h.write (self.full_ly ())
454
455         def output_html (self):
456                 base = self.basename ()
457                 option_string = self.match.group ('options')
458                 if option_string and VERBATIM in split_options (option_string)\
459                    and format == HTML:
460                         verb = verbatim_html (self.substring ('code'))
461                         h.write (output[HTML][VERBATIM] % vars ())
462                 h.write (output[HTML][BEFORE])
463                 h.write (output[HTML][OUTPUT] % vars ())
464                 h.write (output[HTML][AFTER])
465                         
466         def output_latex (self):
467                 option_string = self.match.group ('options')
468                 if option_string and VERBATIM in split_options (option_string)\
469                    and format == LATEX:
470                         verb = self.substring ('code')
471                         h.write (output[LATEX][VERBATIM] % vars ())
472                 h.write (output[LATEX][BEFORE])
473                 base = self.basename ()
474                 h.write (output[LATEX][OUTPUT] % vars ())
475                 h.write (output[LATEX][AFTER])
476                         
477         def output_texinfo (self):
478                 option_string = self.match.group ('options')
479                 if option_string and VERBATIM in split_options (option_string):
480                         verb = verbatim_texinfo (self.substring ('code'))
481                         h.write (output[TEXINFO][VERBATIM] % vars ())
482                 h.write ('\n@tex\n')
483                 self.output_latex ()
484                 h.write ('\n@end tex\n')
485                 
486                 h.write ('\n@html\n')
487                 self.output_html ()
488                 h.write ('\n@end html\n')
489                         
490         def outdated_p (self):
491                 if self.type != 'lilypond-block' and self.type != 'lilypond'\
492                        and self.type != 'lilypond-file':
493                         return None
494                 base = self.basename ()
495                 if os.path.exists (base + '.ly') \
496                    and os.path.exists (base + '.tex') \
497                    and (use_hash_p \
498                         or self.ly () == open (base + '.ly').read ()):
499                         # TODO: something smart with target formats
500                         # (ps, png) and m/ctimes
501                         return None
502                 return self
503
504         def filter_code (self):
505                 global index
506                 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
507                 #if snippet.match.group ('code'):
508                 # urg
509                 if self.type == 'lilypond' or self.type == 'lilypond-block':
510                         h.write (self.source[index:self.start ('code')])
511                         h.write (run_filter (self.substring ('code')))
512                         h.write (self.source[self.end ('code'):self.end (0)])
513                 else:
514                         h.write (self.source[index:self.end (0)])
515                 index = self.end (0)
516
517         def compile_output (self):
518                 global index
519                 # Hmm, why is verbatim's group called 'code'; rename to 'verb'?
520                 # if snippet.match.group ('code'):
521                 # urg
522                 if self.type == 'lilypond' \
523                        or self.type == 'lilypond-block'\
524                        or self.type == 'lilypond-file':
525                         h.write (self.source[index:self.start (0)])
526                         snippet_output = eval ("Snippet.output_" + format)
527                         snippet_output (self)
528                 elif self.type == 'include':
529                         h.write (self.source[index:self.start ('filename')])
530                         base = os.path.splitext (self.substring ('filename'))[0]
531                         h.write (base + format2ext[format])
532                         h.write (self.source[self.end ('filename'):self.end (0)])
533                 else:
534                         h.write (self.source[index:self.end (0)])
535                 index = self.end (0)
536
537 def find_toplevel_snippets (s, types):
538         res = {}
539         for i in types:
540                 res[i] = ly.re.compile (snippet_res[format][i])
541
542         snippets = []
543         index = 0
544         found = {}.fromkeys (types)
545         while 1:
546                 first = 0
547                 endex = 1 << 30
548                 for i in types:
549                         if not found[i] or found[i].start (0) < index:
550                                 found[i] = 0
551                                 m = res[i].search (s[index:endex])
552                                 if m:
553                                         found[i] = Snippet (i, s, index, m)
554                         if found[i] \
555                                and (not first \
556                                     or found[i].start (0) < found[first].start (0)):
557                                 first = i
558                                 endex = found[first].start (0)
559                 if not first:
560                         break
561                 snippets.append (found[first])
562                 index = found[first].end (0)
563                 
564         return snippets
565
566 def filter_pipe (input, cmd):
567         if verbose_p:
568                 ly.progress (_ ("Opening filter `%s\'") % cmd)
569                 
570         stdin, stdout, stderr = os.popen3 (cmd)
571         stdin.write (input)
572         status = stdin.close ()
573
574         if not status:
575                 status = 0
576                 output = stdout.read ()
577                 status = stdout.close ()
578                 error = stderr.read ()
579                 
580         if not status:
581                 status = 0
582         signal = 0x0f & status
583         if status or (not output and error):
584                 exit_status = status >> 8
585                 ly.error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
586                 ly.error (_ ("The error log is as follows:"))
587                 sys.stderr.write (error)
588                 sys.stderr.write (stderr.read ())
589                 ly.exit (status)
590         
591         if verbose_p:
592                 ly.progress ('\n')
593
594         return output
595         
596 def run_filter (s):
597         return filter_pipe (s, filter_cmd)
598
599 def process_snippets (cmd, snippets):
600         names = filter (lambda x:x, map (Snippet.basename, snippets))
601         if names:
602                 ly.system (string.join ([cmd] + names))
603
604         if format == HTML or format == TEXINFO:
605                 for i in names:
606                         to_eps (i)
607                         ly.make_ps_images (i + '.eps', resolution=110)
608
609 LATEX_DOCUMENT = r'''
610 %(preamble)s
611 \begin{document}
612 \typeout{textwidth=\the\textwidth}
613 \typeout{columnsep=\the\columnsep}
614 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
615 \end{document}
616 '''
617 #need anything else besides textwidth?
618 def get_latex_textwidth (source):
619         m = re.search (r'''(?P<preabmle>\\begin\s*{document})''', source)
620         preamble = source[:m.start (0)]
621         latex_document = LATEX_DOCUMENT % vars ()
622         parameter_string = filter_pipe (latex_document, latex_filter_cmd)
623
624         columns = 0
625         m = re.search ('columns=([0-9.]*)', parameter_string)
626         if m:
627                 columns = string.atoi (m.group (1))
628
629         columnsep = 0
630         m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
631         if m:
632                 columnsep = string.atof (m.group (1))
633
634         textwidth = 0
635         m = re.search('textwidth=([0-9.]*)pt', parameter_string)
636         if m:
637                 textwidth = string.atof (m.group (1))
638                 if columns:
639                         textwidth = (textwidth - columnsep) / columns
640
641         return textwidth
642
643
644 ext2format = {
645         '.html' : HTML,
646         '.itely' : TEXINFO,
647         '.lytex' : LATEX,
648         '.tely' : TEXINFO,
649         '.tex': LATEX,
650         '.texi' : TEXINFO,
651         '.texinfo' : TEXINFO,
652         '.xml' : HTML,
653         }
654                                
655 format2ext = {
656         HTML: '.html',
657         #TEXINFO: '.texinfo',
658         TEXINFO: '.texi',
659         LATEX: '.tex',
660         }
661
662 def do_file (input_filename):
663         #ugh
664         global format
665         if not format:
666                 e = os.path.splitext (input_filename)[1]
667                 if e in ext2format.keys ():
668                         #FIXME
669                         format = ext2format[e]
670                 else:
671                         ly.error (_ ("cannot determine format for: %s" \
672                                      % input_filename))
673
674         ly.progress (_ ("Reading %s...") % input_filename)
675         if not input_filename or input_filename == '-':
676                 ih = sys.stdin
677         else:
678                 ih = open (input_filename)
679         source = ih.read ()
680         ly.progress ('\n')
681
682         ly.progress (_ ("Dissecting..."))
683         #snippets = find_toplevel_snippets (source, snippet_res[format].keys ())
684         snippet_types = (
685                 'lilypond-block',
686                 'verb',
687                 'verbatim',
688                 'singleline-comment',
689                 'multiline-comment',
690                 'lilypond-file',
691                 'include',
692                 'lilypond', )
693         
694         snippets = find_toplevel_snippets (source, snippet_types)
695         ly.progress ('\n')
696
697         global h
698         if output_name == '-' or not output_name:
699                 h = sys.stdout
700                 output_filename = '-'
701         else:
702                 if not os.path.isdir (output_name):
703                         os.mkdir (output_name, 0777)
704                 if input_filename == '-':
705                         input_base = 'stdin'
706                 else:
707                         input_base = os.path.splitext (input_filename)[0]
708                 output_filename = output_name + '/' + input_base \
709                                   + format2ext[format]
710                 h = open (output_filename, 'w')
711                 os.chdir (output_name)
712
713         global default_ly_options
714         textwidth = 0
715         if format == LATEX and LINEWIDTH not in default_ly_options.keys ():
716                 textwidth = get_latex_textwidth (source)
717                 default_ly_options[LINEWIDTH] = '''%.0f\pt''' % textwidth
718
719         global index
720         if filter_cmd:
721                 index = 0
722                 map (Snippet.filter_code, snippets)
723                 h.write (source[index:])
724         elif process_cmd:
725                 outdated = filter (lambda x:x,
726                                    map (Snippet.outdated_p, snippets))
727                 ly.progress (_ ("Writing snippets..."))
728                 map (Snippet.write_ly, snippets)
729                 ly.progress ('\n')
730                 
731                 if outdated:
732                         ly.progress (_ ("Processing..."))
733                         process_snippets (process_cmd, outdated)
734                 else:
735                         ly.progress (_ ("All snippets are up to date..."))
736                 ly.progress ('\n')
737                 
738                 ly.progress (_ ("Compiling %s...") % output_filename)
739                 index = 0
740                 map (Snippet.compile_output, snippets)
741                 h.write (source[index:])
742                 ly.progress ('\n')
743
744         if h != sys.stdout:
745                 h.close ()
746
747         def process_include (snippet):
748                 os.chdir (original_dir)
749                 name = snippet.substring ('filename')
750                 ly.progress (_ ('Processing include: %s') % name)
751                 ly.progress ('\n')
752                 do_file (name)
753
754         map (process_include, filter (lambda x: x.type == 'include', snippets))
755
756 def do_options ():
757         global format, output_name
758         global filter_cmd, process_cmd, verbose_p
759         
760         (sh, long) = ly.getopt_args (option_definitions)
761         try:
762                 (options, files) = getopt.getopt (sys.argv[1:], sh, long)
763         except getopt.error, s:
764                 sys.stderr.write ('\n')
765                 ly.error (_ ("getopt says: `%s\'" % s))
766                 sys.stderr.write ('\n')
767                 ly.help ()
768                 ly.exit (2)
769
770         for opt in options:
771                 o = opt[0]
772                 a = opt[1]
773
774                 if 0:
775                         pass
776                 elif o == '--version' or o == '-v':
777                         ly.identify (sys.stdout)
778                         sys.exit (0)
779                 elif o == '--verbose' or o == '-V':
780                         verbose_p = 1
781                 elif o == '--filter' or o == '-F':
782                         filter_cmd = a
783                         process_cmd = 0
784                 elif o == '--format' or o == '-f':
785                         format = a
786                         if a == 'texi-html' or a == 'texi':
787                                 format = TEXINFO
788                 elif o == '--help' or o == '-h':
789                         ly.help ()
790                         sys.exit (0)
791                 elif o == '--include' or o == '-I':
792                         include_path.append (os.path.join (original_dir,
793                                                            ly.abspath (a)))
794                 elif o == '--output' or o == '-o':
795                         output_name = a
796                 elif o == '--outdir':
797                         output_name = a
798                 elif o == '--process' or o == '-P':
799                         process_cmd = a
800                         filter_cmd = 0
801                 elif o == '--warranty' or o == '-w':
802                         if 1 or status:
803                                 ly.warranty ()
804                         sys.exit (0)
805         return files
806
807 def main ():
808         files = do_options ()
809         ly.identify (sys.stderr)
810         ly.setup_environment ()
811         if files:
812                 do_file (files[0])
813
814 if __name__ == '__main__':
815         main ()