]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
* lily/book.cc:
[lilypond.git] / scripts / lilypond-book.py
index de067edc25dca0906a1669f6ef4a9054078c4da7..559a08012e031fba7121b10bc30e3eeccd018425 100644 (file)
@@ -15,7 +15,7 @@ classic lilypond-book:
 TODO:
     *  ly-options: intertext?
     *  --linewidth?
-    *  eps in latex / eps by lilypond -fps?
+    *  eps in latex / eps by lilypond -ps?
     *  check latex parameters, twocolumn, multicolumn?
     *  use --png --ps --pdf for making images?
 
@@ -64,6 +64,7 @@ program_name = sys.argv[0]
 verbose_p = 0
 pseudo_filter_p = 0
 original_dir = os.getcwd ()
+backend = 'ps'
 
 help_summary = _ (
 '''Process LilyPond snippets in hybrid HTML, LaTeX, or texinfo document.
@@ -133,6 +134,7 @@ NOQUOTE = 'noquote'
 NOTES = 'body'
 NOTIME = 'notime'
 OUTPUT = 'output'
+OUTPUTIMAGE = 'outputimage'
 PAPER = 'paper'
 PREAMBLE = 'preamble'
 PRINTFILENAME = 'printfilename'
@@ -143,6 +145,8 @@ STAFFSIZE = 'staffsize'
 TEXIDOC = 'texidoc'
 TEXINFO = 'texinfo'
 VERBATIM = 'verbatim'
+FONTLOAD = 'fontload'
+
 
 # NOTIME has no opposite so it isn't part of this dictionary.
 # NOQUOTE is used internally only.
@@ -378,7 +382,8 @@ simple_options = [
        NOINDENT,
        PRINTFILENAME,
        TEXIDOC,
-       VERBATIM
+       VERBATIM,
+       FONTLOAD
 ]
 
 ly_options = {
@@ -453,7 +458,7 @@ output = {
   \preLilyPondExample
 \fi
 \def\lilypondbook{}%%
-\input %(base)s.tex
+\input %(base)s-systems.tex
 \ifx\postLilyPondExample \undefined
   \relax
 \else
@@ -485,8 +490,25 @@ output = {
 %(code)s
 @lilypond''',
 
-               OUTPUT: r'''@noindent
-@image{%(base)s,,,[image of music],%(ext)s}''',
+               OUTPUT: r'''
+@iftex
+@include %(base)s-systems.texi
+@end iftex
+''',
+
+               OUTPUTIMAGE: r'''@noindent
+@ifinfo
+@image{%(base)s,,,[image of music],%(ext)s}
+@end ifinfo
+@html
+<p>
+  <a href="%(base)s.ly">
+    <img align="center" valign="center"
+        border="0" src="%(image)s" alt="[image of music]">
+  </a>
+</p>
+@end html
+''',
 
                PRINTFILENAME: '''@file{%(filename)s}
        ''',
@@ -519,6 +541,7 @@ PREAMBLE_LY = r'''%%%% Generated by %(program_name)s
 
 \paper {
   #(define dump-extents #t)
+  %(font_dump_setting)s
   %(paper_string)s
 }
 
@@ -731,35 +754,12 @@ def compose_ly (code, options, type):
        preamble_string = \
          string.join (compose_dict[PREAMBLE], '\n  ') % override
 
+        font_dump_setting = ''
+       if FONTLOAD in options:
+               font_dump_setting = '#(define-public force-eps-font-include #t)\n'
+                                 
        return (PREAMBLE_LY + body) % vars ()
 
-# BARF
-# Use lilypond for latex (.lytex) books,
-# and lilypond --preview for html, texinfo books?
-def to_eps (file):
-       cmd = r'latex "\nonstopmode \input %s"' % file
-       # Ugh.  (La)TeX writes progress and error messages on stdout.
-       # Redirect to stderr.
-       cmd = '(( %s >&2 ) >&- )' % cmd
-       ly.system (cmd)
-       ly.system (
-         'dvips -Ppdf -u+ec-mftrace.map -u+lilypond.map -E -o %s.eps %s' \
-           % (file, file))
-
-       # Check if it really is EPS.
-       # Otherwise music glyphs disappear from 2nd and following pages.
-
-       # TODO: Should run dvips -pp -E per page, then we get proper
-       #       cropping as well.
-
-       f = open ('%s.eps' % file)
-       for x in range (0, 10):
-               if re.search ('^%%Pages: ', f.readline ()):
-                       # Make non-EPS.
-                       ly.system (
-                         'dvips -Ppdf -u+ec-mftrace.map -u+lilypond.map -o %s.eps %s' \
-                           % (file, file))
-                       break
 
 def find_file (name):
        for i in include_path:
@@ -870,13 +870,13 @@ class Lilypond_snippet (Snippet):
                base = self.basename ()
 
                tex_file = '%s.tex' % base
+               eps_file = '%s.eps' % base
+               system_file = '%s-systems.tex' % base
                ly_file = '%s.ly' % base
                ok = os.path.exists (ly_file) \
-                    and os.path.exists (tex_file)\
-                    and os.stat (tex_file)[stat.ST_SIZE] \
-                    and open (tex_file).readlines ()[-1][1:-1] \
-                      == 'lilypondend'
-
+                    and os.path.exists (system_file)\
+                    and os.stat (system_file)[stat.ST_SIZE] \
+                    and re.match ('% eof', open (system_file).readlines ()[-1])
                if ok and (use_hash_p \
                           or self.ly () == open (ly_file).read ()):
                        # TODO: Do something smart with target formats
@@ -891,6 +891,14 @@ class Lilypond_snippet (Snippet):
                        ok = ok and (os.path.exists (base + '.png')
                                     or glob.glob (base + '-page*.png'))
                return not ok
+       def texstr_is_outdated (self):
+               if backend == 'ps':
+                       return 0
+
+               base = self.basename ()
+               ok = self.ly_is_outdated ()
+               ok = ok and (os.path.exists (base + '.texstr'))
+               return not ok
 
        def filter_text (self):
                code = self.substring ('code')
@@ -938,14 +946,17 @@ class Lilypond_snippet (Snippet):
                return str
 
        def output_info (self):
-               str = self.output_print_filename (HTML)
+               str = ''
                for image in self.get_images ():
                        (base, ext) = os.path.splitext (image)
 
                        # URG, makeinfo implicitly prepends dot to extension.
                        # Specifying no extension is most robust.
                        ext = ''
-                       str += output[TEXINFO][OUTPUT] % vars ()
+                       str += output[TEXINFO][OUTPUTIMAGE] % vars ()
+
+               base = self.basename ()
+               str += output[format][OUTPUT] % vars ()
                return str
 
        def output_latex (self):
@@ -968,6 +979,7 @@ class Lilypond_snippet (Snippet):
                        base = self.basename ()
                        filename = self.substring ('filename')
                        str = output[format][PRINTFILENAME] % vars ()
+
                return str
 
        def output_texinfo (self):
@@ -989,11 +1001,13 @@ class Lilypond_snippet (Snippet):
                        verb = verbatim_texinfo (self.substring ('code'))
                        str += (output[TEXINFO][VERBATIM] % vars ())
                        if not QUOTE in self.options:
-                               str = output[TEXINFO][NOQUOTE] % vars()
+                               str = output[TEXINFO][NOQUOTE] % vars ()
 
-               str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
-               str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
-               str += ('@html\n' + self.output_html () + '\n@end html\n')
+               str += self.output_info ()
+
+#              str += ('@ifinfo\n' + self.output_info () + '\n@end ifinfo\n')
+#              str += ('@tex\n' + self.output_latex () + '\n@end tex\n')
+#              str += ('@html\n' + self.output_html () + '\n@end html\n')
 
                if QUOTE in self.options:
                        str = output[TEXINFO][QUOTE] % vars ()
@@ -1124,31 +1138,34 @@ def is_derived_class (cl, baseclass):
                        return 1
        return 0
 
-def process_snippets (cmd, ly_snippets, png_snippets):
+def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
        ly_names = filter (lambda x: x,
                           map (Lilypond_snippet.basename, ly_snippets))
+       texstr_names = filter (lambda x: x,
+                          map (Lilypond_snippet.basename, texstr_snippets))
        png_names = filter (lambda x: x,
                            map (Lilypond_snippet.basename, png_snippets))
 
        status = 0
-       if ly_names:
-               status = ly.system (string.join ([cmd] + ly_names),
+       def my_system (cmd):
+               status = ly.system (cmd,
                                    ignore_error = 1, progress_p = 1)
 
-       if status:
-               ly.error ('Process %s exited unsuccessfully.' % cmd)
-               raise Compile_error
+               if status:
+                       ly.error ('Process %s exited unsuccessfully.' % cmd)
+                       raise Compile_error
+
+       # UGH
+       # the --process=CMD switch is a bad idea
+       # it is too generic for lilypond-book.
+       if texstr_names and re.search ('^[0-9A-Za-z/]*lilypond', cmd):
 
-       if format == HTML or format == TEXINFO:
-               for i in png_names:
-                       if not os.path.exists (i + '.eps') \
-                          and os.path.exists (i + '.tex'):
-                               to_eps (i)
-                               ly.make_ps_images (i + '.eps',
-                                                  resolution = 110)
+               my_system (string.join ([cmd + ' --backend texstr ' ] + texstr_names))
+               for l in texstr_names:
+                       my_system ('latex %s.texstr' % l)
 
-#                      elif os.path.exists (i + '.ps'):
-#                              ly.make_ps_images (i + '.ps', resolution = 110)
+       if ly_names:
+               my_system (string.join ([cmd] + ly_names))
 
 LATEX_DOCUMENT = r'''
 %(preamble)s
@@ -1213,6 +1230,11 @@ def do_process_cmd (chunks):
                                              Lilypond_snippet)
                            and x.ly_is_outdated (),
                  chunks)
+       texstr_outdated = \
+         filter (lambda x: is_derived_class (x.__class__,
+                                             Lilypond_snippet)
+                           and x.texstr_is_outdated (),
+                 chunks)
        png_outdated = \
          filter (lambda x: is_derived_class (x.__class__,
                                              Lilypond_snippet)
@@ -1224,25 +1246,27 @@ def do_process_cmd (chunks):
        ly.progress ('\n')
 
        if ly_outdated:
-               ly.progress (_ ("Processing...\n"))
-               process_snippets (process_cmd, ly_outdated, png_outdated)
+               ly.progress (_ ("Processing..."))
+               ly.progress ('\n')
+               process_snippets (process_cmd, ly_outdated, texstr_outdated, png_outdated)
        else:
                ly.progress (_ ("All snippets are up to date..."))
        ly.progress ('\n')
 
+def guess_format (input_filename):
+       format = None
+       e = os.path.splitext (input_filename)[1]
+       if e in ext2format.keys ():
+               # FIXME
+               format = ext2format[e]
+       else:
+               ly.error (_ ("cannot determine format for: %s" \
+                            % input_filename))
+               ly.exit (1)
+       return format
+
 def do_file (input_filename):
        # Ugh.
-       global format
-       if not format:
-               e = os.path.splitext (input_filename)[1]
-               if e in ext2format.keys ():
-                       # FIXME
-                       format = ext2format[e]
-               else:
-                       ly.error (_ ("cannot determine format for: %s" \
-                                    % input_filename))
-                       ly.exit (1)
-
        if not input_filename or input_filename == '-':
                in_handle = sys.stdin
                input_fullname = '<stdin>'
@@ -1386,6 +1410,8 @@ def do_options ():
                        format = a
                        if a == 'texi-html' or a == 'texi':
                                format = TEXINFO
+               elif o == '--tex-backend ':
+                       backend = 'tex'
                elif o == '--help' or o == '-h':
                        ly.help ()
                        sys.exit (0)
@@ -1412,9 +1438,19 @@ def do_options ():
 
 def main ():
        files = do_options ()
-       global process_cmd
+       if not files:
+               ly.warning ("Need to have command line option")
+               ly.exit (2)
+
+       file = files[0]
+       global process_cmd, format
+       format = guess_format (files[0])
+
+       formats = 'ps'
+       if format == TEXINFO:
+               formats += ',png' 
        if process_cmd == '':
-               process_cmd = lilypond_binary + ' -f tex'
+               process_cmd = lilypond_binary + ' --formats=%s --backend eps ' % formats
 
        if process_cmd:
                process_cmd += string.join ([(' -I %s' % p)
@@ -1422,13 +1458,12 @@ def main ():
 
        ly.identify (sys.stderr)
        ly.setup_environment ()
-       if files:
-               try:
-                       do_file (files[0])
-               except Compile_error:
-                       ly.exit (1)
+
+       try:
+               do_file (file)
+       except Compile_error:
+               ly.exit (1)
+
 
 if __name__ == '__main__':
        main ()
-
-# EOF