]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
patch::: 1.3.132.jcn1
[lilypond.git] / scripts / lilypond-book.py
index e6480c0e77163c7ab895dd999eadaa271a655b6a..e395ca7d48c87d21d06f8246748cb9467c6039b2 100644 (file)
@@ -212,8 +212,10 @@ class LatexPaper:
                if not self.m_use_geometry:
                        return latex_linewidths[self.m_papersize][self.m_fontsize]
                else:
-                       geo_opts = (a == None, b == None, c == None)
-                       
+                       geo_opts = (self.m_geo_lmargin == None,
+                                   self.m_geo_width == None,
+                                   self.m_geo_rmargin == None)
+
                        if geo_opts == (1, 1, 1):
                                if self.m_geo_textwidth:
                                        return self.m_geo_textwidth
@@ -326,8 +328,11 @@ output_dict= {
       %s
     }
   >
-\end{lilypond}""", 
-               'output-lilypond':r"""\begin[%s]{lilypond}
+\end{lilypond}""",
+               'output-filename' : r'''
+
+\verb+%s+:''',
+               'output-lilypond': r"""\begin[%s]{lilypond}
 %s
 \end{lilypond}""",
                'output-verbatim': "\\begin{verbatim}%s\\end{verbatim}",
@@ -342,6 +347,9 @@ output_dict= {
 %s
 @end lilypond 
 """,
+               'output-filename' : r'''
+
+@file{%s}:''',   
                  'output-lilypond-fragment': """@lilypond[%s]
 \context Staff\context Voice{ %s }
 @end lilypond """,
@@ -514,7 +522,8 @@ def compose_full_body (body, opts):
        optstring = string.join (opts, ' ')
        optstring = re.sub ('\n', ' ', optstring)
        body = r"""
-%% Generated by lilypond-book.py; options are %s  %%ughUGH not original options
+%% Generated automatically by: lilypond-book.py
+%% options are %s  %%ughUGH not original options
 \include "paper%d.ly"
 \paper  { linewidth = %f \pt; } 
 """ % (optstring, music_size, l) + body
@@ -630,9 +639,11 @@ def completize_preamble (chunks):
 read_files = []
 def find_file (name):
        """
-       Search the include path for NAME. If found, return the contents of teh file.
+       Search the include path for NAME. If found, return the (CONTENTS, PATH) of the file.
        """
+       
        f = None
+       nm = ''
        for a in include_path:
                try:
                        nm = os.path.join (a, name)
@@ -643,10 +654,10 @@ def find_file (name):
                        pass
        if f:
                sys.stderr.write ("Reading `%s'\n" % nm)
-               return f.read ()
+               return (f.read (), nm)
        else:
                error ("File not found `%s'\n" % name)
-               return ''
+               return ('', '')
 
 def do_ignore(match_object):
        return [('ignore', match_object.group('code'))]
@@ -677,12 +688,22 @@ def make_lilypond(m):
                        (options, m.group('code')))]
 
 def make_lilypond_file(m):
+       """
+
+       Find @lilypondfile{bla.ly} occurences and substitute bla.ly
+       into a @lilypond .. @end lilypond block.
+       
+       """
+       
        if m.group('options'):
                options = m.group('options')
        else:
                options = ''
+       (content, nm) = find_file(m.group('filename'))
+       options = "filename=%s," % nm + options
+
        return [('input', get_output('output-lilypond') %
-                       (options, find_file(m.group('filename'))))]
+                       (options, content))]
 
 def make_lilypond_block(m):
        if m.group('options'):
@@ -752,7 +773,7 @@ def determine_format (str):
 def read_doc_file (filename):
        """Read the input file, find verbatim chunks and do \input and \include
        """
-       str = find_file(filename)
+       (str, path) = find_file(filename)
        determine_format (str)
        
        chunks = [('input', str)]
@@ -784,7 +805,7 @@ def schedule_lilypond_block (chunk):
        (type, body, opts) = chunk
        assert type == 'lilypond'
        file_body = compose_full_body (body, opts)
-       basename = `abs(hash (file_body))`
+       basename = 'lily-' + `abs(hash (file_body))`
        for o in opts:
                m = re.search ('filename="(.*?)"', o)
                if m:
@@ -820,6 +841,15 @@ def schedule_lilypond_block (chunk):
        if 'png' in needed_filetypes and f(pathbase, '.eps', '.png'):
                todo.append('png')
        newbody = ''
+
+       if 'printfilename' in opts:
+               for o in opts:
+                       m= re.match ("filename=(.*)", o)
+                       if m:
+                               newbody = newbody + get_output ("output-filename") % m.group(1)
+                               break
+               
+       
        if 'verbatim' in opts:
                newbody = output_verbatim (body)
 
@@ -868,6 +898,7 @@ def system (cmd):
        return st
 
 def compile_all_files (chunks):
+       global foutn
        eps = []
        tex = []
        png = []
@@ -896,10 +927,25 @@ def compile_all_files (chunks):
                                x = os.path.join (g_here_dir, x)
                        return ' -I %s' % x
 
-               incs =  map (incl_opt, include_path)
+               incs = map (incl_opt, include_path)
                lilyopts = string.join (incs, ' ' )
+               if do_deps:
+                       lilyopts = lilyopts + ' --dependencies '
+                       if g_outdir:
+                               lilyopts = lilyopts + '--dep-prefix=' + g_outdir + '/'
                texfiles = string.join (tex, ' ')
                system ('lilypond --header=texidoc %s %s' % (lilyopts, texfiles))
+
+               #
+               # Ugh, fixing up dependencies for .tex generation
+               #
+               if do_deps:
+                       depfiles=map (lambda x: re.sub ('(.*)\.ly', '\\1.dep', x), tex)
+                       for i in depfiles:
+                               text=open (i).read ()
+                               text=re.sub ('\n([^:\n]*):', '\n' + foutn + ':', text)
+                               open (i, 'w').write (text)
+
        for e in eps:
                system(r"tex '\nonstopmode \input %s'" % e)
                system(r"dvips -E -o %s %s" % (e + '.eps', e))
@@ -907,8 +953,7 @@ def compile_all_files (chunks):
                cmd = r"""gs -sDEVICE=pgm  -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -q -sOutputFile=- -r90 -dNOPAUSE %s -c quit | pnmcrop | pnmtopng > %s"""
                cmd = cmd % (g + '.eps', g + '.png')
                system (cmd)
-       if g_outdir:
-               os.chdir(d)
+       os.chdir (d)
 
 
 def update_file (body, name):
@@ -1005,15 +1050,34 @@ Han-Wen Nienhuys <hanwen@cs.uu.nl>
        sys.exit (0)
 
 
-def write_deps (fn, target):
-       sys.stdout.write('writing `%s\'\n' % os.path.join(g_outdir, fn))
+def write_deps (fn, target, chunks):
+       global read_files
+       sys.stdout.write('Writing `%s\'\n' % os.path.join(g_outdir, fn))
        f = open (os.path.join(g_outdir, fn), 'w')
        f.write ('%s%s: ' % (g_dep_prefix, target))
-       for d in __main__.read_files:
+       for d in read_files:
                f.write ('%s ' %  d)
+       basenames=[]
+        for c in chunks:
+               if c[0] == 'lilypond':
+                       (type, body, opts, todo, basename) = c;
+                       basenames.append (basename)
+       for d in basenames:
+               if g_outdir:
+                       d=g_outdir + '/' + d
+               if g_dep_prefix:
+                       #if not os.isfile (d): # thinko?
+                       if not re.search ('/', d):
+                               d = g_dep_prefix + d
+               f.write ('%s.tex ' %  d)
+       f.write ('\n')
+       #if len (basenames):
+       #       for d in basenames:
+       #               f.write ('%s.ly ' %  d)
+       #       f.write (' : %s' % target)
        f.write ('\n')
        f.close ()
-       __main__.read_files = []
+       read_files = []
 
 def identify():
        sys.stdout.write ('lilypond-book (GNU LilyPond) %s\n' % program_version)
@@ -1033,7 +1097,7 @@ def check_texidoc (chunks):
                        (type, body, opts, todo, basename) = c;
                        pathbase = os.path.join (g_outdir, basename)
                        if os.path.isfile (pathbase + '.texidoc'):
-                               body = '\n@include %s.texidoc' % basename + body
+                               body = '\n@include %s.texidoc\n' % basename + body
                                c = (type, body, opts, todo, basename)
                n.append (c)
        return n
@@ -1049,7 +1113,9 @@ def fix_epswidth (chunks):
        return newchunks
 
 
+foutn=""
 def do_file(input_filename):
+       global foutn
        file_settings = {}
        if outname:
                my_outname = outname
@@ -1070,6 +1136,8 @@ def do_file(input_filename):
        scan_preamble(chunks)
        chunks = process_lilypond_blocks(my_outname, chunks)
 
+       foutn = os.path.join (g_outdir, my_outname + '.' + format)
+
        # Do It.
        if __main__.g_run_lilypond:
                compile_all_files (chunks)
@@ -1080,7 +1148,6 @@ def do_file(input_filename):
 
        x = 0
        chunks = completize_preamble (chunks)
-       foutn = os.path.join(g_outdir, my_outname + '.' + format)
        sys.stderr.write ("Writing `%s'\n" % foutn)
        fout = open (foutn, 'w')
        for c in chunks:
@@ -1089,7 +1156,7 @@ def do_file(input_filename):
        # should chmod -w
 
        if do_deps:
-               write_deps (my_depname, foutn)
+               write_deps (my_depname, foutn, chunks)
 
 
 outname = ''