]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/mudela-book.py
patch::: 1.2.8.jcn2
[lilypond.git] / scripts / mudela-book.py
index 8021e5181800d88377bbddee58db4a980e64d417..bed9d1c12e7b99a3ba7aaa7f01666c981903d334 100644 (file)
@@ -57,6 +57,7 @@ options = [
 
 format = ''
 run_lilypond = 1
+use_hash = 1
 no_match = 'a\ba'
 
 # format specific strings, ie. regex-es for input, and % strings for output
@@ -69,7 +70,7 @@ output_dict= {
     }
   >
 \end{mudela}""", 
-               'output-mudela':r"""\begin%s{mudela}
+               'output-mudela':r"""\begin[%s]{mudela}
 %s
 \end{mudela}""",
                'output-verbatim': r"""\begin{verbatim}%s\end{verbatim}""",
@@ -135,7 +136,7 @@ re_dict = {
                 'mudela-file': '@mudelafile(\[[^\\]]+\])?{([^}]+)}',
                 'mudela' : '@mudela(\[.*?\])?{(.*?)}',
                 'mudela-block': r"""(?s)@mudela(\[.*?\])?(.*?)@end mudela""",
-                'interesting-cs': r"""[\\@](node|mudelagraphic)""",
+                'interesting-cs': r"""[\\@](chapter|section)""",
                  'comma-sep' : ', *',           
                 }
        }
@@ -223,27 +224,32 @@ def compose_full_body (body, opts):
 
        # urg: breaks on \include of full score
        # Use nofly option if you want to \include full score.
-       if not 'nofly' in opts and not re.search ('\\\\score', body):
-               opts.append ('fly')
+       if 'nofly'  not in opts and not re.search ('\\\\score', body):
+               opts.append ('fragment')
 
-       if  'fragment' in opts or 'singleline' in opts:
+       if 'fragment' in opts and 'nosingleline' not in opts:
+               opts.append ('singleline')
+
+       if 'singleline' in opts:
                l = -1.0;
        else:
                l = latex_linewidths[cols][paper][latex_size]
 
 
-       if 'fly' in opts:
+       if 'relative' in opts:
+               body = '\\relative c { %s }' % body
+
+       
+       if 'fragment' in opts:
                body = r"""\score { 
-  \notes\relative c {
-    %s
-  }
+  \notes { %s }
   \paper { }  
 }""" % body
 
         opts = uniq (opts)
         optstring = string.join (opts, ' ')
        optstring = re.sub ('\n', ' ', optstring)
-               
+       
        body = r"""
 %% Generated by mudela-book.py; options are %s
 \include "paper%d.ly"
@@ -420,7 +426,8 @@ def find_mudela_shorthands (b):
                str = f.read (-1)
                opts = match.group (1)
                if opts:
-                       opts = re.split (',[ \n\t]*', opts[1:-1])
+                       opts = opts[1:-1]
+                       opts = re.split (',[ \n\t]*', opts)
                else:
                        opts = []
 
@@ -432,8 +439,6 @@ def find_mudela_shorthands (b):
                        opts .append ('nofly')
                        
                str_opts = string.join (opts, ',')
-               if str_opts: str_opts = '[' + str_opts + ']'
-
 
                str = ("%% copied from file `%s'\n" % full_path) + str 
                return get_output ('output-mudela') % (str_opts, str)
@@ -444,7 +449,7 @@ def find_mudela_shorthands (b):
        
 def find_mudela_chunks (str):
        """Find mudela blocks, while watching for verbatim. Returns
-       (STR,MUDS) with \mudelagraphic substituted for the blocks in STR,
+       (STR,MUDS) with  substituted for the blocks in STR,
        and the blocks themselves MUDS"""
   
        chunks = []
@@ -499,7 +504,7 @@ def advance_counters (counter, opts, str):
                                pass
                elif g == 'chapter':
                        (chapter, section, count)  = (chapter + 1, 0, 0)
-               elif g == 'section' or g == 'node':
+               elif g == 'section':
                        (section, count)  = (section + 1, 0)
                        
 
@@ -512,7 +517,7 @@ def schedule_mudela_block (base, chunk, extra_opts):
        for the main file).  The .ly is written, and scheduled in
        TODO.
 
-       Return: a chunk (TYPE_STR, MAIN_STR, OPTIONS, TODO)
+       Return: a chunk (TYPE_STR, MAIN_STR, OPTIONS, TODO, BASE)
 
        TODO has format [basename, extension, extension, ... ]
        
@@ -527,10 +532,13 @@ def schedule_mudela_block (base, chunk, extra_opts):
                newbody = output_verbatim (body)
 
        file_body = compose_full_body (body, opts)
-       updated = update_file (file_body, base + '.ly')
-       todo = [base]                   # UGH.
+       basename = base
+       if __main__.use_hash:
+               basename = `hash (file_body)`
+       updated = update_file (file_body, basename + '.ly')
+       todo = [basename]                       # UGH.
 
-       if not os.path.isfile (base + '.tex') or updated:
+       if not os.path.isfile (basename + '.tex') or updated:
                todo.append ('tex')
                updated = 1
 
@@ -546,25 +554,24 @@ def schedule_mudela_block (base, chunk, extra_opts):
                opts.append ('eps')
 
        if 'eps' in opts and ('tex' in todo or
-                             not os.path.isfile (base + '.eps')):
+                             not os.path.isfile (basename + '.eps')):
                todo.append ('eps')
 
        if 'png' in opts and ('eps' in todo or
-                             not os.path.isfile (base + '.png')):
+                             not os.path.isfile (basename + '.png')):
                todo.append ('png')
 
        if format == 'latex':
                if 'eps' in opts :
-                       newbody = newbody + get_output ('output-eps') %  (base, base)
+                       newbody = newbody + get_output ('output-eps') %  (basename, basename)
                else:
-                       newbody = newbody + get_output ('output-tex') % base
+                       newbody = newbody + get_output ('output-tex') % basename
 
        elif format == 'texi':
-               newbody = newbody + get_output ('output-all') % (base, base) 
+               newbody = newbody + get_output ('output-all') % (basename, basename) 
 
+       return ('mudela', newbody, opts, todo, base)
 
-       
-       return ('mudela', newbody, opts, todo)
 
 def find_eps_dims (match):
        "Fill in dimensions of EPS files."
@@ -650,6 +657,7 @@ def compile_all_files (chunks):
        eps = []
        tex = []
        png = []
+       hash_dict = {}
 
        for c in chunks:
                if c[0] <> 'mudela':
@@ -664,6 +672,9 @@ def compile_all_files (chunks):
                        elif e == 'png':
                                png.append (base)
 
+               if __main__.use_hash:
+                       hash_dict[c[4]] = c[3][0]
+
        if tex:
                lilyopts = map (lambda x:  '-I ' + x, include_path)
                lilyopts = string.join (lilyopts, ' ' )
@@ -681,6 +692,22 @@ def compile_all_files (chunks):
                cmd = cmd % (g + '.eps', g + '.png')
                system (cmd)
 
+       if __main__.use_hash:
+               name = ''
+               last_name = ''
+               f = 0
+               ks = hash_dict.keys ()
+               ks.sort ()
+               for i in ks:
+                       name = re.sub ("(.*)-[0-9]+\.[0-9]+\.[0-9]+", "\\1", i)
+                       name = name + '.mix'
+                       if name != last_name:
+                               if last_name:
+                                       f.close ()
+                               f = open (name, 'w')
+                               last_name = name
+                       f.write ("%s:%s\n" % (i, hash_dict[i]))
+
        
 def update_file (body, name):
        same = 0