]> git.donarmstrong.com Git - lilypond.git/blob - scripts/lilypond-book.py
460e74eb2ece36e53115b554b04851c09382aa46
[lilypond.git] / scripts / lilypond-book.py
1 #!@PYTHON@
2
3 '''
4 Example usage:
5
6 test:
7      lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
8
9 convert-ly on book:
10      lilypond-book --filter="convert-ly --no-version --from=1.6.11 -" BOOK
11
12 classic lilypond-book:
13      lilypond-book --process="lilypond" BOOK.tely
14
15 TODO:
16
17     *  this script is too complex. Modularize.
18     
19     *  ly-options: intertext?
20     *  --line-width?
21     *  eps in latex / eps by lilypond -b ps?
22     *  check latex parameters, twocolumn, multicolumn?
23     *  use --png --ps --pdf for making images?
24
25     *  Converting from lilypond-book source, substitute:
26        @mbinclude foo.itely -> @include foo.itely
27        \mbinput -> \input
28
29 '''
30
31 import __main__
32 import glob
33 import stat
34 import string
35 import tempfile
36 import commands
37 import optparse
38 import os
39 import sys
40 import re
41
42 # Users of python modules should include this snippet
43 # and customize variables below.
44
45 # We'll suffer this path initialization stuff as long as we don't install
46 # our python packages in <prefix>/lib/pythonX.Y
47
48 # If set, LILYPONDPREFIX must take prevalence.
49 # if datadir is not set, we're doing a build and LILYPONDPREFIX.
50
51 ################
52 # RELOCATION
53 ################
54
55 datadir = '@local_lilypond_datadir@'
56 if not os.path.isdir (datadir):
57         datadir = '@lilypond_datadir@'
58
59 sys.path.insert (0, os.path.join (datadir, 'python'))
60
61 if os.environ.has_key ('LILYPONDPREFIX'):
62         datadir = os.environ['LILYPONDPREFIX']
63         while datadir[-1] == os.sep:
64                 datadir= datadir[:-1]
65                 
66         datadir = os.path.join (datadir, "share/lilypond/current/")
67 sys.path.insert (0, os.path.join (datadir, 'python'))
68
69 # dynamic relocation, for GUB binaries.
70 bindir = os.path.split (sys.argv[0])[0]
71 for p in ['share', 'lib']:
72         datadir = os.path.abspath (bindir + '/../%s/lilypond/current/python/' % p)
73         sys.path.insert (0, os.path.join (datadir))
74
75
76 import lilylib as ly
77 import fontextract
78 global _;_=ly._
79
80
81 # Lilylib globals.
82 program_version = '@TOPLEVEL_VERSION@'
83 program_name = os.path.basename (sys.argv[0])
84
85 original_dir = os.getcwd ()
86 backend = 'ps'
87
88 help_summary = _ (
89 '''Process LilyPond snippets in hybrid HTML, LaTeX, or texinfo document.
90
91 Example usage:
92
93    lilypond-book --filter="tr '[a-z]' '[A-Z]'" BOOK
94    lilypond-book --filter="convert-ly --no-version --from=2.0.0 -" BOOK
95    lilypond-book --process='lilypond -I include' BOOK
96 ''')
97
98 copyright = ('Jan Nieuwenhuizen <janneke@gnu.org>',
99              'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
100
101 def get_option_parser ():
102         p = ly.get_option_parser (usage='lilypond-book [OPTIONS] FILE',
103                                   version="@TOPLEVEL_VERSION@",
104                                   description=help_summary)
105
106         p.add_option ('-F', '--filter', metavar=_ ("FILTER"),
107                       action="store",
108                       dest="filter_cmd",
109                       help=_ ("pipe snippets through FILTER [convert-ly -n -]"),
110                       default=None)
111         p.add_option ('-f', '--format', help=_('''use output format FORMAT (texi [default], texi-html, latex, html)'''),
112                       action='store')
113         p.add_option ("-I", '--include', help=_('add DIR to include path'),
114                       metavar="DIR",
115                       action='append', dest='include_path',
116                       default=[os.path.abspath (os.getcwd ())])
117         
118         p.add_option ("-o", '--output', help=_('write output to DIR'),
119                       metavar="DIR",
120                       action='store', dest='output_name', default=None)
121         p.add_option ('-P', '--process', metavar=_("COMMAND"),
122                       help = _ ("process ly_files using COMMAND FILE..."),
123                       action='store', 
124                       dest='process_cmd', default='lilypond -b eps')
125         
126         p.add_option ('', '--psfonts', action="store_true", dest="psfonts",
127                       help=_ ('''extract all PostScript fonts into INPUT.psfonts for LaTeX'''
128                               '''must use this with dvips -h INPUT.psfonts'''),
129                       default=None)
130         p.add_option ('-V', '--verbose', help=_("be verbose"), action="store_true",
131                       dest="verbose")
132                       
133         p.add_option ('-w', '--warranty', help=_("show warranty and copyright"),
134                       action='store_true'),
135
136         
137         p.add_option_group  ('bugs',
138                              description='''Report bugs via http://post.gmane.org/post.php'''
139                              '''?group=gmane.comp.gnu.lilypond.bugs\n''')
140         
141         return p
142
143 lilypond_binary = os.path.join ('@bindir@', 'lilypond')
144
145 # Only use installed binary when we are installed too.
146 if '@bindir@' == ('@' + 'bindir@') or not os.path.exists (lilypond_binary):
147         lilypond_binary = 'lilypond'
148
149 global_options = None
150
151
152 default_ly_options = { 'alt': "[image of music]" }
153
154 #
155 # Is this pythonic?  Personally, I find this rather #define-nesque. --hwn
156 #
157 AFTER = 'after'
158 BEFORE = 'before'
159 EXAMPLEINDENT = 'exampleindent'
160 FILTER = 'filter'
161 FRAGMENT = 'fragment'
162 HTML = 'html'
163 INDENT = 'indent'
164 LATEX = 'latex'
165 LAYOUT = 'layout'
166 LINE_WIDTH = 'line-width'
167 NOFRAGMENT = 'nofragment'
168 NOINDENT = 'noindent'
169 NOQUOTE = 'noquote'
170 NOTES = 'body'
171 NOTIME = 'notime'
172 OUTPUT = 'output'
173 OUTPUTIMAGE = 'outputimage'
174 PACKED = 'packed'
175 PAPER = 'paper'
176 PREAMBLE = 'preamble'
177 PRINTFILENAME = 'printfilename'
178 QUOTE = 'quote'
179 RAGGED_RIGHT = 'ragged-right'
180 RELATIVE = 'relative'
181 STAFFSIZE = 'staffsize'
182 TEXIDOC = 'texidoc'
183 TEXINFO = 'texinfo'
184 VERBATIM = 'verbatim'
185 FONTLOAD = 'fontload'
186 FILENAME = 'filename'
187 ALT = 'alt'
188
189
190 # NOTIME has no opposite so it isn't part of this dictionary.
191 # NOQUOTE is used internally only.
192 no_options = {
193         NOFRAGMENT: FRAGMENT,
194         NOINDENT: INDENT,
195 }
196
197
198 # Recognize special sequences in the input.
199 #
200 #   (?P<name>regex) -- Assign result of REGEX to NAME.
201 #   *? -- Match non-greedily.
202 #   (?m) -- Multiline regex: Make ^ and $ match at each line.
203 #   (?s) -- Make the dot match all characters including newline.
204 #   (?x) -- Ignore whitespace in patterns.
205 no_match = 'a\ba'
206 snippet_res = {
207         ##
208         HTML: {
209                 'include':
210                   no_match,
211
212                 'lilypond':
213                   r'''(?mx)
214                     (?P<match>
215                     <lilypond
216                       (\s*(?P<options>.*?)\s*:)?\s*
217                       (?P<code>.*?)
218                     />)''',
219
220                 'lilypond_block':
221                   r'''(?msx)
222                     (?P<match>
223                     <lilypond
224                       \s*(?P<options>.*?)\s*
225                     >
226                     (?P<code>.*?)
227                     </lilypond>)''',
228
229                 'lilypond_file':
230                   r'''(?mx)
231                     (?P<match>
232                     <lilypondfile
233                       \s*(?P<options>.*?)\s*
234                     >
235                     \s*(?P<filename>.*?)\s*
236                     </lilypondfile>)''',
237
238                 'multiline_comment':
239                   r'''(?smx)
240                     (?P<match>
241                     \s*(?!@c\s+)
242                     (?P<code><!--\s.*?!-->)
243                     \s)''',
244
245                 'singleline_comment':
246                   no_match,
247
248                 'verb':
249                   r'''(?x)
250                     (?P<match>
251                       (?P<code><pre>.*?</pre>))''',
252
253                 'verbatim':
254                   r'''(?x)
255                     (?s)
256                     (?P<match>
257                       (?P<code><pre>\s.*?</pre>\s))''',
258         },
259
260         ##
261         LATEX: {
262                 'include':
263                   r'''(?smx)
264                     ^[^%\n]*?
265                     (?P<match>
266                     \\input\s*{
267                       (?P<filename>\S+?)
268                     })''',
269
270                 'lilypond':
271                   r'''(?smx)
272                     ^[^%\n]*?
273                     (?P<match>
274                     \\lilypond\s*(
275                     \[
276                       \s*(?P<options>.*?)\s*
277                     \])?\s*{
278                       (?P<code>.*?)
279                     })''',
280
281                 'lilypond_block':
282                   r'''(?smx)
283                     ^[^%\n]*?
284                     (?P<match>
285                     \\begin\s*(
286                     \[
287                       \s*(?P<options>.*?)\s*
288                     \])?\s*{lilypond}
289                       (?P<code>.*?)
290                     ^[^%\n]*?
291                     \\end\s*{lilypond})''',
292
293                 'lilypond_file':
294                   r'''(?smx)
295                     ^[^%\n]*?
296                     (?P<match>
297                     \\lilypondfile\s*(
298                     \[
299                       \s*(?P<options>.*?)\s*
300                     \])?\s*\{
301                       (?P<filename>\S+?)
302                     })''',
303
304                 'multiline_comment':
305                   no_match,
306
307                 'singleline_comment':
308                   r'''(?mx)
309                     ^.*?
310                     (?P<match>
311                       (?P<code>
312                       %.*$\n+))''',
313
314                 'verb':
315                   r'''(?mx)
316                     ^[^%\n]*?
317                     (?P<match>
318                       (?P<code>
319                       \\verb(?P<del>.)
320                         .*?
321                       (?P=del)))''',
322
323                 'verbatim':
324                   r'''(?msx)
325                     ^[^%\n]*?
326                     (?P<match>
327                       (?P<code>
328                       \\begin\s*{verbatim}
329                         .*?
330                       \\end\s*{verbatim}))''',
331         },
332
333         ##
334         TEXINFO: {
335                 'include':
336                   r'''(?mx)
337                     ^(?P<match>
338                     @include\s+
339                       (?P<filename>\S+))''',
340
341                 'lilypond':
342                   r'''(?smx)
343                     ^[^\n]*?(?!@c\s+)[^\n]*?
344                     (?P<match>
345                     @lilypond\s*(
346                     \[
347                       \s*(?P<options>.*?)\s*
348                     \])?\s*{
349                       (?P<code>.*?)
350                     })''',
351
352                 'lilypond_block':
353                   r'''(?msx)
354                     ^(?P<match>
355                     @lilypond\s*(
356                     \[
357                       \s*(?P<options>.*?)\s*
358                     \])?\s+?
359                     ^(?P<code>.*?)
360                     ^@end\s+lilypond)\s''',
361
362                 'lilypond_file':
363                   r'''(?mx)
364                     ^(?P<match>
365                     @lilypondfile\s*(
366                     \[
367                       \s*(?P<options>.*?)\s*
368                     \])?\s*{
369                       (?P<filename>\S+)
370                     })''',
371
372                 'multiline_comment':
373                   r'''(?smx)
374                     ^(?P<match>
375                       (?P<code>
376                       @ignore\s
377                         .*?
378                       @end\s+ignore))\s''',
379
380                 'singleline_comment':
381                   r'''(?mx)
382                     ^.*
383                     (?P<match>
384                       (?P<code>
385                       @c([ \t][^\n]*|)\n))''',
386
387         # Don't do this: It interferes with @code{@{}.
388         #       'verb': r'''(?P<code>@code{.*?})''',
389
390                 'verbatim':
391                   r'''(?sx)
392                     (?P<match>
393                       (?P<code>
394                       @example
395                         \s.*?
396                       @end\s+example\s))''',
397         },
398 }
399
400
401
402
403 format_res = {
404         HTML: {
405                 'intertext': r',?\s*intertext=\".*?\"',
406                 'option_sep': '\s*',
407         },
408
409         LATEX: {
410                 'intertext': r',?\s*intertext=\".*?\"',
411                 'option_sep': '\s*,\s*',
412         },
413
414         TEXINFO: {
415                 'intertext': r',?\s*intertext=\".*?\"',
416                 'option_sep': '\s*,\s*',
417         },
418 }
419
420 # Options without a pattern in ly_options.
421 simple_options = [
422         EXAMPLEINDENT,
423         FRAGMENT,
424         NOFRAGMENT,
425         NOINDENT,
426         PRINTFILENAME,
427         TEXIDOC,
428         VERBATIM,
429         FONTLOAD,
430         FILENAME,
431         ALT
432 ]
433
434 ly_options = {
435         ##
436         NOTES: {
437                 RELATIVE: r'''\relative c%(relative_quotes)s''',
438         },
439
440         ##
441         PAPER: {
442                 INDENT: r'''indent = %(indent)s''',
443
444                 LINE_WIDTH: r'''line-width = %(line-width)s''',
445
446                 QUOTE: r'''line-width = %(line-width)s - 2.0 * %(exampleindent)s''',
447
448                 RAGGED_RIGHT: r'''ragged-right = ##t''',
449
450                 PACKED: r'''packed = ##t''',
451         },
452
453         ##
454         LAYOUT: {
455                 NOTIME: r'''
456   \context {
457     \Score
458     timing = ##f
459   }
460   \context {
461     \Staff
462     \remove Time_signature_engraver
463   }''',
464         },
465
466         ##
467         PREAMBLE: {
468                 STAFFSIZE: r'''#(set-global-staff-size %(staffsize)s)''',
469         },
470 }
471
472 output = {
473         ##
474         HTML: {
475                 FILTER: r'''<lilypond %(options)s>
476 %(code)s
477 </lilypond>
478 ''',
479
480                 AFTER: r'''
481   </a>
482 </p>''',
483
484                 BEFORE: r'''<p>
485   <a href="%(base)s.ly">''',
486
487                 OUTPUT: r'''
488     <img align="center" valign="center"
489          border="0" src="%(image)s" alt="%(alt)s">''',
490
491                 PRINTFILENAME: '<p><tt><a href="%(base)s.ly">%(filename)s</a></tt></p>',
492
493                 QUOTE: r'''<blockquote>
494 %(str)s
495 </blockquote>
496 ''',
497
498                 VERBATIM: r'''<pre>
499 %(verb)s</pre>''',
500         },
501
502         ##
503         LATEX: {
504                 OUTPUT: r'''{%%
505 \parindent 0pt%%
506 \catcode`\@=12%%
507 \ifx\preLilyPondExample \undefined%%
508   \relax%%
509 \else%%
510   \preLilyPondExample%%
511 \fi%%
512 \def\lilypondbook{}%%
513 \input %(base)s-systems.tex%%
514 \ifx\postLilyPondExample \undefined%%
515   \relax%%
516 \else%%
517   \postLilyPondExample%%
518 \fi%%
519 }''',
520
521                 PRINTFILENAME: '''\\texttt{%(filename)s}
522         ''',
523
524                 QUOTE: r'''\begin{quotation}%(str)s
525 \end{quotation}''',
526
527                 VERBATIM: r'''\noindent
528 \begin{verbatim}%(verb)s\end{verbatim}''',
529
530                 FILTER: r'''\begin{lilypond}[%(options)s]
531 %(code)s
532 \end{lilypond}''',
533         },
534
535         ##
536         TEXINFO: {
537                 FILTER: r'''@lilypond[%(options)s]
538 %(code)s
539 @lilypond''',
540
541                 OUTPUT: r'''
542 @iftex
543 @include %(base)s-systems.texi
544 @end iftex
545 ''',
546
547                 OUTPUTIMAGE: r'''@noindent
548 @ifinfo
549 @image{%(base)s,,,%(alt)s,%(ext)s}
550 @end ifinfo
551 @html
552 <p>
553   <a href="%(base)s.ly">
554     <img align="center" valign="center"
555          border="0" src="%(image)s" alt="%(alt)s">
556   </a>
557 </p>
558 @end html
559 ''',
560
561                 PRINTFILENAME: '''@file{%(filename)s}
562         ''',
563
564                 QUOTE: r'''@quotation
565 %(str)s@end quotation
566 ''',
567
568                 NOQUOTE: r'''@format
569 %(str)s@end format
570 ''',
571
572                 VERBATIM: r'''@exampleindent 0
573 @example
574 %(verb)s@end example
575 ''',
576         },
577 }
578
579 #
580 # Maintain line numbers.
581 #
582
583 ## TODO
584 if 0:
585         for f in [HTML, LATEX]:
586                 for s in (QUOTE, VERBATIM):
587                         output[f][s] = output[f][s].replace("\n"," ")
588
589
590 PREAMBLE_LY = '''%%%% Generated by %(program_name)s
591 %%%% Options: [%(option_string)s]
592
593 #(set! toplevel-score-handler print-score-with-defaults)
594 #(set! toplevel-music-handler
595   (lambda (p m)
596    (if (not (eq? (ly:music-property m \'void) #t))
597         (print-score-with-defaults
598          p (scorify-music m p)))))
599
600 #(ly:set-option (quote no-point-and-click))
601 #(define inside-lilypond-book #t)
602 #(define version-seen? #t)
603 %(preamble_string)s
604
605
606
607
608
609
610 %% ****************************************************************
611 %% Start cut-&-pastable-section 
612 %% ****************************************************************
613
614 \paper {
615   #(define dump-extents #t)
616   %(font_dump_setting)s
617   %(paper_string)s
618 }
619
620 \layout {
621   %(layout_string)s
622 }
623 '''
624
625 FRAGMENT_LY = r'''
626 %(notes_string)s
627 {
628
629
630 %% ****************************************************************
631 %% ly snippet contents follows:
632 %% ****************************************************************
633 %(code)s
634
635
636 %% ****************************************************************
637 %% end ly snippet
638 %% ****************************************************************
639 }
640 '''
641
642 FULL_LY = '''
643
644
645 %% ****************************************************************
646 %% ly snippet:
647 %% ****************************************************************
648 %(code)s
649
650
651 %% ****************************************************************
652 %% end ly snippet
653 %% ****************************************************************
654 '''
655
656 texinfo_line_widths = {
657         '@afourpaper': '160\\mm',
658         '@afourwide': '6.5\\in',
659         '@afourlatex': '150\\mm',
660         '@smallbook': '5\\in',
661         '@letterpaper': '6\\in',
662 }
663
664 def classic_lilypond_book_compatibility (key, value):
665         if key == 'singleline' and value == None:
666                 return (RAGGED_RIGHT, None)
667
668         m = re.search ('relative\s*([-0-9])', key)
669         if m:
670                 return ('relative', m.group (1))
671
672         m = re.match ('([0-9]+)pt', key)
673         if m:
674                 return ('staffsize', m.group (1))
675
676         if key == 'indent' or key == 'line-width':
677                 m = re.match ('([-.0-9]+)(cm|in|mm|pt|staffspace)', value)
678                 if m:
679                         f = float (m.group (1))
680                         return (key, '%f\\%s' % (f, m.group (2)))
681
682         return (None, None)
683
684 def find_file (name):
685         for i in global_options.include_path:
686                 full = os.path.join (i, name)
687                 if os.path.exists (full):
688                         return full
689         ly.error (_ ("file not found: %s") % name + '\n')
690         ly.exit (1)
691         return ''
692
693 def verbatim_html (s):
694         return re.sub ('>', '&gt;',
695                        re.sub ('<', '&lt;',
696                                re.sub ('&', '&amp;', s)))
697
698 def verbatim_texinfo (s):
699         return re.sub ('{', '@{',
700                        re.sub ('}', '@}',
701                                re.sub ('@', '@@', s)))
702
703 def split_options (option_string):
704         if option_string:
705                 if global_options.format == HTML:
706                         options = re.findall('[\w\.-:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|\S+))?',option_string)
707                         for i in range(len(options)):
708                                 options[i] = re.sub('^([^=]+=\s*)(?P<q>["\'])(.*)(?P=q)','\g<1>\g<3>',options[i])
709                         return options
710                 else:
711                         return re.split (format_res[global_options.format]['option_sep'],
712                                          option_string)
713         return []
714
715 def set_default_options (source):
716         global default_ly_options
717         if not default_ly_options.has_key (LINE_WIDTH):
718                 if global_options.format == LATEX:
719                         textwidth = get_latex_textwidth (source)
720                         default_ly_options[LINE_WIDTH] = \
721                           '''%.0f\\pt''' % textwidth
722                 elif global_options.format == TEXINFO:
723                         for (k, v) in texinfo_line_widths.items ():
724                                 # FIXME: @layout is usually not in
725                                 # chunk #0:
726                                 #
727                                 #  \input texinfo @c -*-texinfo-*-
728                                 #
729                                 # Bluntly search first K items of
730                                 # source.
731                                 # s = chunks[0].replacement_text ()
732                                 if re.search (k, source[:1024]):
733                                         default_ly_options[LINE_WIDTH] = v
734                                         break
735
736 class Chunk:
737         def replacement_text (self):
738                 return ''
739
740         def filter_text (self):
741                 return self.replacement_text ()
742
743         def ly_is_outdated (self):
744                 return 0
745
746         def png_is_outdated (self):
747                 return 0
748
749         def is_plain (self):
750                 return False
751         
752 class Substring (Chunk):
753         def __init__ (self, source, start, end, line_number):
754                 self.source = source
755                 self.start = start
756                 self.end = end
757                 self.line_number = line_number
758                 self.override_text = None
759                 
760         def is_plain (self):
761                 return True
762
763         def replacement_text (self):
764                 if self.override_text:
765                         return self.override_text
766                 else:
767                         return self.source[self.start:self.end]
768
769 class Snippet (Chunk):
770         def __init__ (self, type, match, format, line_number):
771                 self.type = type
772                 self.match = match
773                 self.hash = 0
774                 self.option_dict = {}
775                 self.format = format
776                 self.line_number = line_number
777
778         def replacement_text (self):
779                 return self.match.group ('match')
780
781         def substring (self, s):
782                 return self.match.group (s)
783
784         def __repr__ (self):
785                 return `self.__class__` + ' type = ' + self.type
786
787 class Include_snippet (Snippet):
788         def processed_filename (self):
789                 f = self.substring ('filename')
790                 return os.path.splitext (f)[0] + format2ext[global_options.format]
791
792         def replacement_text (self):
793                 s = self.match.group ('match')
794                 f = self.substring ('filename')
795
796                 return re.sub (f, self.processed_filename (), s)
797
798 class Lilypond_snippet (Snippet):
799         def __init__ (self, type, match, format, line_number):
800                 Snippet.__init__ (self, type, match, format, line_number)
801                 os = match.group ('options')
802                 self.do_options (os, self.type)
803
804         def ly (self):
805                 return self.substring ('code')
806
807         def full_ly (self):
808                 s = self.ly ()
809                 if s:
810                         return self.compose_ly (s)
811                 return ''
812
813         def do_options (self, option_string, type):
814                 self.option_dict = {}
815
816                 options = split_options (option_string)
817
818                 for i in options:
819                         if string.find (i, '=') > 0:
820                                 (key, value) = re.split ('\s*=\s*', i)
821                                 self.option_dict[key] = value
822                         else:
823                                 if i in no_options.keys ():
824                                         if no_options[i] in self.option_dict.keys ():
825                                                 del self.option_dict[no_options[i]]
826                                 else:
827                                         self.option_dict[i] = None
828
829                 has_line_width = self.option_dict.has_key (LINE_WIDTH)
830                 no_line_width_value = 0
831
832                 # If LINE_WIDTH is used without parameter, set it to default.
833                 if has_line_width and self.option_dict[LINE_WIDTH] == None:
834                         no_line_width_value = 1
835                         del self.option_dict[LINE_WIDTH]
836
837                 for i in default_ly_options.keys ():
838                         if i not in self.option_dict.keys ():
839                                 self.option_dict[i] = default_ly_options[i]
840
841                 if not has_line_width:
842                         if type == 'lilypond' or FRAGMENT in self.option_dict.keys ():
843                                 self.option_dict[RAGGED_RIGHT] = None
844
845                         if type == 'lilypond':
846                                 if LINE_WIDTH in self.option_dict.keys ():
847                                         del self.option_dict[LINE_WIDTH]
848                         else:
849                                 if RAGGED_RIGHT in self.option_dict.keys ():
850                                         if LINE_WIDTH in self.option_dict.keys ():
851                                                 del self.option_dict[LINE_WIDTH]
852
853                         if QUOTE in self.option_dict.keys () or type == 'lilypond':
854                                 if LINE_WIDTH in self.option_dict.keys ():
855                                         del self.option_dict[LINE_WIDTH]
856
857                 if not INDENT in self.option_dict.keys ():
858                         self.option_dict[INDENT] = '0\\mm'
859
860                 # The QUOTE pattern from ly_options only emits the `line-width'
861                 # keyword.
862                 if has_line_width and QUOTE in self.option_dict.keys ():
863                         if no_line_width_value:
864                                 del self.option_dict[LINE_WIDTH]
865                         else:
866                                 del self.option_dict[QUOTE]
867
868         def compose_ly (self, code):
869                 if FRAGMENT in self.option_dict.keys ():
870                         body = FRAGMENT_LY
871                 else:
872                         body = FULL_LY
873
874                 # Defaults.
875                 relative = 1
876                 override = {}
877                 # The original concept of the `exampleindent' option is broken.
878                 # It is not possible to get a sane value for @exampleindent at all
879                 # without processing the document itself.  Saying
880                 #
881                 #   @exampleindent 0
882                 #   @example
883                 #   ...
884                 #   @end example
885                 #   @exampleindent 5
886                 #
887                 # causes ugly results with the DVI backend of texinfo since the
888                 # default value for @exampleindent isn't 5em but 0.4in (or a smaller
889                 # value).  Executing the above code changes the environment
890                 # indentation to an unknown value because we don't know the amount
891                 # of 1em in advance since it is font-dependent.  Modifying
892                 # @exampleindent in the middle of a document is simply not
893                 # supported within texinfo.
894                 #
895                 # As a consequence, the only function of @exampleindent is now to
896                 # specify the amount of indentation for the `quote' option.
897                 #
898                 # To set @exampleindent locally to zero, we use the @format
899                 # environment for non-quoted snippets.
900                 override[EXAMPLEINDENT] = r'0.4\in'
901                 override[LINE_WIDTH] = texinfo_line_widths['@smallbook']
902                 override.update (default_ly_options)
903
904                 option_list = []
905                 for (key, value) in self.option_dict.items ():
906                         if value == None:
907                                 option_list.append (key)
908                         else:
909                                 option_list.append (key + '=' + value)
910                 option_string = string.join (option_list, ',')
911
912                 compose_dict = {}
913                 compose_types = [NOTES, PREAMBLE, LAYOUT, PAPER]
914                 for a in compose_types:
915                         compose_dict[a] = []
916
917                 for (key, value) in self.option_dict.items ():
918                         (c_key, c_value) = \
919                           classic_lilypond_book_compatibility (key, value)
920                         if c_key:
921                                 if c_value:
922                                         ly.warning \
923                                           (_ ("deprecated ly-option used: %s=%s" \
924                                             % (key, value)))
925                                         ly.warning \
926                                           (_ ("compatibility mode translation: %s=%s" \
927                                             % (c_key, c_value)))
928                                 else:
929                                         ly.warning \
930                                           (_ ("deprecated ly-option used: %s" \
931                                             % key))
932                                         ly.warning \
933                                           (_ ("compatibility mode translation: %s" \
934                                             % c_key))
935
936                                 (key, value) = (c_key, c_value)
937
938                         if value:
939                                 override[key] = value
940                         else:
941                                 if not override.has_key (key):
942                                         override[key] = None
943
944                         found = 0
945                         for type in compose_types:
946                                 if ly_options[type].has_key (key):
947                                         compose_dict[type].append (ly_options[type][key])
948                                         found = 1
949                                         break
950
951                         if not found and key not in simple_options:
952                                 ly.warning (_ ("ignoring unknown ly option: %s") % key)
953
954                 # URGS
955                 if RELATIVE in override.keys () and override[RELATIVE]:
956                         relative = string.atoi (override[RELATIVE])
957
958                 relative_quotes = ''
959
960                 # 1 = central C
961                 if relative < 0:
962                         relative_quotes += ',' * (- relative)
963                 elif relative > 0:
964                         relative_quotes += "'" * relative
965
966                 program_name = __main__.program_name
967
968                 paper_string = string.join (compose_dict[PAPER],
969                                             '\n  ') % override
970                 layout_string = string.join (compose_dict[LAYOUT],
971                                              '\n  ') % override
972                 notes_string = string.join (compose_dict[NOTES],
973                                             '\n  ') % vars ()
974                 preamble_string = string.join (compose_dict[PREAMBLE],
975                                                '\n  ') % override
976
977                 font_dump_setting = ''
978                 if FONTLOAD in self.option_dict:
979                         font_dump_setting = '#(define-public force-eps-font-include #t)\n'
980
981                 return (PREAMBLE_LY + body) % vars ()
982
983         # TODO: Use md5?
984         def get_hash (self):
985                 if not self.hash:
986                         self.hash = abs (hash (self.full_ly ()))
987                 return self.hash
988
989         def basename (self):
990                 if FILENAME in self.option_dict:
991                         return self.option_dict[FILENAME]
992                 if global_options.use_hash:
993                         return 'lily-%d' % self.get_hash ()
994                 raise 'to be done'
995
996         def write_ly (self):
997                 outf = open (self.basename () + '.ly', 'w')
998                 outf.write (self.full_ly ())
999
1000                 open (self.basename () + '.txt', 'w').write ('image of music')
1001
1002         def ly_is_outdated (self):
1003                 base = self.basename ()
1004
1005                 tex_file = '%s.tex' % base
1006                 eps_file = '%s.eps' % base
1007                 system_file = '%s-systems.tex' % base
1008                 ly_file = '%s.ly' % base
1009                 ok = os.path.exists (ly_file) \
1010                      and os.path.exists (system_file)\
1011                      and os.stat (system_file)[stat.ST_SIZE] \
1012                      and re.match ('% eof', open (system_file).readlines ()[-1])
1013                 if ok and (not global_options.use_hash or FILENAME in self.option_dict):
1014                         ok = (self.full_ly () == open (ly_file).read ())
1015                 if ok:
1016                         # TODO: Do something smart with target formats
1017                         #       (ps, png) and m/ctimes.
1018                         return None
1019                 return self
1020
1021         def png_is_outdated (self):
1022                 base = self.basename ()
1023                 ok = self.ly_is_outdated ()
1024                 if global_options.format in (HTML, TEXINFO):
1025                         ok = ok and os.path.exists (base + '.eps')
1026
1027                         page_count = 0
1028                         if ok:
1029                                 page_count = ly.ps_page_count (base + '.eps')
1030                         
1031                         if page_count == 1:
1032                                 ok = ok and os.path.exists (base + '.png')
1033                         elif page_count > 1:
1034                                 for a in range (1, page_count + 1):
1035                                                 ok = ok and os.path.exists (base + '-page%d.png' % a)
1036                                 
1037                 return not ok
1038         
1039         def texstr_is_outdated (self):
1040                 if backend == 'ps':
1041                         return 0
1042
1043                 base = self.basename ()
1044                 ok = self.ly_is_outdated ()
1045                 ok = ok and (os.path.exists (base + '.texstr'))
1046                 return not ok
1047
1048         def filter_text (self):
1049                 code = self.substring ('code')
1050                 s = run_filter (code)
1051                 d = {
1052                         'code': s,
1053                         'options': self.match.group ('options')
1054                 }
1055                 # TODO
1056                 return output[self.format][FILTER] % d
1057
1058         def replacement_text (self):
1059                 func = Lilypond_snippet.__dict__['output_' + self.format]
1060                 return func (self)
1061
1062         def get_images (self):
1063                 base = self.basename ()
1064                 # URGUGHUGHUGUGH
1065                 single = '%(base)s.png' % vars ()
1066                 multiple = '%(base)s-page1.png' % vars ()
1067                 images = (single,)
1068                 if os.path.exists (multiple) \
1069                    and (not os.path.exists (single) \
1070                         or (os.stat (multiple)[stat.ST_MTIME] \
1071                             > os.stat (single)[stat.ST_MTIME])):
1072                         count = ly.ps_page_count ('%(base)s.eps' % vars ())
1073                         images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1074                         images = tuple (images)
1075                 return images
1076
1077         def output_html (self):
1078                 str = ''
1079                 base = self.basename ()
1080                 if global_options.format == HTML:
1081                         str += self.output_print_filename (HTML)
1082                         if VERBATIM in self.option_dict:
1083                                 verb = verbatim_html (self.substring ('code'))
1084                                 str += write (output[HTML][VERBATIM] % vars ())
1085                         if QUOTE in self.option_dict:
1086                                 str = output[HTML][QUOTE] % vars ()
1087
1088                 str += output[HTML][BEFORE] % vars ()
1089                 for image in self.get_images ():
1090                         (base, ext) = os.path.splitext (image)
1091                         alt = self.option_dict[ALT]
1092                         str += output[HTML][OUTPUT] % vars ()
1093                 str += output[HTML][AFTER] % vars ()
1094                 return str
1095
1096         def output_info (self):
1097                 str = ''
1098                 for image in self.get_images ():
1099                         (base, ext) = os.path.splitext (image)
1100
1101                         # URG, makeinfo implicitly prepends dot to extension.
1102                         # Specifying no extension is most robust.
1103                         ext = ''
1104                         alt = self.option_dict[ALT]
1105                         str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1106
1107                 base = self.basename ()
1108                 str += output[global_options.format][OUTPUT] % vars ()
1109                 return str
1110
1111         def output_latex (self):
1112                 str = ''
1113                 base = self.basename ()
1114                 if global_options.format == LATEX:
1115                         str += self.output_print_filename (LATEX)
1116                         if VERBATIM in self.option_dict:
1117                                 verb = self.substring ('code')
1118                                 str += (output[LATEX][VERBATIM] % vars ())
1119                 
1120                 str += (output[LATEX][OUTPUT] % vars ())
1121
1122                 ## todo: maintain breaks
1123                 if 0:
1124                         breaks = self.ly ().count ("\n")
1125                         str += "".ljust (breaks, "\n").replace ("\n","%\n")
1126                 
1127                 if QUOTE in self.option_dict:
1128                         str = output[LATEX][QUOTE] % vars ()
1129                 return str
1130
1131         def output_print_filename (self, format):
1132                 str = ''
1133                 if PRINTFILENAME in self.option_dict:
1134                         base = self.basename ()
1135                         filename = self.substring ('filename')
1136                         str = output[global_options.format][PRINTFILENAME] % vars ()
1137
1138                 return str
1139
1140         def output_texinfo (self):
1141                 str = ''
1142                 if self.output_print_filename (TEXINFO):
1143                         str += ('@html\n'
1144                                 + self.output_print_filename (HTML)
1145                                 + '\n@end html\n')
1146                         str += ('@tex\n'
1147                                 + self.output_print_filename (LATEX)
1148                                 + '\n@end tex\n')
1149                 base = self.basename ()
1150                 if TEXIDOC in self.option_dict:
1151                         texidoc = base + '.texidoc'
1152                         if os.path.exists (texidoc):
1153                                 str += '@include %(texidoc)s\n\n' % vars ()
1154
1155                 if VERBATIM in self.option_dict:
1156                         verb = verbatim_texinfo (self.substring ('code'))
1157                         str += (output[TEXINFO][VERBATIM] % vars ())
1158                         if not QUOTE in self.option_dict:
1159                                 str = output[TEXINFO][NOQUOTE] % vars ()
1160
1161                 str += self.output_info ()
1162
1163 #               str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1164 #               str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1165 #               str += ('@html\n' + self.output_html () + '\n@end html\n')
1166
1167                 if QUOTE in self.option_dict:
1168                         str = output[TEXINFO][QUOTE] % vars ()
1169
1170                 # need par after image
1171                 str += '\n'
1172
1173                 return str
1174
1175 class Lilypond_file_snippet (Lilypond_snippet):
1176         def ly (self):
1177                 name = self.substring ('filename')
1178                 return '\\sourcefilename \"%s\"\n%s' \
1179                          % (name, open (find_file (name)).read ())
1180
1181 snippet_type_to_class = {
1182         'lilypond_file': Lilypond_file_snippet,
1183         'lilypond_block': Lilypond_snippet,
1184         'lilypond': Lilypond_snippet,
1185         'include': Include_snippet,
1186 }
1187
1188 def find_linestarts (s):
1189         nls = [0]
1190         start = 0
1191         end = len (s)
1192         while 1:
1193                 i = s.find ('\n', start)
1194                 if i < 0:
1195                         break
1196
1197                 i = i + 1
1198                 nls.append (i)
1199                 start = i
1200
1201         nls.append (len (s))
1202         return nls
1203
1204 def find_toplevel_snippets (s, types):
1205         res = {}
1206         for i in types:
1207                 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1208
1209         snippets = []
1210         index = 0
1211         ## found = dict (map (lambda x: (x, None),
1212         ##                    types))
1213         ## urg python2.1
1214         found = {}
1215         map (lambda x, f = found: f.setdefault (x, None),
1216              types)
1217
1218         line_starts = find_linestarts (s)
1219         line_start_idx = 0
1220         # We want to search for multiple regexes, without searching
1221         # the string multiple times for one regex.
1222         # Hence, we use earlier results to limit the string portion
1223         # where we search.
1224         # Since every part of the string is traversed at most once for
1225         # every type of snippet, this is linear.
1226
1227         while 1:
1228                 first = None
1229                 endex = 1 << 30
1230                 for type in types:
1231                         if not found[type] or found[type][0] < index:
1232                                 found[type] = None
1233                                 
1234                                 m = res[type].search (s[index:endex])
1235                                 if not m:
1236                                         continue
1237
1238                                 cl = Snippet
1239                                 if snippet_type_to_class.has_key (type):
1240                                         cl = snippet_type_to_class[type]
1241
1242
1243                                 start = index + m.start ('match')
1244                                 line_number = line_start_idx
1245                                 while (line_starts[line_number] < start):
1246                                         line_number += 1
1247
1248                                 line_number += 1
1249                                 snip = cl (type, m, global_options.format, line_number)
1250
1251                                 found[type] = (start, snip)
1252
1253                         if found[type] \
1254                            and (not first \
1255                                 or found[type][0] < found[first][0]):
1256                                 first = type
1257
1258                                 # FIXME.
1259
1260                                 # Limiting the search space is a cute
1261                                 # idea, but this *requires* to search
1262                                 # for possible containing blocks
1263                                 # first, at least as long as we do not
1264                                 # search for the start of blocks, but
1265                                 # always/directly for the entire
1266                                 # @block ... @end block.
1267
1268                                 endex = found[first][0]
1269
1270                 if not first:
1271                         snippets.append (Substring (s, index, len (s), line_start_idx))
1272                         break
1273
1274                 while (start > line_starts[line_start_idx+1]):
1275                         line_start_idx += 1
1276
1277                 (start, snip) = found[first]
1278                 snippets.append (Substring (s, index, start, line_start_idx + 1))
1279                 snippets.append (snip)
1280                 found[first] = None
1281                 index = start + len (snip.match.group ('match'))
1282
1283         return snippets
1284
1285 def filter_pipe (input, cmd):
1286         if global_options.verbose:
1287                 ly.progress (_ ("Opening filter `%s'") % cmd)
1288
1289         (stdin, stdout, stderr) = os.popen3 (cmd)
1290         stdin.write (input)
1291         status = stdin.close ()
1292
1293         if not status:
1294                 status = 0
1295                 output = stdout.read ()
1296                 status = stdout.close ()
1297                 error = stderr.read ()
1298
1299         if not status:
1300                 status = 0
1301         signal = 0x0f & status
1302         if status or (not output and error):
1303                 exit_status = status >> 8
1304                 ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1305                 ly.error (_ ("The error log is as follows:"))
1306                 sys.stderr.write (error)
1307                 sys.stderr.write (stderr.read ())
1308                 ly.exit (status)
1309
1310         if global_options.verbose:
1311                 ly.progress ('\n')
1312
1313         return output
1314
1315 def run_filter (s):
1316         return filter_pipe (s, global_options.filter_cmd)
1317
1318 def is_derived_class (cl, baseclass):
1319         if cl == baseclass:
1320                 return 1
1321         for b in cl.__bases__:
1322                 if is_derived_class (b, baseclass):
1323                         return 1
1324         return 0
1325
1326 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1327         ly_names = filter (lambda x: x,
1328                            map (Lilypond_snippet.basename, ly_snippets))
1329         texstr_names = filter (lambda x: x,
1330                            map (Lilypond_snippet.basename, texstr_snippets))
1331         png_names = filter (lambda x: x,
1332                             map (Lilypond_snippet.basename, png_snippets))
1333
1334         status = 0
1335         def my_system (cmd):
1336                 status = ly.system (cmd,
1337                                     ignore_error = 1, progress_p = 1)
1338
1339                 if status:
1340                         ly.error ('Process %s exited unsuccessfully.' % cmd)
1341                         raise Compile_error
1342
1343         # UGH
1344         # the --process=CMD switch is a bad idea
1345         # it is too generic for lilypond-book.
1346         if texstr_names:
1347                 my_system (string.join ([cmd, '--backend texstr',
1348                                          'snippet-map.ly'] + texstr_names))
1349                 for l in texstr_names:
1350                         my_system ('latex %s.texstr' % l)
1351
1352         if ly_names:
1353                 my_system (string.join ([cmd, 'snippet-map.ly'] + ly_names))
1354
1355 LATEX_INSPECTION_DOCUMENT = r'''
1356 \nonstopmode
1357 %(preamble)s
1358 \begin{document}
1359 \typeout{textwidth=\the\textwidth}
1360 \typeout{columnsep=\the\columnsep}
1361 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1362 \end{document}
1363 '''
1364
1365 # Do we need anything else besides `textwidth'?
1366 def get_latex_textwidth (source):
1367         m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1368         preamble = source[:m.start (0)]
1369         latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1370         # Workaround problems with unusable $TMP on Cygwin:
1371         tempfile.tempdir = ''
1372         tmpfile = tempfile.mktemp('.tex')
1373         logfile = os.path.splitext (tmpfile)[0] + '.log'
1374         open (tmpfile,'w').write (latex_document)
1375         ly.system ('latex %s' % tmpfile)
1376         parameter_string = open (logfile).read()
1377         os.unlink (tmpfile)
1378         os.unlink (logfile)
1379
1380         columns = 0
1381         m = re.search ('columns=([0-9.]*)', parameter_string)
1382         if m:
1383                 columns = string.atoi (m.group (1))
1384
1385         columnsep = 0
1386         m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1387         if m:
1388                 columnsep = string.atof (m.group (1))
1389
1390         textwidth = 0
1391         m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1392         if m:
1393                 textwidth = string.atof (m.group (1))
1394                 if columns:
1395                         textwidth = (textwidth - columnsep) / columns
1396
1397         return textwidth
1398
1399 def modify_preamble (chunk):
1400         str = chunk.replacement_text ()
1401         if (re.search (r"\\begin *{document}", str)
1402             and not re.search ("{graphic[sx]", str)):
1403                 str = re.sub (r"\\begin{document}",
1404                               r"\\usepackage{graphics}" + '\n'
1405                               + r"\\begin{document}",
1406                               str)
1407                 chunk.override_text = str 
1408                 
1409         
1410
1411 ext2format = {
1412         '.html': HTML,
1413         '.itely': TEXINFO,
1414         '.latex': LATEX,
1415         '.lytex': LATEX,
1416         '.tely': TEXINFO,
1417         '.tex': LATEX,
1418         '.texi': TEXINFO,
1419         '.texinfo': TEXINFO,
1420         '.xml': HTML,
1421 }
1422
1423 format2ext = {
1424         HTML: '.html',
1425         # TEXINFO: '.texinfo',
1426         TEXINFO: '.texi',
1427         LATEX: '.tex',
1428 }
1429
1430 class Compile_error:
1431         pass
1432
1433 def write_file_map (lys, name):
1434         snippet_map = open ('snippet-map.ly', 'w')
1435         snippet_map.write ("""
1436 #(define version-seen? #t)
1437 #(ly:add-file-name-alist '(
1438 """)
1439         for ly in lys:
1440                 snippet_map.write ('("%s.ly" . "%s:%d (%s.ly)")\n'
1441                                    % (ly.basename (),
1442                                       name,
1443                                       ly.line_number,
1444                                       ly.basename ()))
1445
1446         snippet_map.write ('))\n')
1447
1448 def do_process_cmd (chunks, input_name):
1449         all_lys = filter (lambda x: is_derived_class (x.__class__,
1450                                                       Lilypond_snippet),
1451                           chunks)
1452
1453         write_file_map (all_lys, input_name)
1454         ly_outdated = \
1455           filter (lambda x: is_derived_class (x.__class__,
1456                                               Lilypond_snippet)
1457                             and x.ly_is_outdated (),
1458                   chunks)
1459         texstr_outdated = \
1460           filter (lambda x: is_derived_class (x.__class__,
1461                                               Lilypond_snippet)
1462                             and x.texstr_is_outdated (),
1463                   chunks)
1464         png_outdated = \
1465           filter (lambda x: is_derived_class (x.__class__,
1466                                               Lilypond_snippet)
1467                             and x.png_is_outdated (),
1468                   chunks)
1469
1470         ly.progress (_ ("Writing snippets..."))
1471         map (Lilypond_snippet.write_ly, ly_outdated)
1472         ly.progress ('\n')
1473
1474         if ly_outdated:
1475                 ly.progress (_ ("Processing..."))
1476                 ly.progress ('\n')
1477                 process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
1478         else:
1479                 ly.progress (_ ("All snippets are up to date..."))
1480         ly.progress ('\n')
1481
1482 def guess_format (input_filename):
1483         format = None
1484         e = os.path.splitext (input_filename)[1]
1485         if e in ext2format.keys ():
1486                 # FIXME
1487                 format = ext2format[e]
1488         else:
1489                 ly.error (_ ("can't determine format for: %s" \
1490                              % input_filename))
1491                 ly.exit (1)
1492         return format
1493
1494 def write_if_updated (file_name, lines):
1495         try:
1496                 f = open (file_name)
1497                 oldstr = f.read ()
1498                 new_str = string.join (lines, '')
1499                 if oldstr == new_str:
1500                         ly.progress (_ ("%s is up to date.") % file_name)
1501                         ly.progress ('\n')
1502                         return
1503         except:
1504                 pass
1505
1506         ly.progress (_ ("Writing `%s'...") % file_name)
1507         open (file_name, 'w').writelines (lines)
1508         ly.progress ('\n')
1509
1510 def note_input_file (name, inputs=[]):
1511         inputs.append (name)
1512         return inputs
1513
1514 def do_file (input_filename):
1515         # Ugh.
1516         if not input_filename or input_filename == '-':
1517                 in_handle = sys.stdin
1518                 input_fullname = '<stdin>'
1519         else:
1520                 if os.path.exists (input_filename):
1521                         input_fullname = input_filename
1522                 elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'): 
1523                         # urg python interface to libkpathsea?
1524                         input_fullname = ly.read_pipe ('kpsewhich '
1525                                                        + input_filename)[:-1]
1526                 else:
1527                         input_fullname = find_file (input_filename)
1528
1529                 note_input_file (input_fullname)
1530                 in_handle = open (input_fullname)
1531
1532         if input_filename == '-':
1533                 input_base = 'stdin'
1534         else:
1535                 input_base = os.path.basename \
1536                              (os.path.splitext (input_filename)[0])
1537
1538         # Only default to stdout when filtering.
1539         if global_options.output_name == '-' or (not global_options.output_name and global_options.filter_cmd):
1540                 output_filename = '-'
1541                 output_file = sys.stdout
1542         else:
1543                 # don't complain when global_options.output_name is existing
1544                 output_filename = input_base + format2ext[global_options.format]
1545                 if global_options.output_name:
1546                         if not os.path.isdir (global_options.output_name):
1547                                 os.mkdir (global_options.output_name, 0777)
1548                         os.chdir (global_options.output_name)
1549                 else: 
1550                         if os.path.exists (input_filename) \
1551                            and os.path.exists (output_filename) \
1552                            and os.path.samefile (output_filename, input_fullname):
1553                            ly.error (
1554                            _ ("Output would overwrite input file; use --output."))
1555                            ly.exit (2)
1556
1557         try:
1558                 ly.progress (_ ("Reading %s...") % input_fullname)
1559                 source = in_handle.read ()
1560                 ly.progress ('\n')
1561
1562                 set_default_options (source)
1563
1564
1565                 # FIXME: Containing blocks must be first, see
1566                 #        find_toplevel_snippets.
1567                 snippet_types = (
1568                         'multiline_comment',
1569                         'verbatim',
1570                         'lilypond_block',
1571         #               'verb',
1572                         'singleline_comment',
1573                         'lilypond_file',
1574                         'include',
1575                         'lilypond',
1576                 )
1577                 ly.progress (_ ("Dissecting..."))
1578                 chunks = find_toplevel_snippets (source, snippet_types)
1579
1580                 if global_options.format == LATEX:
1581                         for c in chunks:
1582                                 if (c.is_plain () and
1583                                     re.search (r"\\begin *{document}", c.replacement_text())):
1584                                         modify_preamble (c)
1585                                         break
1586                 ly.progress ('\n')
1587
1588                 if global_options.filter_cmd:
1589                         write_if_updated (output_filename,
1590                                           [c.filter_text () for c in chunks])
1591                 elif global_options.process_cmd:
1592                         do_process_cmd (chunks, input_fullname)
1593                         ly.progress (_ ("Compiling %s...") % output_filename)
1594                         ly.progress ('\n')
1595                         write_if_updated (output_filename,
1596                                           [s.replacement_text ()
1597                                            for s in chunks])
1598                 
1599                 def process_include (snippet):
1600                         os.chdir (original_dir)
1601                         name = snippet.substring ('filename')
1602                         ly.progress (_ ("Processing include: %s") % name)
1603                         ly.progress ('\n')
1604                         return do_file (name)
1605
1606                 include_chunks = map (process_include,
1607                                       filter (lambda x: is_derived_class (x.__class__,
1608                                                                           Include_snippet),
1609                                               chunks))
1610
1611
1612                 return chunks + reduce (lambda x,y: x + y, include_chunks, [])
1613                 
1614         except Compile_error:
1615                 os.chdir (original_dir)
1616                 ly.progress (_ ("Removing `%s'") % output_filename)
1617                 ly.progress ('\n')
1618                 raise Compile_error
1619
1620 def do_options ():
1621         opt_parser = get_option_parser()
1622         (options, args) = opt_parser.parse_args ()
1623
1624         if options.format in ('texi-html', 'texi'):
1625                 options.format = TEXINFO
1626         options.use_hash = True
1627         options.pseudo_filter = False
1628         
1629         global global_options
1630         global_options = options
1631         return args
1632
1633 def main ():
1634         files = do_options ()
1635         if not files or len (files) > 1:
1636                 ly.help ()
1637                 ly.exit (2)
1638
1639         file = files[0]
1640
1641         basename = os.path.splitext (file)[0]
1642         basename = os.path.split (basename)[1]
1643         
1644         if not global_options.format:
1645                 global_options.format = guess_format (files[0])
1646
1647         formats = 'ps'
1648         if global_options.format in (TEXINFO, HTML):
1649                 formats += ',png'
1650         if global_options.process_cmd == '':
1651                 global_options.process_cmd = lilypond_binary \
1652                               + ' --formats=%s --backend eps ' % formats
1653
1654         if global_options.process_cmd:
1655                 global_options.process_cmd += string.join ([(' -I %s' % commands.mkarg (p))
1656                                                             for p in global_options.include_path])
1657
1658         ly.identify (sys.stderr)
1659
1660         try:
1661                 chunks = do_file (file)
1662                 if global_options.psfonts:
1663                         fontextract.verbose = global_options.verbose
1664                         snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1665                                                                               Lilypond_snippet),
1666                                                  chunks)
1667
1668                         psfonts_file = basename + '.psfonts' 
1669                         if not global_options.verbose:
1670                                 ly.progress (_ ("Writing fonts to %s...") % psfonts_file)
1671                         fontextract.extract_fonts (psfonts_file,
1672                                                    [x.basename() + '.eps'
1673                                                     for x in snippet_chunks])
1674                         if not global_options.verbose:
1675                                 ly.progress ('\n')
1676                         
1677         except Compile_error:
1678                 ly.exit (1)
1679
1680         if global_options.format in (TEXINFO, LATEX):
1681                 if not global_options.psfonts:
1682                         ly.warning (_ ("option --psfonts not used"))
1683                         ly.warning (_ ("processing with dvips will have no fonts"))
1684
1685                 psfonts_file = os.path.join (global_options.output_name, basename + '.psfonts')
1686                 output = os.path.join (global_options.output_name, basename +  '.dvi' )
1687                 
1688                 ly.progress ('\n')
1689                 ly.progress (_ ("DVIPS usage:"))
1690                 ly.progress ('\n')
1691                 ly.progress ("    dvips -h %(psfonts_file)s %(output)s" % vars ())
1692                 ly.progress ('\n')
1693
1694         inputs = note_input_file ('')
1695         inputs.pop ()
1696
1697         base_file_name = os.path.splitext (os.path.basename (file))[0]
1698         dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1699         final_output_file = os.path.join (global_options.output_name,
1700                                           base_file_name
1701                                           + '.%s' % global_options.format)
1702         
1703         os.chdir (original_dir)
1704         open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1705
1706 if __name__ == '__main__':
1707         main ()