]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/ly2dvi.py
fix spelling for acciaccatura.
[lilypond.git] / scripts / ly2dvi.py
index 8c418aa6c84bd17a3a263f05807b5415a0a45849..1c5c6208ebd2888fe4055f8eeb4f24d4794ea9d1 100644 (file)
@@ -6,7 +6,7 @@
 # 
 # source file of the GNU LilyPond music typesetter
 # 
-# (c) 1998--2002  Han-Wen Nienhuys <hanwen@cs.uu.nl>
+# (c)  1998--2003  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 #                 Jan Nieuwenhuizen <janneke@gnu.org>
 
 # This is the third incarnation of ly2dvi.
@@ -39,7 +39,6 @@ TODO:
 
   * for foo.ly, rename ly2dvi.dir to out-ly2dvi, foo.ly2dvi, foo.dir ?
      
-     
   * move versatile taglines, 
   
      \header {
@@ -74,6 +73,7 @@ import operator
 import stat
 import string
 import traceback
+import glob
 
 ################################################################
 # Users of python modules should include this snippet
@@ -95,6 +95,7 @@ if os.environ.has_key ('LILYPONDPREFIX') :
        while datadir[-1] == os.sep:
                datadir= datadir[:-1]
 
+
 sys.path.insert (0, os.path.join (datadir, 'python'))
 
 # Customize these
@@ -106,6 +107,7 @@ global re;re = ly.re
 
 # lilylib globals
 program_name = 'ly2dvi'
+program_version = '@TOPLEVEL_VERSION@'
 verbose_p = 0
 pseudo_filter_p = 0
 original_dir = os.getcwd ()
@@ -118,43 +120,50 @@ debug_p = 0
 ## ly2dvi: silly name?
 ## do -P or -p by default?
 ##help_summary = _ ("Run LilyPond using LaTeX for titling")
-help_summary = _ ("Run LilyPond, add titles, generate printable document")
+help_summary = _ ("Run LilyPond, add titles, generate printable document.")
 copyright = ('Han-Wen Nienhuys <hanwen@cs.uu.nl',
             'Jan Nieuwenhuizen <janneke@gnu.org')
 
 option_definitions = [
        ('', 'd', 'dependencies',
         _ ("write Makefile dependencies for every input file")),
-       ('', 'h', 'help', _ ("this help")),
+       ('', 'h', 'help', _ ("print this help")),
        ('', '', 'debug', _ ("print even more output")),
        (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
        ('', 'k', 'keep',
         _ ("keep all output, output to directory %s.dir") % program_name),
        ('', '', 'no-lily', _ ("don't run LilyPond")),
        ('', 'm', 'no-paper', _ ("produce MIDI output only")),
-       (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
+       (_ ("FILE"), 'o', 'output', _ ("write output to FILE")),
        (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
        (_ ('RES'), '', 'preview-resolution',
         _ ("set the resolution of the preview to RES")),
        ('', 'P', 'postscript', _ ("generate PostScript output")),
-       ('', 'p', 'pdf', _ ("generate PDF output")),    
+       ('', '', 'png', _("generate PNG page images")),
+       ('', '', 'psgz', _("generate PS.GZ")), 
+       ('', 'p', 'pdf', _ ("generate PDF output")),
        ('', '', 'pdftex', _ ("use pdflatex to generate a PDF output")),
        # FIXME: preview, picture; to indicate creation of a PNG?
        ('', '', 'preview', _ ("make a picture of the first system")),
+       ('','', 'html', _("make HTML file with links to all output")),
        (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
-       ('', 'V', 'verbose', _ ("verbose")),
+       ('', 'V', 'verbose', _ ("be verbose")),
        ('', 'v', 'version', _ ("print version number")),
        ('', 'w', 'warranty', _ ("show warranty and copyright")),
        ]
 
 # other globals
 preview_p = 0
+page_images_p = 0
 lilypond_error_p = 0
+html_p = 0
 
 # Pdftex support
 pdftex_p = 0
 latex_cmd = 'latex'
-tex_extension = '.tex'
+
+
+tex_extension = '.tex'  ## yuk.
 
 #lilypond_binary = 'valgrind --suppressions=%(home)s/usr/src/guile-1.6.supp --num-callers=10 %(home)s/usr/src/lilypond/lily/out/lilypond '% { 'home' : '/home/hanwen' }
 
@@ -177,7 +186,10 @@ extra_init = {
        'language' : [],
        'latexheaders' : [],
        'latexpackages' :  ['geometry'],
-       'latexoptions' : [],
+
+       # for geometry v3
+       'latexoptions' : ['compat2'],
+       
        'papersize' : [],
        'pagenumber' : [1],
        'textheight' : [], 
@@ -252,7 +264,7 @@ def run_lilypond (files, dep_prefix):
 
        if debug_p:
                ly.print_environment ()
-
+               
        cmd = string.join ((lilypond_binary, opts, fs))
        status = ly.system (cmd, ignore_error = 1, progress_p = 1)
        signal = 0x0f & status
@@ -282,7 +294,7 @@ def analyse_lilypond_output (filename, extra):
        # urg
        '''Grep FILENAME for interesting stuff, and
        put relevant info into EXTRA.'''
-       filename = filename+tex_extension
+       filename = filename + tex_extension
        ly.progress (_ ("Analyzing %s...") % filename)
        s = open (filename).read ()
 
@@ -295,24 +307,25 @@ def analyse_lilypond_output (filename, extra):
        ly.progress ('\n')
 
 def find_tex_files_for_base (base, extra):
-
        '''
        Find the \header fields dumped from BASE.
        '''
        
        headerfiles = {}
        for f in layout_fields:
-               if os.path.exists (base + '.' + f):
-                       headerfiles[f] = base+'.'+f
+               fn = base + '.' + f
+               if os.path.exists (fn):
+                       headerfiles[f] = fn
 
        if os.path.exists (base  +'.dep'):
                dependency_files.append (base + '.dep')
 
        for f in extra_fields:
-               if os.path.exists (base + '.' + f):
-                       extra[f].append (open (base + '.' + f).read ())
+               fn =base + '.' + f
+               if os.path.exists (fn):
+                       extra[f].append (open (fn).read ())
        
-       return (base+tex_extension,headerfiles)
+       return (base + tex_extension, headerfiles)
         
 
 def find_tex_files (files, extra):
@@ -479,12 +492,13 @@ None
        f.close ()
 
        cmd = latex_cmd + ' \\\\nonstopmode \\\\input %s' % latex_fn
+       
        # Ugh.  (La)TeX writes progress and error messages on stdout
        # Redirect to stderr
-       cmd += ' 1>/dev/stderr'
+       cmd = '(( %s  >&2 ) >&- )' % cmd
        status = ly.system (cmd, ignore_error = 1)
        signal = 0xf & status
-       exit_status = status >> 8
+       exit_status = status >> 8
 
        if exit_status:
 
@@ -503,21 +517,25 @@ None
                ly.exit (1)
        
        if preview_p:
-               # make a preview by rendering only the 1st line.
-               preview_fn = outbase + '.preview.tex'
-               f = open (preview_fn, 'w')
-               wfs = find_tex_files (files, extra)
-               s = global_latex_definition (wfs, extra)
-
-               s = re.sub ('thispagestyle{firstpage}', r'''thispagestyle{empty}%
-\\def\\interscoreline{\\endinput}''',s ) 
-               s = re.sub ('thispagestyle{lastpage}', r'''thispagestyle{empty}%
-\\def\\interscoreline{\\endinput}''',s ) 
-               f.write (s)
-               f.close()
-               cmd = '%s \\\\nonstopmode \\\\input %s' % (latex_cmd, preview_fn)
-               ly.system (cmd)
-       
+               # make a preview by rendering only the 1st line
+               # of each score
+               for score in find_tex_files (files, extra):
+                       preview_base = ly.strip_extension (score[0], '.tex')
+                       preview_fn = preview_base + '.preview.tex'
+                       s = global_latex_definition ((score,), extra)
+                       s = re.sub ('thispagestyle{firstpage}',
+                                   r'''thispagestyle{empty}%
+                                   \\def\\interscoreline{\\endinput}''', s)
+                       s = re.sub ('thispagestyle{lastpage}',
+                                   r'''thispagestyle{empty}%
+                                   \\def\\interscoreline{\\endinput}''', s)
+                       f = open (preview_fn, 'w')
+                       f.write (s)
+                       f.close ()
+                       cmd = '%s \\\\nonstopmode \\\\input %s' \
+                             % (latex_cmd, preview_fn)
+                       ly.system (cmd)
+
 
 def run_dvips (outbase, extra):
 
@@ -548,7 +566,7 @@ None.
                                pass
 
                if pfa_file:
-                       opts = opts + ' -Ppdf -G0 -u lilypond.map'
+                       opts = opts + ' -Ppdf -G0 -u +lilypond.map'
                else:
                        ly.warning (_ ('''Trying create PDF, but no PFA fonts found.
 Using bitmap fonts instead. This will look bad.'''))
@@ -557,8 +575,12 @@ Using bitmap fonts instead. This will look bad.'''))
        ly.system (cmd)
 
        if preview_p:
-               cmd = 'dvips -E -o%s %s' % ( outbase + '.preview.ps', outbase + '.preview.dvi')         
-               ly.system (cmd)
+               for score in find_tex_files (files, extra):
+                       preview_base = ly.strip_extension (score[0], '.tex')
+                       cmd = 'dvips -E -o%s %s' \
+                             % (preview_base + '.preview.ps',
+                                preview_base + '.preview.dvi')
+                       ly.system (cmd)
 
        if 'PDF' in targets:
                cmd = 'ps2pdf %s.ps %s.pdf' % (outbase , outbase)
@@ -604,7 +626,48 @@ def find_pfa_fonts (name):
                m = re.match ('.*?/(feta[-a-z0-9]+) +findfont', s[here:], re.DOTALL)
        return pfa
 
+
+def make_html_menu_file (html_file, files_found):
+       exts = {
+               'pdf' : "Print (PDF, %s)",
+               'ps.gz' : "Print (gzipped PostScript, %s)",
+               'png' : "View (PNG, %s)",
+               'midi' : "Listen (MIDI, %s)",
+               'ly' : "View source code (%s)", 
+               }
+       html_str = ''
+
+       pages = filter (lambda x : re.search ('page[0-9]+.png',  x),
+                       files_found)
+       rest =  filter (lambda x : not re.search ('page[0-9]+.png',  x),
+                       files_found)
+
+       preview = filter (lambda x: re.search ('.png$', x), rest)
+       if preview:
+               html_str = '<img src="%s">' % preview[0]
+
+       for p in pages:
+               page = re.sub ('.*page([0-9])+.*', 'View page \\1 (PNG picture, %s)\n', p)
+               page = page % 'unknown size'
+               
+               html_str += '<li><a href="%s">%s</a>' % (p, page)
+               
+               
+       for e in ['pdf', 'ps.gz', 'midi', 'ly']:
+               fs = filter (lambda x: re.search ('.%s$' % e, x), rest)
+               for f in fs:
+                       entry = exts[e] % 'unknown size' # todo
+                       html_str += '<li><a href="%s">%s</a>\n\n' % (f, entry)
+
+       html_str += "\n\n</li>"
+       ly.progress (_("Writing HTML menu `%s'") % html_file)
+       ly.progress ('\n')
+       open (html_file, 'w').write (html_str)
        
+################################################################
+## MAIN
+################################################################
+
 (sh, long) = ly.getopt_args (option_definitions)
 try:
        (options, files) = getopt.getopt (sys.argv[1:], sh, long)
@@ -647,7 +710,8 @@ for opt in options:
                lily_p = 0
        elif o == '--preview':
                preview_p = 1
-               targets.append ('PNG')
+               if 'PNG' not in targets:
+                       targets.append ('PNG')
        elif o == '--preview-resolution':
                preview_resolution = string.atoi (a)
        elif o == '--no-paper' or o == '-m':
@@ -676,6 +740,14 @@ for opt in options:
                if status:
                        ly.warranty ()
                sys.exit (0)
+       elif o == '--html':
+               html_p = 1
+       elif o == '--png':
+               page_images_p = 1
+               if 'PNG' not in targets:
+                       targets.append ('PNG')
+       elif o == '--psgz':
+               targets.append ('PS.GZ')
        else:
                unimplemented_option () # signal programming error
 
@@ -710,6 +782,8 @@ if not files:
 
 if 1:
        ly.identify (sys.stderr)
+       ly.lilypond_version_check (lilypond_binary, '@TOPLEVEL_VERSION@')
+       
        original_output = output_name
        
        # Ugh, maybe make a setup () function
@@ -779,6 +853,9 @@ if 1:
        # have subsequent stages and use 'lelie' output.
        if pseudo_filter_p:
                files[0] = 'lelie'
+
+       if 'PS.GZ'  in targets:
+               targets.append ('PS')
                
        if 'PNG' in targets and 'PS' not in targets:
                targets.append ('PS')
@@ -813,8 +890,12 @@ if 1:
                        else:
                                ly.warning (_("Failed to make PS file. Rerun with --verbose for a trace."))
 
-       if 'PNG' in  targets:
-               ly.make_preview (outbase)
+       if preview_p:
+               for score in find_tex_files (files, extra_init):
+                       preview_base = ly.strip_extension (score[0], '.tex')
+                       ly.make_ps_images (preview_base + '.preview.ps',
+                                          resolution=preview_resolution
+                                          )
 
        if 'PDFTEX' in targets:
                try:
@@ -837,8 +918,12 @@ if 1:
                        if verbose_p:
                                traceback.print_exc ()
                        else:
-                               ly.warning (_("Running LaTeX falied. Rerun with --verbose for a trace."))
+                               ly.warning (_("Running LaTeX failed. Rerun with --verbose for a trace."))
                                
+       if page_images_p:
+               ly.make_ps_images (outbase + '.ps' ,
+                                  resolution = preview_resolution
+                                  )
 
        # add DEP to targets?
        if track_dependencies_p:
@@ -846,7 +931,7 @@ if 1:
                generate_dependency_file (depfile, depfile)
                if os.path.isfile (depfile):
                        ly.progress (_ ("dependencies output to `%s'...") %
-                                 depfile)
+                                    depfile)
                        ly.progress ('\n')
 
        if pseudo_filter_p:
@@ -865,20 +950,34 @@ if 1:
                targets = []
                ly.progress ('\n')
                
+       if 'PS.GZ' in targets:
+               ly.system ("gzip *.ps") 
+               targets.remove ('PS')
+
        # Hmm, if this were a function, we could call it the except: clauses
+       files_found = []
        for i in targets:
                ext = string.lower (i)
+
+               pattern = '%s.%s' % (outbase, ext)
+               if i == 'PNG':
+                       pattern  = '*.png' 
+               ls = glob.glob (pattern)
+               files_found += ls 
                ly.cp_to_dir ('.*\.%s$' % ext, outdir)
-               outname = outbase + '.' + string.lower (i)
-               abs = os.path.join (outdir, outname)
-               if reldir != '.':
-                       outname = os.path.join (reldir, outname)
-               if os.path.isfile (abs):
-                       ly.progress (_ ("%s output to `%s'...") % (i, outname))
+
+
+               if ls:
+                       names = string.join (map (lambda x: "`%s'" % x, ls))
+                       ly.progress (_ ("%s output to %s...") % (i, names))
                        ly.progress ('\n')
                elif verbose_p:
-                       ly.warning (_ ("can't find file: `%s'") % outname)
-                       
+                       ly.warning (_ ("can't find file: `%s.%s'") % (outbase, ext))
+
+       if html_p:
+               make_html_menu_file (os.path.join (outdir, outbase + ".html"),
+                                    files_found)
+
        os.chdir (original_dir)
        ly.cleanup_temp ()