]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/lilypond-book.py
release: 1.5.18
[lilypond.git] / scripts / lilypond-book.py
index e15ee356ddaff5485255bcb2291298bdc053c8e0..fd5386915babe6e225f9a6ba3e524e45c487bac7 100644 (file)
@@ -1,8 +1,9 @@
 #!@PYTHON@
 # vim: set noexpandtab:
 # TODO:
-# * Figure out clean set of options. Hmm, isn't it pretty ok now?
-# * add support for .lilyrc
+# * junk --outdir for --output 
+# * Figure out clean set of options.
+# * 
 # * EndLilyPondOutput is def'd as vfil. Causes large white gaps.
 # * texinfo: add support for @pagesize
 
@@ -44,6 +45,30 @@ program_version = '@TOPLEVEL_VERSION@'
 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
        program_version = '1.4pre'
 
+#
+# Try to cater for bad installations of LilyPond, that have
+# broken TeX setup.  Just hope this doesn't hurt good TeX
+# setups.  Maybe we should check if kpsewhich can find
+# feta16.{afm,mf,tex,tfm}, and only set env upon failure.
+#
+datadir = '@datadir@'
+environment = {
+       'MFINPUTS' : datadir + '/mf:',
+       'TEXINPUTS': datadir + '/tex:' + datadir + '/ps:.:',
+       'TFMFONTS' : datadir + '/tfm:',
+       'GS_FONTPATH' : datadir + '/afm:' + datadir + '/pfa',
+       'GS_LIB' : datadir + '/ps',
+}
+
+def setup_environment ():
+       for key in environment.keys ():
+               val = environment[key]
+               if os.environ.has_key (key):
+                       val = val + os.pathsep + os.environ[key]
+               os.environ[key] = val
+
+
+
 include_path = [os.getcwd()]
 
 
@@ -163,11 +188,13 @@ class LatexPaper:
                elif name[-5:] == 'paper':
                        self.m_papersize = name
                else:
-                       self._set_dimen('m_geo_'+name, value)
+                               pass 
+                       # what is _set_dimen ?? /MB
+                               #self._set_dimen('m_geo_'+name, value)
        def __setattr__(self, name, value):
                if type(value) == type("") and \
                   dimension_conversion_dict.has_key (value[-2:]):
-                       f = dimension_conversion_dict[dim]
+                       f = dimension_conversion_dict[value[-2:]]
                        self.__dict__[name] = f(float(value[:-2]))
                else:
                        self.__dict__[name] = value
@@ -340,7 +367,7 @@ output_dict= {
                'output-default-pre': "\\def\preLilypondExample{}\n",
                'usepackage-graphics': '\\usepackage{graphics}\n',
                'output-eps': '\\noindent\\parbox{\\lilypondepswidth{%(fn)s.eps}}{\includegraphics{%(fn)s.eps}}',
-               'output-tex': '\\preLilypondExample \\input %(fn)s.tex \\postLilypondExample\n',
+               'output-tex': '{\\preLilypondExample \\input %(fn)s.tex \\postLilypondExample\n}',
                'pagebreak': r'\pagebreak',
                },
        'texi' : {'output-lilypond': """@lilypond[%s]
@@ -376,7 +403,9 @@ output_dict= {
 @end tex
 @html
 <p>
-<img src=%(fn)s.png>
+<a href="%(fn)s.png">
+<img border=0 src="%(fn)s.png" alt="[picture of music]">
+</a>
 @end html
 """,
                }
@@ -391,15 +420,15 @@ def output_verbatim (body):
 re_dict = {
        'latex': {'input': r'(?m)^[^%\n]*?(?P<match>\\mbinput{?([^}\t \n}]*))',
                  'include': r'(?m)^[^%\n]*?(?P<match>\\mbinclude{(?P<filename>[^}]+)})',
-                 'option-sep' : ', *',
+                 'option-sep' : ',\s*',
                  'header': r"\\documentclass\s*(\[.*?\])?",
                  'geometry': r"^(?m)[^%\n]*?\\usepackage\s*(\[(?P<options>.*)\])?\s*{geometry}",
                  'preamble-end': r'(?P<code>\\begin{document})',
                  'verbatim': r"(?s)(?P<code>\\begin{verbatim}.*?\\end{verbatim})",
                  'verb': r"(?P<code>\\verb(?P<del>.).*?(?P=del))",
-                 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile(\[(?P<options>.*?)\])?\{(?P<filename>.+)})',
-                 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond(\[(?P<options>.*?)\])?{(?P<code>.*?)})',
-                 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin(\[(?P<options>.*?)\])?{lilypond}(?P<code>.*?)\\end{lilypond})",
+                 'lilypond-file': r'(?m)^[^%\n]*?(?P<match>\\lilypondfile\s*(\[(?P<options>.*?)\])?\s*\{(?P<filename>.+)})',
+                 'lilypond' : r'(?m)^[^%\n]*?(?P<match>\\lilypond\s*(\[(?P<options>.*?)\])?\s*{(?P<code>.*?)})',
+                 'lilypond-block': r"(?sm)^[^%\n]*?(?P<match>\\begin\s*(\[(?P<options>.*?)\])?\s*{lilypond}(?P<code>.*?)\\end{lilypond})",
                  'def-post-re': r"\\def\\postLilypondExample",
                  'def-pre-re': r"\\def\\preLilypondExample",             
                  'usepackage-graphics': r"\usepackage{graphics}",
@@ -422,7 +451,7 @@ re_dict = {
                 'lilypond-file': '(?m)^(?!@c)(?P<match>@lilypondfile(\[(?P<options>.*?)\])?{(?P<filename>[^}]+)})',
                 'lilypond' : '(?m)^(?!@c)(?P<match>@lilypond(\[(?P<options>.*?)\])?{(?P<code>.*?)})',
                 'lilypond-block': r"""(?m)^(?!@c)(?P<match>(?s)(?P<match>@lilypond(\[(?P<options>.*?)\])?\s(?P<code>.*?)@end lilypond\s))""",
-                 'option-sep' : ', *',
+                 'option-sep' : ',\s*',
                  'intertext': r',?\s*intertext=\".*?\"',
                  'multiline-comment': r"(?sm)^\s*(?!@c\s+)(?P<code>@ignore\s.*?@end ignore)\s",
                  'singleline-comment': r"(?m)^.*?(?P<match>(?P<code>@c.*$\n+))",
@@ -470,7 +499,6 @@ def bounding_box_dimensions(fname):
        else:
                return (0,0)
 
-
 def error (str):
        sys.stderr.write (str + "\n  Exiting ... \n\n")
        raise 'Exiting.'
@@ -508,9 +536,24 @@ def compose_full_body (body, opts):
                l = -1.0;
        else:
                l = __main__.paperguru.get_linewidth()
-       
-       if 'relative' in opts:#ugh only when is_fragment
-               body = '\\relative c { %s }' % body
+
+       for o in opts:
+               m= re.search ('relative(.*)', o)
+               v = 0
+               if m:
+                       try:
+                               v = string.atoi (m.group (1))
+                       except ValueError:
+                               pass
+
+                       v = v + 1
+                       pitch = 'c'
+                       if v < 0:
+                               pitch = pitch + '\,' * v
+                       elif v > 0:
+                               pitch = pitch + '\'' * v
+
+                       body = '\\relative %s { %s }' %(pitch, body)
        
        if is_fragment:
                body = r"""\score { 
@@ -523,10 +566,12 @@ def compose_full_body (body, opts):
        optstring = re.sub ('\n', ' ', optstring)
        body = r"""
 %% Generated automatically by: lilypond-book.py
-%% options are %s  %%ughUGH not original options
+%% options are %s  
 \include "paper%d.ly"
-\paper  { linewidth = %f \pt; } 
+\paper  { linewidth = %f \pt } 
 """ % (optstring, music_size, l) + body
+
+       # ughUGH not original options
        return body
 
 def parse_options_string(s):
@@ -945,9 +990,13 @@ def compile_all_files (chunks):
                if do_deps:
                        depfiles=map (lambda x: re.sub ('(.*)\.ly', '\\1.dep', x), tex)
                        for i in depfiles:
-                               text=open (i).read ()
+                               f =open (i)
+                               text=f.read ()
+                               f.close ()
                                text=re.sub ('\n([^:\n]*):', '\n' + foutn + ':', text)
-                               open (i, 'w').write (text)
+                               f = open (i, 'w')
+                               f.write (text)
+                               f.close ()
 
        for e in eps:
                system(r"tex '\nonstopmode \input %s'" % e)
@@ -955,7 +1004,12 @@ def compile_all_files (chunks):
        for g in png:
                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)
+               try:
+                       status = system (cmd)
+               except:
+                       os.unlink (g + '.png')
+                       error ("Removing output file")
+               
        os.chdir (d)
 
 
@@ -1219,6 +1273,7 @@ if g_outdir:
                error ("outdir is a file: %s" % g_outdir)
        if not os.path.exists(g_outdir):
                os.mkdir(g_outdir)
+setup_environment ()
 for input_filename in files:
        do_file(input_filename)