]> git.donarmstrong.com Git - lilypond.git/blob - scripts/lilypond-book.py
48a16f49c365d36f8b52067e561d9b97d49e95b8
[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                 d = globals().copy()
1000                 d.update (locals())
1001                 return (PREAMBLE_LY + body) % d
1002
1003         # TODO: Use md5?
1004         def get_hash (self):
1005                 if not self.hash:
1006                         self.hash = abs (hash (self.full_ly ()))
1007                 return self.hash
1008
1009         def basename (self):
1010                 if FILENAME in self.option_dict:
1011                         return self.option_dict[FILENAME]
1012                 if global_options.use_hash:
1013                         return 'lily-%d' % self.get_hash ()
1014                 raise 'to be done'
1015
1016         def write_ly (self):
1017                 outf = open (self.basename () + '.ly', 'w')
1018                 outf.write (self.full_ly ())
1019
1020                 open (self.basename () + '.txt', 'w').write ('image of music')
1021
1022         def ly_is_outdated (self):
1023                 base = self.basename ()
1024
1025                 tex_file = '%s.tex' % base
1026                 eps_file = '%s.eps' % base
1027                 system_file = '%s-systems.tex' % base
1028                 ly_file = '%s.ly' % base
1029                 ok = os.path.exists (ly_file) \
1030                      and os.path.exists (system_file)\
1031                      and os.stat (system_file)[stat.ST_SIZE] \
1032                      and re.match ('% eof', open (system_file).readlines ()[-1])
1033                 if ok and (not global_options.use_hash or FILENAME in self.option_dict):
1034                         ok = (self.full_ly () == open (ly_file).read ())
1035                 if ok:
1036                         # TODO: Do something smart with target formats
1037                         #       (ps, png) and m/ctimes.
1038                         return None
1039                 return self
1040
1041         def png_is_outdated (self):
1042                 base = self.basename ()
1043                 ok = self.ly_is_outdated ()
1044                 if global_options.format in (HTML, TEXINFO):
1045                         ok = ok and os.path.exists (base + '.eps')
1046
1047                         page_count = 0
1048                         if ok:
1049                                 page_count = ly.ps_page_count (base + '.eps')
1050                         
1051                         if page_count == 1:
1052                                 ok = ok and os.path.exists (base + '.png')
1053                         elif page_count > 1:
1054                                 for a in range (1, page_count + 1):
1055                                                 ok = ok and os.path.exists (base + '-page%d.png' % a)
1056                                 
1057                 return not ok
1058         
1059         def texstr_is_outdated (self):
1060                 if backend == 'ps':
1061                         return 0
1062
1063                 base = self.basename ()
1064                 ok = self.ly_is_outdated ()
1065                 ok = ok and (os.path.exists (base + '.texstr'))
1066                 return not ok
1067
1068         def filter_text (self):
1069                 code = self.substring ('code')
1070                 s = run_filter (code)
1071                 d = {
1072                         'code': s,
1073                         'options': self.match.group ('options')
1074                 }
1075                 # TODO
1076                 return output[self.format][FILTER] % d
1077
1078         def replacement_text (self):
1079                 func = Lilypond_snippet.__dict__['output_' + self.format]
1080                 return func (self)
1081
1082         def get_images (self):
1083                 base = self.basename ()
1084                 # URGUGHUGHUGUGH
1085                 single = '%(base)s.png' % vars ()
1086                 multiple = '%(base)s-page1.png' % vars ()
1087                 images = (single,)
1088                 if os.path.exists (multiple) \
1089                    and (not os.path.exists (single) \
1090                         or (os.stat (multiple)[stat.ST_MTIME] \
1091                             > os.stat (single)[stat.ST_MTIME])):
1092                         count = ly.ps_page_count ('%(base)s.eps' % vars ())
1093                         images = ['%s-page%d.png' % (base, a) for a in range (1, count+1)]
1094                         images = tuple (images)
1095                 return images
1096
1097         def output_html (self):
1098                 str = ''
1099                 base = self.basename ()
1100                 if global_options.format == HTML:
1101                         str += self.output_print_filename (HTML)
1102                         if VERBATIM in self.option_dict:
1103                                 verb = verbatim_html (self.substring ('code'))
1104                                 str += output[HTML][VERBATIM] % vars ()
1105                         if QUOTE in self.option_dict:
1106                                 str = output[HTML][QUOTE] % vars ()
1107
1108                 str += output[HTML][BEFORE] % vars ()
1109                 for image in self.get_images ():
1110                         (base, ext) = os.path.splitext (image)
1111                         alt = self.option_dict[ALT]
1112                         str += output[HTML][OUTPUT] % vars ()
1113                 str += output[HTML][AFTER] % vars ()
1114                 return str
1115
1116         def output_info (self):
1117                 str = ''
1118                 for image in self.get_images ():
1119                         (base, ext) = os.path.splitext (image)
1120
1121                         # URG, makeinfo implicitly prepends dot to extension.
1122                         # Specifying no extension is most robust.
1123                         ext = ''
1124                         alt = self.option_dict[ALT]
1125                         str += output[TEXINFO][OUTPUTIMAGE] % vars ()
1126
1127                 base = self.basename ()
1128                 str += output[global_options.format][OUTPUT] % vars ()
1129                 return str
1130
1131         def output_latex (self):
1132                 str = ''
1133                 base = self.basename ()
1134                 if global_options.format == LATEX:
1135                         str += self.output_print_filename (LATEX)
1136                         if VERBATIM in self.option_dict:
1137                                 verb = self.substring ('code')
1138                                 str += (output[LATEX][VERBATIM] % vars ())
1139                 
1140                 str += (output[LATEX][OUTPUT] % vars ())
1141
1142                 ## todo: maintain breaks
1143                 if 0:
1144                         breaks = self.ly ().count ("\n")
1145                         str += "".ljust (breaks, "\n").replace ("\n","%\n")
1146                 
1147                 if QUOTE in self.option_dict:
1148                         str = output[LATEX][QUOTE] % vars ()
1149                 return str
1150
1151         def output_print_filename (self, format):
1152                 str = ''
1153                 if PRINTFILENAME in self.option_dict:
1154                         base = self.basename ()
1155                         filename = self.substring ('filename')
1156                         str = output[global_options.format][PRINTFILENAME] % vars ()
1157
1158                 return str
1159
1160         def output_texinfo (self):
1161                 str = ''
1162                 if self.output_print_filename (TEXINFO):
1163                         str += ('@html\n'
1164                                 + self.output_print_filename (HTML)
1165                                 + '\n@end html\n')
1166                         str += ('@tex\n'
1167                                 + self.output_print_filename (LATEX)
1168                                 + '\n@end tex\n')
1169                 base = self.basename ()
1170                 if TEXIDOC in self.option_dict:
1171                         texidoc = base + '.texidoc'
1172                         if os.path.exists (texidoc):
1173                                 str += '@include %(texidoc)s\n\n' % vars ()
1174
1175                 if VERBATIM in self.option_dict:
1176                         verb = verbatim_texinfo (self.substring ('code'))
1177                         str += (output[TEXINFO][VERBATIM] % vars ())
1178                         if not QUOTE in self.option_dict:
1179                                 str = output[TEXINFO][NOQUOTE] % vars ()
1180
1181                 str += self.output_info ()
1182
1183 #               str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
1184 #               str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
1185 #               str += ('@html\n' + self.output_html () + '\n@end html\n')
1186
1187                 if QUOTE in self.option_dict:
1188                         str = output[TEXINFO][QUOTE] % vars ()
1189
1190                 # need par after image
1191                 str += '\n'
1192
1193                 return str
1194
1195 class Lilypond_file_snippet (Lilypond_snippet):
1196         def ly (self):
1197                 name = self.substring ('filename')
1198                 return '\\sourcefilename \"%s\"\n%s' \
1199                          % (name, open (find_file (name)).read ())
1200
1201 snippet_type_to_class = {
1202         'lilypond_file': Lilypond_file_snippet,
1203         'lilypond_block': Lilypond_snippet,
1204         'lilypond': Lilypond_snippet,
1205         'include': Include_snippet,
1206 }
1207
1208 def find_linestarts (s):
1209         nls = [0]
1210         start = 0
1211         end = len (s)
1212         while 1:
1213                 i = s.find ('\n', start)
1214                 if i < 0:
1215                         break
1216
1217                 i = i + 1
1218                 nls.append (i)
1219                 start = i
1220
1221         nls.append (len (s))
1222         return nls
1223
1224 def find_toplevel_snippets (s, types):
1225         res = {}
1226         for i in types:
1227                 res[i] = ly.re.compile (snippet_res[global_options.format][i])
1228
1229         snippets = []
1230         index = 0
1231         ## found = dict (map (lambda x: (x, None),
1232         ##                    types))
1233         ## urg python2.1
1234         found = {}
1235         map (lambda x, f = found: f.setdefault (x, None),
1236              types)
1237
1238         line_starts = find_linestarts (s)
1239         line_start_idx = 0
1240         # We want to search for multiple regexes, without searching
1241         # the string multiple times for one regex.
1242         # Hence, we use earlier results to limit the string portion
1243         # where we search.
1244         # Since every part of the string is traversed at most once for
1245         # every type of snippet, this is linear.
1246
1247         while 1:
1248                 first = None
1249                 endex = 1 << 30
1250                 for type in types:
1251                         if not found[type] or found[type][0] < index:
1252                                 found[type] = None
1253                                 
1254                                 m = res[type].search (s[index:endex])
1255                                 if not m:
1256                                         continue
1257
1258                                 cl = Snippet
1259                                 if snippet_type_to_class.has_key (type):
1260                                         cl = snippet_type_to_class[type]
1261
1262
1263                                 start = index + m.start ('match')
1264                                 line_number = line_start_idx
1265                                 while (line_starts[line_number] < start):
1266                                         line_number += 1
1267
1268                                 line_number += 1
1269                                 snip = cl (type, m, global_options.format, line_number)
1270
1271                                 found[type] = (start, snip)
1272
1273                         if found[type] \
1274                            and (not first \
1275                                 or found[type][0] < found[first][0]):
1276                                 first = type
1277
1278                                 # FIXME.
1279
1280                                 # Limiting the search space is a cute
1281                                 # idea, but this *requires* to search
1282                                 # for possible containing blocks
1283                                 # first, at least as long as we do not
1284                                 # search for the start of blocks, but
1285                                 # always/directly for the entire
1286                                 # @block ... @end block.
1287
1288                                 endex = found[first][0]
1289
1290                 if not first:
1291                         snippets.append (Substring (s, index, len (s), line_start_idx))
1292                         break
1293
1294                 while (start > line_starts[line_start_idx+1]):
1295                         line_start_idx += 1
1296
1297                 (start, snip) = found[first]
1298                 snippets.append (Substring (s, index, start, line_start_idx + 1))
1299                 snippets.append (snip)
1300                 found[first] = None
1301                 index = start + len (snip.match.group ('match'))
1302
1303         return snippets
1304
1305 def filter_pipe (input, cmd):
1306         if global_options.verbose:
1307                 ly.progress (_ ("Opening filter `%s'") % cmd)
1308
1309         (stdin, stdout, stderr) = os.popen3 (cmd)
1310         stdin.write (input)
1311         status = stdin.close ()
1312
1313         if not status:
1314                 status = 0
1315                 output = stdout.read ()
1316                 status = stdout.close ()
1317                 error = stderr.read ()
1318
1319         if not status:
1320                 status = 0
1321         signal = 0x0f & status
1322         if status or (not output and error):
1323                 exit_status = status >> 8
1324                 ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status))
1325                 ly.error (_ ("The error log is as follows:"))
1326                 sys.stderr.write (error)
1327                 sys.stderr.write (stderr.read ())
1328                 ly.exit (status)
1329
1330         if global_options.verbose:
1331                 ly.progress ('\n')
1332
1333         return output
1334
1335 def run_filter (s):
1336         return filter_pipe (s, global_options.filter_cmd)
1337
1338 def is_derived_class (cl, baseclass):
1339         if cl == baseclass:
1340                 return 1
1341         for b in cl.__bases__:
1342                 if is_derived_class (b, baseclass):
1343                         return 1
1344         return 0
1345
1346 def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
1347         ly_names = filter (lambda x: x,
1348                            map (Lilypond_snippet.basename, ly_snippets))
1349         texstr_names = filter (lambda x: x,
1350                            map (Lilypond_snippet.basename, texstr_snippets))
1351         png_names = filter (lambda x: x,
1352                             map (Lilypond_snippet.basename, png_snippets))
1353
1354         status = 0
1355         def my_system (cmd):
1356                 status = ly.system (cmd,
1357                                     ignore_error = 1, progress_p = 1)
1358
1359                 if status:
1360                         ly.error ('Process %s exited unsuccessfully.' % cmd)
1361                         raise Compile_error
1362
1363         # UGH
1364         # the --process=CMD switch is a bad idea
1365         # it is too generic for lilypond-book.
1366         if texstr_names:
1367                 my_system (string.join ([cmd, '--backend texstr',
1368                                          'snippet-map.ly'] + texstr_names))
1369                 for l in texstr_names:
1370                         my_system ('latex %s.texstr' % l)
1371
1372         if ly_names:
1373                 my_system (string.join ([cmd, 'snippet-map.ly'] + ly_names))
1374
1375 LATEX_INSPECTION_DOCUMENT = r'''
1376 \nonstopmode
1377 %(preamble)s
1378 \begin{document}
1379 \typeout{textwidth=\the\textwidth}
1380 \typeout{columnsep=\the\columnsep}
1381 \makeatletter\if@twocolumn\typeout{columns=2}\fi\makeatother
1382 \end{document}
1383 '''
1384
1385 # Do we need anything else besides `textwidth'?
1386 def get_latex_textwidth (source):
1387         m = re.search (r'''(?P<preamble>\\begin\s*{document})''', source)
1388         preamble = source[:m.start (0)]
1389         latex_document = LATEX_INSPECTION_DOCUMENT % vars ()
1390         
1391         (handle, tmpfile) = tempfile.mkstemp('.tex')
1392         logfile = os.path.splitext (tmpfile)[0] + '.log'
1393         open (tmpfile,'w').write (latex_document)
1394         ly.system ('latex %s' % tmpfile)
1395         parameter_string = open (logfile).read()
1396         
1397         os.unlink (tmpfile)
1398         os.unlink (logfile)
1399
1400         columns = 0
1401         m = re.search ('columns=([0-9.]*)', parameter_string)
1402         if m:
1403                 columns = int (m.group (1))
1404
1405         columnsep = 0
1406         m = re.search ('columnsep=([0-9.]*)pt', parameter_string)
1407         if m:
1408                 columnsep = float (m.group (1))
1409
1410         textwidth = 0
1411         m = re.search ('textwidth=([0-9.]*)pt', parameter_string)
1412         if m:
1413                 textwidth = float (m.group (1))
1414                 if columns:
1415                         textwidth = (textwidth - columnsep) / columns
1416
1417         return textwidth
1418
1419 def modify_preamble (chunk):
1420         str = chunk.replacement_text ()
1421         if (re.search (r"\\begin *{document}", str)
1422             and not re.search ("{graphic[sx]", str)):
1423                 str = re.sub (r"\\begin{document}",
1424                               r"\\usepackage{graphics}" + '\n'
1425                               + r"\\begin{document}",
1426                               str)
1427                 chunk.override_text = str 
1428                 
1429         
1430
1431 ext2format = {
1432         '.html': HTML,
1433         '.itely': TEXINFO,
1434         '.latex': LATEX,
1435         '.lytex': LATEX,
1436         '.tely': TEXINFO,
1437         '.tex': LATEX,
1438         '.texi': TEXINFO,
1439         '.texinfo': TEXINFO,
1440         '.xml': HTML,
1441 }
1442
1443 format2ext = {
1444         HTML: '.html',
1445         # TEXINFO: '.texinfo',
1446         TEXINFO: '.texi',
1447         LATEX: '.tex',
1448 }
1449
1450 class Compile_error:
1451         pass
1452
1453 def write_file_map (lys, name):
1454         snippet_map = open ('snippet-map.ly', 'w')
1455         snippet_map.write ("""
1456 #(define version-seen? #t)
1457 #(ly:add-file-name-alist '(
1458 """)
1459         for ly in lys:
1460                 snippet_map.write ('("%s.ly" . "%s:%d (%s.ly)")\n'
1461                                    % (ly.basename (),
1462                                       name,
1463                                       ly.line_number,
1464                                       ly.basename ()))
1465
1466         snippet_map.write ('))\n')
1467
1468 def do_process_cmd (chunks, input_name):
1469         all_lys = filter (lambda x: is_derived_class (x.__class__,
1470                                                       Lilypond_snippet),
1471                           chunks)
1472
1473         write_file_map (all_lys, input_name)
1474         ly_outdated = \
1475           filter (lambda x: is_derived_class (x.__class__,
1476                                               Lilypond_snippet)
1477                             and x.ly_is_outdated (),
1478                   chunks)
1479         texstr_outdated = \
1480           filter (lambda x: is_derived_class (x.__class__,
1481                                               Lilypond_snippet)
1482                             and x.texstr_is_outdated (),
1483                   chunks)
1484         png_outdated = \
1485           filter (lambda x: is_derived_class (x.__class__,
1486                                               Lilypond_snippet)
1487                             and x.png_is_outdated (),
1488                   chunks)
1489
1490         ly.progress (_ ("Writing snippets..."))
1491         map (Lilypond_snippet.write_ly, ly_outdated)
1492         ly.progress ('\n')
1493
1494         if ly_outdated:
1495                 ly.progress (_ ("Processing..."))
1496                 ly.progress ('\n')
1497                 process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
1498         else:
1499                 ly.progress (_ ("All snippets are up to date..."))
1500         ly.progress ('\n')
1501
1502 def guess_format (input_filename):
1503         format = None
1504         e = os.path.splitext (input_filename)[1]
1505         if e in ext2format.keys ():
1506                 # FIXME
1507                 format = ext2format[e]
1508         else:
1509                 ly.error (_ ("can't determine format for: %s" \
1510                              % input_filename))
1511                 ly.exit (1)
1512         return format
1513
1514 def write_if_updated (file_name, lines):
1515         try:
1516                 f = open (file_name)
1517                 oldstr = f.read ()
1518                 new_str = string.join (lines, '')
1519                 if oldstr == new_str:
1520                         ly.progress (_ ("%s is up to date.") % file_name)
1521                         ly.progress ('\n')
1522                         return
1523         except:
1524                 pass
1525
1526         ly.progress (_ ("Writing `%s'...") % file_name)
1527         open (file_name, 'w').writelines (lines)
1528         ly.progress ('\n')
1529
1530 def note_input_file (name, inputs=[]):
1531         ## hack: inputs is mutable!
1532         inputs.append (name)
1533         return inputs
1534
1535 def do_file (input_filename):
1536         # Ugh.
1537         if not input_filename or input_filename == '-':
1538                 in_handle = sys.stdin
1539                 input_fullname = '<stdin>'
1540         else:
1541                 if os.path.exists (input_filename):
1542                         input_fullname = input_filename
1543                 elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'): 
1544                         # urg python interface to libkpathsea?
1545                         input_fullname = ly.read_pipe ('kpsewhich '
1546                                                        + input_filename)[:-1]
1547                 else:
1548                         input_fullname = find_file (input_filename)
1549
1550                 note_input_file (input_fullname)
1551                 in_handle = open (input_fullname)
1552
1553         if input_filename == '-':
1554                 input_base = 'stdin'
1555         else:
1556                 input_base = os.path.basename \
1557                              (os.path.splitext (input_filename)[0])
1558
1559         # Only default to stdout when filtering.
1560         if global_options.output_name == '-' or (not global_options.output_name and global_options.filter_cmd):
1561                 output_filename = '-'
1562                 output_file = sys.stdout
1563         else:
1564                 # don't complain when global_options.output_name is existing
1565                 output_filename = input_base + format2ext[global_options.format]
1566                 if global_options.output_name:
1567                         if not os.path.isdir (global_options.output_name):
1568                                 os.mkdir (global_options.output_name, 0777)
1569                         os.chdir (global_options.output_name)
1570                 else: 
1571                         if os.path.exists (input_filename) \
1572                            and os.path.exists (output_filename) \
1573                            and os.path.samefile (output_filename, input_fullname):
1574                            ly.error (
1575                            _ ("Output would overwrite input file; use --output."))
1576                            ly.exit (2)
1577
1578         try:
1579                 ly.progress (_ ("Reading %s...") % input_fullname)
1580                 source = in_handle.read ()
1581                 ly.progress ('\n')
1582
1583                 set_default_options (source)
1584
1585
1586                 # FIXME: Containing blocks must be first, see
1587                 #        find_toplevel_snippets.
1588                 snippet_types = (
1589                         'multiline_comment',
1590                         'verbatim',
1591                         'lilypond_block',
1592         #               'verb',
1593                         'singleline_comment',
1594                         'lilypond_file',
1595                         'include',
1596                         'lilypond',
1597                 )
1598                 ly.progress (_ ("Dissecting..."))
1599                 chunks = find_toplevel_snippets (source, snippet_types)
1600
1601                 if global_options.format == LATEX:
1602                         for c in chunks:
1603                                 if (c.is_plain () and
1604                                     re.search (r"\\begin *{document}", c.replacement_text())):
1605                                         modify_preamble (c)
1606                                         break
1607                 ly.progress ('\n')
1608
1609                 if global_options.filter_cmd:
1610                         write_if_updated (output_filename,
1611                                           [c.filter_text () for c in chunks])
1612                 elif global_options.process_cmd:
1613                         do_process_cmd (chunks, input_fullname)
1614                         ly.progress (_ ("Compiling %s...") % output_filename)
1615                         ly.progress ('\n')
1616                         write_if_updated (output_filename,
1617                                           [s.replacement_text ()
1618                                            for s in chunks])
1619                 
1620                 def process_include (snippet):
1621                         os.chdir (original_dir)
1622                         name = snippet.substring ('filename')
1623                         ly.progress (_ ("Processing include: %s") % name)
1624                         ly.progress ('\n')
1625                         return do_file (name)
1626
1627                 include_chunks = map (process_include,
1628                                       filter (lambda x: is_derived_class (x.__class__,
1629                                                                           Include_snippet),
1630                                               chunks))
1631
1632
1633                 return chunks + reduce (lambda x,y: x + y, include_chunks, [])
1634                 
1635         except Compile_error:
1636                 os.chdir (original_dir)
1637                 ly.progress (_ ("Removing `%s'") % output_filename)
1638                 ly.progress ('\n')
1639                 raise Compile_error
1640
1641 def do_options ():
1642         opt_parser = get_option_parser()
1643         (options, args) = opt_parser.parse_args ()
1644
1645         if options.format in ('texi-html', 'texi'):
1646                 options.format = TEXINFO
1647         options.use_hash = True
1648         options.pseudo_filter = False
1649
1650         options.include_path =  map (os.path.abspath, options.include_path)
1651
1652         global global_options
1653         global_options = options
1654         return args
1655
1656 def main ():
1657         files = do_options ()
1658         if not files or len (files) > 1:
1659                 ly.help ()
1660                 ly.exit (2)
1661
1662         file = files[0]
1663
1664         basename = os.path.splitext (file)[0]
1665         basename = os.path.split (basename)[1]
1666         
1667         if not global_options.format:
1668                 global_options.format = guess_format (files[0])
1669
1670         formats = 'ps'
1671         if global_options.format in (TEXINFO, HTML):
1672                 formats += ',png'
1673         if global_options.process_cmd == '':
1674                 global_options.process_cmd = lilypond_binary \
1675                               + ' --formats=%s --backend eps ' % formats
1676
1677         if global_options.process_cmd:
1678                 global_options.process_cmd += string.join ([(' -I %s' % commands.mkarg (p))
1679                                                             for p in global_options.include_path])
1680
1681         ly.identify (sys.stderr)
1682
1683         try:
1684                 chunks = do_file (file)
1685                 if global_options.psfonts:
1686                         fontextract.verbose = global_options.verbose
1687                         snippet_chunks = filter (lambda x: is_derived_class (x.__class__,
1688                                                                               Lilypond_snippet),
1689                                                  chunks)
1690
1691                         psfonts_file = basename + '.psfonts' 
1692                         if not global_options.verbose:
1693                                 ly.progress (_ ("Writing fonts to %s...") % psfonts_file)
1694                         fontextract.extract_fonts (psfonts_file,
1695                                                    [x.basename() + '.eps'
1696                                                     for x in snippet_chunks])
1697                         if not global_options.verbose:
1698                                 ly.progress ('\n')
1699                         
1700         except Compile_error:
1701                 ly.exit (1)
1702
1703         if global_options.format in (TEXINFO, LATEX):
1704                 if not global_options.psfonts:
1705                         ly.warning (_ ("option --psfonts not used"))
1706                         ly.warning (_ ("processing with dvips will have no fonts"))
1707
1708                 psfonts_file = os.path.join (global_options.output_name, basename + '.psfonts')
1709                 output = os.path.join (global_options.output_name, basename +  '.dvi' )
1710                 
1711                 ly.progress ('\n')
1712                 ly.progress (_ ("DVIPS usage:"))
1713                 ly.progress ('\n')
1714                 ly.progress ("    dvips -h %(psfonts_file)s %(output)s" % vars ())
1715                 ly.progress ('\n')
1716
1717         inputs = note_input_file ('')
1718         inputs.pop ()
1719
1720         base_file_name = os.path.splitext (os.path.basename (file))[0]
1721         dep_file = os.path.join (global_options.output_name, base_file_name + '.dep')
1722         final_output_file = os.path.join (global_options.output_name,
1723                                           base_file_name
1724                                           + '.%s' % global_options.format)
1725         
1726         os.chdir (original_dir)
1727         open (dep_file, 'w').write ('%s: %s' % (final_output_file, ' '.join (inputs)))
1728
1729 if __name__ == '__main__':
1730         main ()