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