]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/mudela-book.py
release: 1.1.55
[lilypond.git] / scripts / mudela-book.py
index 52d2ff849179c2f4581ab60a560cd052ef630eae..eafc978c0e0eee1a1d639555414d39a7adf6659d 100644 (file)
@@ -6,7 +6,6 @@
 #  bug-gnu-music@gnu.org
 #  help-gnu-music@gnu.org
 #
-# All non-english comments are NOT in swedish, they are norwegian!
 #  TODO:
 # * center option (??)
 # * make mudela-book understand usepackage{geometry}
 #   - bf: \mudela{ \times 2/3{...} }
 #        * \t in \times is not tab character and
 #        * dont treat the first '}' as command ending
-
+# 0.5.4: (Mats B)
+#   - .fly and .sly files in \mudelafile{} are treated as standalone Lilypond.
+#   - Fragments, .fly and .sly files are in \relative mode.
+# 0.5.5: (Mats B)
+#   - bf: Default fragments have linewidth=-1.0
+#   - Added 'singleline' and 'multiline' options.
+# 0.5.6:
+#   - \mudelafile{} set linewidth correct, -1 for .sly and texlinewidth for .fly
+#   - changes to Mudela_output
+#   - changed RE to search for pre/postMudelaExample to make it possible to
+#     comment out a definition.
+#   - use sys.stderr and sys.stdout instead of print
 import os
 import string
 import re
@@ -66,7 +76,8 @@ import sys
 
 outdir = 'out'
 initfile = ''
-program_version = '0.5.3'
+program_version = '0.5.6'
+include_path = ['.']
 
 out_files = []
 
@@ -74,6 +85,8 @@ fontsize_i2a = {11:'eleven', 13:'thirteen', 16:'sixteen',
                 20:'twenty', 26:'twentysix'}
 fontsize_pt2i = {'11pt':11, '13pt':13, '16pt':16, '20pt':20, '26pt':26}
 
+# perhaps we can do without this?
+
 begin_mudela_re = re.compile ('^ *\\\\begin{mudela}')
 begin_verbatim_re = re.compile ('^ *\\\\begin{verbatim}')
 end_verbatim_re = re.compile ('^ *\\\\end{verbatim}')
@@ -89,8 +102,10 @@ begin_document_re = re.compile ('^ *\\\\begin{document}')
 documentclass_re = re.compile('\\\\documentclass')
 twocolumn_re = re.compile('\\\\twocolumn')
 onecolumn_re = re.compile('\\\\onecolumn')
-preMudelaExample_re = re.compile('\\\\def\\\\preMudelaExample')
-postMudelaExample_re = re.compile('\\\\def\\\\postMudelaExample')
+mudela_file_re = re.compile('\\\\mudelafile{([^}]+)}')
+file_ext_re = re.compile('.+\\.([^.}]+$)')
+preMudelaExample_re = re.compile('^\s*\\\\def\\\\preMudelaExample')
+postMudelaExample_re = re.compile('^\s*\\\\def\\\\postMudelaExample')
 boundingBox_re = re.compile('%%BoundingBox: ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)')
 intertext_re = re.compile("intertext=\"([^\"]*)\"")
 
@@ -113,6 +128,17 @@ def ps_dimention(fname):
             int(s.groups()[3])-int(s.groups()[1]))
 
 
+def find_file (name):
+    for a in include_path:
+       try:
+           nm = os.path.join (a, name)
+           f = open (nm)
+           return nm
+       except IOError:
+           pass
+    return ''
+
+
 class CompileStatus:
     pass
 class SomethingIsSeriouslyBroken:
@@ -217,6 +243,15 @@ class Properties:
 
 
 class Mudela_output:
+    """ Using only self.code_type to deside both value of linewith and
+    if we have to put code into \score{...} was silly. Now we use:
+    self.code_type:  show us what need to be added.
+                        None : init value
+                        'NOTES' : add \context Voice{ ... }
+                        'CONTEXT' : add \score{ ... }
+                        'COMPLETE' : jupp
+    self.single_line_b:   0 : linewidth=-1  1: linewith=textwidth
+    """
     def __init__ (self, basename):
         self.basename = basename
         self.temp_filename = "%s/%s" %(outdir, 'mudela-temp.ly')
@@ -224,42 +259,58 @@ class Mudela_output:
         self.__lines = []
         # 'tex' or 'eps'
         self.graphic_type = 'tex'
-        self.code_type = 'unknown'
-        self.code_type_override = None
+        self.code_type = None
+        self.single_line_b = 1
+        self.optlist = []
     def write (self, line):
-        # match only if there is nothing but whitespace before \begin HACK
+        # match only if there is nothing but whitespace before \begin.
+        # we should not have to do this RE for every line
         if re.search('^\s*\\\\begin{mudela}', line):
-            self.scan_begin_statement(line)
-        else:
-            if self.code_type == 'unknown':
-                if re.search('^\s*\\\\score', line) or \
-                   re.search('^\s*\\\\paper', line) or \
-                   re.search('^\s*\\\\header', line) or \
-                   re.search('^\s*[A-Za-z]*\s*=', line):
-                    self.code_type = 'ly'
+            r  = begin_mudela_opts_re.search(line)
+            if r:
+                o = r.group()[1:-1]
+                self.optlist =  re.compile('[\s,]*').split(o)
+            else:
+                self.optlist = []
+        else: # ugh this is NOT bulletproof...
+            if not self.code_type:
+                if re.search('^\s*%', line) or re.search('^\s*$', line):
+                    pass
+                elif re.search('^\s*\\\\context', line):
+                    self.code_type = 'CONTEXT'
+                    self.single_line_b = 0
+                elif re.search('^\s*\\\\score', line) or \
+                     re.search('^\s*\\\\paper', line) or \
+                     re.search('^\s*\\\\header', line) or \
+                     re.search('^\s*\\\\version', line) or \
+                     re.search('^\s*\\\\include', line) or \
+                     re.search('^\s*[A-Za-z]*\s*=', line):
+                    self.code_type = 'COMPLETE'
+                    self.single_line_b = 0
+                else:
+                    self.code_type = 'NOTES'
+                    self.single_line_b = 1
             self.__lines.append(line)
-    def scan_begin_statement(self, line):
-        r  = begin_mudela_opts_re.search(line)
-        if r:
-            o = r.group()[1:-1]
-            optlist =  re.compile('[\s,]*').split(o)
-        else:
-            optlist = []
-        if 'fragment' in optlist:
-            self.code_type_override = 'fly'
-        if 'nonfragment' in optlist:
-            self.code_type_override = 'ly'
-        if 'eps' in optlist:
+    def write_red_tape(self):
+        if 'eps' in self.optlist:
             self.graphic_type = 'eps'
+            #self.single_line_b = 1
+        if 'fragment' in self.optlist:
+            self.code_type = 'NOTES'
+        if 'nonfragment' in self.optlist:
+            self.code_type = 'COMPLETE'
+        if 'multiline' in self.optlist:
+            self.single_line_b = 0
         for pt in fontsize_pt2i.keys():
-            if pt in optlist:
+            if pt in self.optlist:
                 Props.setMudelaFontsize(fontsize_pt2i[pt], 'block')
-    def write_red_tape(self):
         self.file.write ('\\include \"paper%d.ly\"\n' \
                          % Props.getMudelaFontsize())
                          
         s = fontsize_i2a[Props.getMudelaFontsize()]
-        if self.code_type == 'fly':
+        if 'singleline' in self.optlist:
+            self.single_line_b = 1
+        if self.single_line_b:
             linewidth_str = 'linewidth = -1.\cm;'
         else:
             linewidth_str = 'linewidth = %i.\\pt;' % Props.getLineWidth()
@@ -268,19 +319,18 @@ class Mudela_output:
                         + linewidth_str
                         + "castingalgorithm = \Gourlay; \n}")
                         #+ "castingalgorithm = \Wordwrap; indent = 2.\cm; \n}")
-        if self.code_type == 'fly':
-            self.file.write('\\score{\n\\notes{')
+        if self.code_type == 'CONTEXT':
+            self.file.write('\\score{\n\\notes\\relative c{')
+        if self.code_type == 'NOTES' :
+            self.file.write('\\score{\n\\notes\\relative c{\\context Voice{')
     def close (self):
-        if self.code_type == 'unknown':
-            self.code_type = 'fly'
-        if self.code_type_override:
-            self.code_type = self.code_type_override
         self.write_red_tape()
         for l in self.__lines:
             self.file.write(l)
-        if self.code_type == 'fly':
+        if self.code_type == 'CONTEXT':
             self.file.write('}}')
-            
+        elif self.code_type == 'NOTES':
+            self.file.write('}}}')
         self.file.close()
 
         inf = outdir + self.basename + '.ly'
@@ -291,10 +341,14 @@ class Mudela_output:
             status = os.system ('diff -q %s %s' % (self.temp_filename, inf))
         if status:
             os.rename (self.temp_filename, inf)
-        if need_recompile_b(inf, outf):
+
+       recompile_b =  need_recompile_b(inf, outf)
+       if recompile_b:
             out_files.append((self.graphic_type, inf))
+       return recompile_b
+
     def insert_me_string(self):
-        "Returns a string that can be used directly in latex."
+        "ugh the name of this function is wrong"
         if self.graphic_type == 'tex':
             return ['tex', self.basename]
         elif self.graphic_type == 'eps':
@@ -330,7 +384,7 @@ class Tex_output:
         if s != '':
             e = os.system('cd %s; lilypond %s' %(outdir, s))
             if e:
-                print "error: lilypond exited with value", e
+                sys.stderr.write("error: lilypond exited with value %i\n" % e)
                 sys.exit(e)
         for g in g_vec:
             if g[0] == 'eps':
@@ -340,20 +394,24 @@ class Tex_output:
     def write_outfile(self):
         file = open(self.output_fn+'.latex', 'w')
         file.write('% Created by mudela-book\n')
+        last_line = None
         for line in self.__lines:
             if type(line)==type([]):
-                if line[0] == 'tex':
-                    file.write('\\preMudelaExample\\input %s\n\postMudelaExample '\
-                              # TeX applies the prefix of the main source automatically.
+                if last_line == '\n':
+                    file.write(r'\vspace{0.5cm}')
+                if line[0] == 'tex':                    
+                    file.write('\\preMudelaExample \\input %s \\postMudelaExample\n'\
                                % (line[1]+'.tex'))
-#                               % (outdir+line[1]+'.tex'))
                 if line[0] == 'eps':
                     ps_dim = ps_dimention(outdir+line[1]+'.eps')
-                    file.write('\\parbox{%ipt}{\includegraphics{%s}}\n' \
+                    file.write('\\noindent\\parbox{%ipt}{\includegraphics{%s}}\n' \
                                % (ps_dim[0], line[1]+'.eps'))
-#                               % (ps_dim[0], outdir+line[1]+'.eps'))
             else:
                 file.write(line)
+            if type(last_line)==type([]):
+                if line=='\n':
+                    file.write(r'\vspace{0.5cm}')
+            last_line = line
         file.close()
 
 # given parameter s="\mudela[some options]{CODE} some text and commands"
@@ -386,6 +444,7 @@ class Tex_input:
             except:
                 continue
         raise IOError
+
     def get_lines (self):
         lines = self.infile.readlines ()
         (retlines, retdeps) = ([],[self.filename])
@@ -410,8 +469,7 @@ class Tex_input:
                     retlines = retlines + ls[0]
                     retdeps = retdeps + ls[1]
                 except:
-                    print "warning: can't find %s, let's hope latex will" \
-                          % r_inp.groups()[0]
+                    sys.stderr.write("warning: can't find %s, let's hope latex will\n" % r_inp.groups()[0])
                     retlines.append (line)
             elif r_inc:
                 try:
@@ -422,13 +480,13 @@ class Tex_input:
                     retlines = retlines + ls[0]
                     retdeps = retdeps + ls[1]
                 except:
-                    print "warning: can't find %s, let's hope latex will" \
-                          % r_inc.groups()[0]
+                    sys.stderr.write("warning: can't find %s, let's hope latex will" % r_inc.groups()[0])
                     retlines.append (line)
             else:
                 # This code should be rewritten, it looks terrible
                 r_mud = defined_mudela_cmd_re.search(line)
                 if r_mud:
+                   # TODO document this
                     ss = "\\\\verb(?P<xx>[^a-zA-Z])\s*\\\\%s\s*(?P=xx)" \
                          % re.escape(r_mud.group()[1:])
                     # just append the line if the command is inside \verb|..|
@@ -531,6 +589,35 @@ class Main_tex_input(Tex_input):
                     self.mudtex.write ('\\def\\preMudelaExample{}\n')
                 if not postMudelaDef:
                     self.mudtex.write ('\\def\\postMudelaExample{}\n')
+
+            elif mudela_file_re.search(line):
+               r = mudela_file_re.search(line)
+
+               self.mudela = Mudela_output(self.gen_basename())
+               fn = r.group (1)
+               full_path = find_file (fn)
+               if not full_path:
+                   sys.stderr.write("error: can't find file '%s'\n" % fn)
+                   sys.exit (1)
+
+               f = open (full_path, 'r')
+               lines =f.readlines ()
+               self.mudela.write ('%% This is a copy of file %s\n' % full_path)
+               for x in lines:
+                   self.mudela.write (x)
+               r = file_ext_re.search(fn)
+                if r:
+                    if r.group(1) == 'fly':
+                        self.mudela.optlist.append('multiline')
+               stat =self.mudela.close ()
+               if stat:
+                       sys.stdout.write("(File %s needs recompiling)\n" % full_path)
+                self.mudtex.write (self.mudela.insert_me_string())
+               self.deps.append (full_path)
+               del self.mudela
+                self.mudela = None
+                self.fine_count = self.fine_count + 1
+               continue
             elif begin_mudela_re.search (line) and not latex_verbatim:
                 Props.clear_for_new_block()
                 if __debug__:
@@ -578,6 +665,7 @@ class Main_tex_input(Tex_input):
                 self.mode = 'latex'
                 continue
 
+
             if self.mode == 'mudela':
                 self.mudela.write (line)
                 if self.verbatim:
@@ -601,6 +689,7 @@ Options:\n
   --force-mudela-fontsize=??pt   force fontsize for all inline mudela
   --force-verbatim               make all mudela verbatim\n
   --dependencies                 write dependencies
+  --include                      include path
   --init                         mudela-book initfile
   """
                     )
@@ -608,11 +697,13 @@ Options:\n
 
 
 def write_deps (fn, out,  deps):
-       out_fn = outdir + '/' + fn
-       print '`writing `%s\'\n\'' % out_fn
+       out_fn = os.path.join (outdir, fn)
+       
+       sys.stdout.write('writing `%s\'\n' % out_fn)
        
        f = open (out_fn, 'w')
-       f.write ('%s: %s\n'% (outdir + '/' + out + '.dvi',
+       target = re.sub (os.sep + os.sep, os.sep, os.path.join (outdir, out + '.latex'))
+       f.write ('%s: %s\n'% (target,
                              reduce (lambda x,y: x + ' '+ y, deps)))
        f.close ()
 
@@ -624,54 +715,57 @@ def main():
     outname = ''
     try:
         (options, files) = getopt.getopt(
-            sys.argv[1:], 'hd:o:', ['outdir=', 'outname=',
+            sys.argv[1:], 'hd:o:I:', ['outdir=', 'outname=',
                                     'default-mudela-fontsize=',
                                     'force-mudela-fontsize=',
-                                    'help', 'dependencies',
+                                    'help', 'dependencies', 'include=',
                                     'force-verbatim', 'init='])
     except getopt.error, msg:
-        print "error:", msg
+        sys.stderr.write("error: %s" % msg)
         sys.exit(1)
         
     do_deps = 0
     for opt in options:    
        o = opt[0]
        a = opt[1]
-       if o == '--outname' or o == '-o':
+       if o == '--include' or o == '-I':
+           include_path.append (a)
+       elif o == '--outname' or o == '-o':
             if len(files) > 1:
                 #HACK
-                print "Mudela-book is confused by --outname on multiple files"
+                sys.stderr.write("Mudela-book is confused by --outname on multiple files")
                 sys.exit(1)
             outname = a
-        if o == '--outdir' or o == '-d':
+        elif o == '--outdir' or o == '-d':
             outdir = a
-        if o == '--help' or o == '-h':
+        elif o == '--help' or o == '-h':
             help ()
-       if o == '--dependencies':
+       elif o == '--dependencies':
             do_deps = 1
-        if o == '--default-mudela-fontsize':
+        elif o == '--default-mudela-fontsize':
             if not fontsize_pt2i.has_key(a):
-                print "Error: illegal fontsize:", a
-                print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
+                sys.stderr.write("Error: illegal fontsize: %s" % a)
+                sys.stderr.write("  accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt")
                 sys.exit()
             Props.setMudelaFontsize(fontsize_pt2i[a], 'init')
-       if o == '--force-mudela-fontsize':
+       elif o == '--force-mudela-fontsize':
             if not fontsize_pt2i.has_key(a):
-                print "Error: illegal fontsize:", a
-                print " accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt"
+                sys.stderr.write("Error: illegal fontsize: %s" % a)
+                sys.stderr.write("  accepted fontsizes are: 11pt, 13pt, 16pt, 20pt, 26pt")
                 sys.exit()
             Props.force_mudela_fontsize = fontsize_pt2i[a]
-        if o == '--force-verbatim':
+        elif o == '--force-verbatim':
             Props.force_verbatim_b = 1
-        if o == '--init':
+        elif o == '--init':
             initfile =  a
     if outdir[-1:] != '/':
         outdir = outdir + '/'
 
+    # r""" ... """ means: leave escape seqs alone.
     defined_mudela_cmd = {'mudela': r"""
-\begin{mudela}[eps \fontoptions]
-  \type Staff <
-    \type Voice{
+\begin{mudela}[eps, singleline \fontoptions]
+  \context Staff <
+    \context Voice{
       \maininput
     }
   >
@@ -685,9 +779,9 @@ def main():
         for i in d.keys():
             defined_mudela_cmd[i] = d[i]
         del d
-    c = defined_mudela_cmd.keys()[0]
-    for x in defined_mudela_cmd.keys()[1:]:
-        c = c + '|'+x
+
+    c = string.join (defined_mudela_cmd.keys(), '|')
+
     defined_mudela_cmd_re = re.compile("\\\\(%s)(\[(\d*pt)\])*{([^}]*)}" %c)
 
     if not os.path.isdir(outdir):
@@ -702,7 +796,6 @@ def main():
         my_depname = my_outname + '.dep'        
         inp = Main_tex_input (input_filename, my_outname)
         inp.do_it ()
-#        os.system('latex %s/%s.latex' % (outdir, my_outname))
         if do_deps:
             write_deps (my_depname, my_outname, inp.deps)