]> git.donarmstrong.com Git - lilypond.git/blobdiff - scripts/convert-ly.py
*** empty log message ***
[lilypond.git] / scripts / convert-ly.py
index 4272e0fb65f56bddf050ddd015d93dccb531aa44..1f0cea3ad7eeabf70f58de646e6248c766b537ad 100644 (file)
@@ -4,20 +4,9 @@
 #
 # source file of the GNU LilyPond music typesetter
 #
-# (c) 1998--2004  Han-Wen Nienhuys <hanwen@cs.uu.nl>
+# (c) 1998--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
 #                 Jan Nieuwenhuizen <janneke@gnu.org>
 
-
-# TODO
-#   use -f and -t for -s output
-
-# NEWS
-# 0.2
-#  - rewrite in python
-
-program_name = 'convert-ly'
-version = '@TOPLEVEL_VERSION@'
-
 import os
 import sys
 import __main__
@@ -26,15 +15,19 @@ import  string
 import re
 import time
 
+program_name = sys.argv[0]
+
+version = '@TOPLEVEL_VERSION@'
+
 # Did we ever have \mudela-version?  I doubt it.
 # lilypond_version_re_str = '\\\\version *\"(.*)\"'
-lilypond_version_re_str = '\\\\(mudela-)?version *\"(.*)\"'
+lilypond_version_re_str = '\\\\(mudela-)?version *\"([^"]+)\"'
 lilypond_version_re = re.compile (lilypond_version_re_str)
 add_version = 1
 
 
 def program_id ():
-       return '%s (GNU LilyPond) %s' %(program_name,  version);
+       return '%s (GNU LilyPond) %s' % (program_name, version)
 
 def identify ():
        sys.stderr.write (program_id () + '\n')
@@ -48,9 +41,9 @@ input is guessed by default from \version directive.
 Options:
   -h, --help             print this help
   -e, --edit             edit in place
-  -f, --from=VERSION     start from version; overrides \version found in file
+  -f, --from=VERSION     start from version [default: \version found in file]
   -s, --show-rules       print all rules
-  -t, --to=VERSION       show target version
+  -t, --to=VERSION       end at version [default: @TOPLEVEL_VERSION@]
   -n, --no-version       don't add new version stamp
       --version          print program version
 
@@ -70,37 +63,22 @@ License, and you are welcome to change it and/or distribute copies of
 it under certain conditions.  invoke as `%s --warranty' for more
 information.
 
-""" % (program_id() , program_name))
+""" % (program_id (), program_name))
        
-def gulp_file(f):
-       try:
-               i = open(f)
-               i.seek (0, 2)
-               n = i.tell ()
-               i.seek (0,0)
-       except:
-               print 'can\'t open file: ' + f + '\n'
-               return ''
-       s = i.read (n)
-       if len (s) <= 0:
-               print 'gulped empty file: ' + f + '\n'
-       i.close ()
-       return s
-
 def str_to_tuple (s):
-       return tuple (map (string.atoi, string.split (s,'.')))
+       return tuple (map (string.atoi, string.split (s, '.')))
 
 def tup_to_str (t):
        return string.join (map (lambda x: '%s' % x, list (t)), '.')
 
 def version_cmp (t1, t2):
-       for x in [0,1,2]:
+       for x in [0, 1, 2]:
                if t1[x] - t2[x]:
                        return t1[x] - t2[x]
        return 0
 
 def guess_lilypond_version (filename):
-       s = gulp_file (filename)
+       s = open (filename).read ()
        m = lilypond_version_re.search (s)
        if m:
                return m.group (2)
@@ -114,7 +92,9 @@ conversions = []
 
 def show_rules (file):
        for x in conversions:
-               file.write  ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
+               if (not from_version or x[0] > from_version) \
+                  and (not to_version or x[0] <= to_version):
+                       file.write  ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
 
 ############################
                
@@ -148,7 +128,7 @@ if 1:                                       # need new a namespace
                return str
 
        conversions.append ((
-               ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax')))
+               ((0,1,20), conv, 'deprecated \\textstyle, new \\key syntax')))
 
 
 if 1:
@@ -402,7 +382,7 @@ if 1:
                return str
 
        conversions.append (((1,3,4), conv,
-                '\\cadenza -> \cadenza{On|Off}'))
+                '\\cadenza -> \\cadenza{On|Off}'))
 
 if 1:
        def conv (str):
@@ -1406,6 +1386,17 @@ if 1:
                str = re.sub (r'@ACCENT@', '>', str)
                return str
 
+       def text_markup (str):
+               str = re.sub (r"""([-_^]) *# *' *\( *music *(\"[^"]*\") *\)""",
+                               r"\1\\markup { \\musicglyph #\2 }", str)
+               str = re.sub (r"""([-_^]) *# *' *\( *([a-z]+) *([^()]*)\)""",
+                               r"\1\\markup { \\\2 \3 }", str)
+               str = re.sub (r"""\\mark *# *' *\( *music *(\"[^"]*\") *\)""",
+                               r"\\mark \\markup { \\musicglyph #\1 }", str)
+               str = re.sub (r"""\\mark *# *' *\( *([a-z]+) *([^()]*)\)""",
+                               r"\\mark \\markup { \\\1 \2 }", str)
+               return str      
+
        def articulation_substitute (str):
                str = re.sub (r"""([^-])\[ *([a-z]+[,']*[!?]?[0-9:]*\.*)""",
                              r"\1 \2[", str)
@@ -1428,13 +1419,14 @@ if 1:
                str =  conv_relative (str)
                str = sub_chords (str)
 
+               str = text_markup (str)
                str = articulation_substitute (str)
                str = re.sub ("@SCM_EOL@", "#'()", str)
                
                return str
        
        conversions.append (((1,9,0), conv, """New relative mode,
-Postfix articulations, new chord syntax."""))
+Postfix articulations, new text markup syntax, new chord syntax."""))
 
 if 1:
        def conv (str):
@@ -1613,7 +1605,7 @@ def conv (str):
        def func(match):
                return "#'font-size = #%d" % (2*string.atoi (match.group (1))) 
                
-       str =re.sub (r"#'font-relative-size\s*=\s*#([0-9-]+)", func, str)
+       str =re.sub (r"#'font-relative-size\s*=\s*#\+?([0-9-]+)", func, str)
        str =re.sub (r"#'font-family\s*=\s*#'ancient",
                     r"#'font-family = #'music", str)
        
@@ -1848,6 +1840,8 @@ Grob::preset_extent removed.
 
 
 def conv (str):
+       str = re.sub (r'(\\property[^=]+)=\s*([-0-9]+)',
+                     r'\1= #\2', str) 
        str = re.sub (r'\\property\s+([^. ]+)\s*\.\s*([^\\=]+)\s*\\(set|override)',
                      r"\\overrid@ \1.\2 ", str)
        str = re.sub (r'\\property\s+([^. ]+)\s*\.\s*([^\\= ]+)\s*=\s*',
@@ -1977,7 +1971,7 @@ def conv (str):
                elif o >= 0:
                        str += "'" * (o + 1)
                        
-               return '\\tuning %s ' % str
+               return '\\transposition %s ' % str
 
        
        str = re.sub (r"\\set ([A-Za-z]+\s*\.\s*)?transposing\s*=\s*#([-0-9]+)",
@@ -2067,9 +2061,269 @@ conversions.append (((2,1, 36), conv,
 def conv (str):
        return str
 
-conversions.append (((2,2, 0), conv,
+conversions.append (((2, 2, 0), conv,
                     '''clean up version. '''))
 
+def conv (str):
+       return re.sub (r'\\apply\b', r'\\applymusic', str)
+
+conversions.append (((2, 3, 1), conv,
+                    '''\\apply -> \\applymusic'''))
+
+def conv (str):
+       if re.search ('textheight', str):
+               sys.stderr.write("\nWarning: tuning of page layout has changed. See reference manual.\n")
+               
+       str = re.sub (r'\\OrchestralScoreContext', '\\Score', str)
+       def func(m):
+               if m.group(1) not in ['RemoveEmptyStaff',
+                                     'AncientRemoveEmptyStaffContext',
+                                     'EasyNotation']:
+                       return '\\' + m.group (1)
+               else:
+                       return m.group (0)
+               
+               
+       str = re.sub (r'\\([a-zA-Z]+)Context\b', func, str)
+
+       str = re.sub ('ly:paper-lookup', 'ly:output-def-lookup', str)
+       return str
+
+conversions.append (((2, 3, 2), conv,
+                    '''\\FooContext -> \\Foo'''))
+
+def conv (str):
+       str = re.sub (r'\\notes\b', '', str)
+       
+       return str
+
+conversions.append (((2, 3, 4), conv,
+                    '''remove \\notes'''))
+
+
+
+def conv (str):
+       str = re.sub (r'lastpagefill\s*=\s*"?1"', 'raggedlastbottom = ##t', str)
+       return str
+
+conversions.append (((2, 3, 6), conv,
+                    '''lastpagefill -> raggedlastbottom'''))
+
+
+
+def conv (str):
+       str = re.sub (r'\\consistsend', '\\consists', str)
+       str = re.sub (r'\\lyricsto\s+("?[a-zA-Z]+"?)(\s*\\new Lyrics\s*)?\\lyrics',
+                     r'\\lyricsto \1 \2', str)
+       return str
+
+conversions.append (((2, 3, 8), conv,
+                    '''remove \\consistsend, strip \\lyrics from \\lyricsto.'''))
+
+def conv (str):
+       str = re.sub (r'neo_mensural', 'neomensural', str)
+       str = re.sub (r'if-text-padding', 'bound-padding', str)
+       return str
+
+conversions.append (((2, 3, 9), conv,
+                    '''neo_mensural -> neomensural, if-text-padding -> bound-padding'''))
+
+
+
+def conv (str):
+       str = re.sub (r'\\addlyrics', r'\\oldaddlyrics', str)
+       str = re.sub (r'\\newlyrics', r'\\addlyrics', str)
+       if re.search (r"\\override\s*TextSpanner", str):
+               sys.stderr.write ("\nWarning: TextSpanner has been split into DynamicTextSpanner and TextSpanner\n") 
+       return str
+
+conversions.append (((2, 3, 10), conv,
+                    '''\\addlyrics -> \\oldaddlyrics, \\newlyrics -> \\addlyrics'''))
+
+def conv (str):
+       str = re.sub (r'\\setMmRestFermata\s+(R[0-9.*/]*)',
+                     r'\1^\\fermataMarkup', str)
+       return str
+
+conversions.append (((2, 3, 11), conv,
+                    '''\\setMmRestFermata -> ^\\fermataMarkup'''))
+
+def conv (str):
+       str = re.sub (r'\\newpage', r'\\pageBreak', str)
+       str = re.sub (r'\\scriptUp', r"""{
+  \\override TextScript  #'direction = #1
+  \\override Script  #'direction = #1
+}""", str)
+       str = re.sub (r'\\scriptDown', r"""{
+  \\override TextScript  #'direction = #-1
+  \\override Script  #'direction = #-1
+}""", str)
+       str = re.sub (r'\\scriptBoth', r"""{
+  \\revert TextScript  #'direction
+  \\revert Script  #'direction
+}""", str)
+       str = re.sub ('soloADue', 'printPartCombineTexts', str)
+       str = re.sub (r'\\applymusic\s*#notes-to-clusters',
+                     '\\makeClusters', str)
+
+       str = re.sub (r'pagenumber\s*=', 'firstpagenumber', str)
+       return str
+
+conversions.append (((2, 3, 12), conv,
+                    '''\\newpage -> \\pageBreak, junk \\script{up,down,both},
+soloADue -> printPartCombineTexts, #notes-to-clusters -> \\makeClusters
+'''))
+
+
+def conv (str):
+       str = re.sub (r'\\chords\b', r'\\chordmode', str)
+       str = re.sub (r'\\lyrics\b', r'\\lyricmode', str)
+       str = re.sub (r'\\figures\b', r'\\figuremode', str)
+       str = re.sub (r'\\notes\b', r'\\notemode', str)
+       str = re.sub (r'\\drums\b', r'\\drummode', str)
+       str = re.sub (r'\\chordmode\s*\\new ChordNames', r'\\chords', str)
+       str = re.sub (r'\\new ChordNames\s*\\chordmode', r'\\chords', str)
+       str = re.sub (r'\\new FiguredBass\s*\\figuremode', r'\\figures', str)
+       str = re.sub (r'\\figuremode\s*\new FiguredBass', r'\\figures', str)
+       str = re.sub (r'\\new DrumStaff\s*\\drummode', r'\\drums', str)
+       str = re.sub (r'\\drummode\s*\\new DrumStaff', r'\\drums', str)
+
+       return str
+
+conversions.append (((2, 3, 16), conv,
+                    '''\foo -> \foomode (for chords, notes, etc.)
+fold \new FooContext \foomode into \foo.'''))
+
+def conv (str):
+       str = re.sub (r'(slur|stem|phrasingSlur|tie|dynamic|dots|tuplet|arpeggio|)Both', r'\1Neutral', str)
+       str = re.sub (r"\\applymusic\s*#\(remove-tag\s*'([a-z-0-9]+)\)",
+                     r"\\removeWithTag #'\1", str)
+       return str
+
+conversions.append (((2, 3, 17), conv,
+                    '''\foo -> \foomode (for chords, notes, etc.)
+fold \new FooContext \foomode into \foo.'''))
+
+
+def conv (str):
+       str = re.sub (r'Text_item', 'Text_interface', str)
+       return str
+
+conversions.append (((2, 3, 18),
+                    conv,
+                    '''Text_item -> Text_interface''' ))
+
+def conv (str):
+       str = re.sub (r'\\paper', r'\\layout', str)
+       str = re.sub (r'\\bookpaper', r'\\paper', str)
+       if re.search ('paper-set-staff-size', str):
+               sys.stderr.write ('''\nWarning: staff size should be changed at top-level
+with
+
+  #(set-global-staff-size <STAFF-HEIGHT-IN-POINT>)
+
+''')
+               
+               
+       str = re.sub (r'#\(paper-set-staff-size', '%Use set-global-staff-size at toplevel\n% #(layout-set-staff-size', str)
+       return str
+
+conversions.append (((2, 3, 22),
+                    conv,
+                    '''paper -> layout
+ bookpaper -> paper''' ))
+
+
+def conv (str):
+       str = re.sub (r'\\context\s+([a-zA-Z]+)\s*=\s*([a-z]+)\s',
+                     r'\\context \1 = "\2" ',
+                     str )
+       return str
+
+conversions.append (((2, 3, 23),
+                    conv,
+                    '''\context Foo = NOTENAME -> \context Foo = "NOTENAME"'''))
+
+def conv (str):
+       def sub(m):
+               return regularize_id (m.group (1))
+       str = re.sub (r'(maintainer_email|maintainer_web|midi_stuff|gourlay_maxmeasures)',
+                     sub, str)
+       return str
+
+conversions.append (((2, 3, 24),
+                    conv,
+                    '''regularize other identifiers.'''))
+
+
+def conv (str):
+       return str
+
+conversions.append (((2, 4, 0),
+                    conv,
+                    ''))
+
+
+def conv (str):
+       str = re.sub (r'\\quote\s+"?([a-zA-Z0-9]+)"?\s+([0-9.*/]+)',
+                     r'\\quoteDuring #"\1" { \skip \2 }',
+                     str
+                     )
+       return str
+
+conversions.append (((2, 5, 0),
+                    conv,
+                    ''))
+
+
+def conv (str):
+       str = re.sub (r'ly:import-module',
+                     r'ly:module-copy', str) 
+       return str
+
+conversions.append (((2, 5, 1),
+                    conv,
+                    'ly:import-module -> ly:module-copy'))
+
+def conv (str):
+       str = re.sub (r'\\(column|fill-line|dir-column|center-align|right-align|left-align|bracketed-y-column)\s*<([^<]*)>',
+                     r'\\\1 {\2}', str)
+       str = re.sub (r'\\(column|fill-line|dir-column|center-align|right-align|left-align|bracketed-y-column)\s*<([^<]*)>',
+                     r'\\\1 {\2}', str)
+       str = re.sub (r'\\(column|fill-line|dir-column|center-align|right-align|left-align|bracketed-y-column)\s*<([^<]*)>',
+                     r'\\\1 {\2}', str)
+       return str
+
+conversions.append (((2, 5, 2),
+                    conv,
+                    '\markup .. < .. > .. -> \markup .. { .. } ..'))
+
+def conv (str):
+       str = re.sub ('ly:find-glyph-by-name', 'ly:font-get-glyph', str)
+       str = re.sub ('"(scripts|clefs|accidentals)-', r'"\1.', str)
+       str = re.sub ("'hufnagel-do-fa", "'hufnagel.do.fa", str) 
+       str = re.sub ("'(vaticana|hufnagel|medicaea|petrucci|neomensural|mensural)-", r"'\1.", str) 
+       return str
+
+conversions.append (((2, 5, 3),
+                    conv,
+                    'ly:find-glyph-by-name -> ly:font-get-glyph, remove - from glyphnames.'))
+
+
+
+def conv (str):
+       if re.search (r"\\encoding", str):
+               sys.stderr.write ("Sorry, input files should be UTF8.\n"
+                                 + "Please convert by hand.")
+               raise FatalConversionError()
+
+       return str
+
+conversions.append (((2, 5, 2),
+                    conv,
+                    'ly:find-glyph-by-name -> ly:font-get-glyph, remove - from glyphnames.'))
+
+
 ################################
 #      END OF CONVERSIONS      
 ################################
@@ -2091,7 +2345,7 @@ def do_conversion (infile, from_version, outfile, to_version):
        last_conversion = ()
        try:
                for x in conv_list:
-                       sys.stderr.write (tup_to_str (x[0])  + ', ')
+                       sys.stderr.write (tup_to_str (x[0]) + ', ')
                        str = x[1] (str)
                        last_conversion = x[0]
 
@@ -2103,11 +2357,11 @@ def do_conversion (infile, from_version, outfile, to_version):
                new_ver =  '\\version \"%s\"' % tup_to_str (last_conversion)
 
                if re.search (lilypond_version_re_str, str):
-                       str = re.sub (lilypond_version_re_str,'\\'+new_ver , str)
+                       str = re.sub (lilypond_version_re_str,'\\'+new_ver, str)
                elif add_version:
                        str = new_ver + '\n' + str
 
-               outfile.write(str)
+               outfile.write (str)
 
        return last_conversion
        
@@ -2127,7 +2381,7 @@ def do_one_file (infile_name):
        else:
                guess = guess_lilypond_version (infile_name)
                if not guess:
-                       raise UnknownVersion()
+                       raise UnknownVersion ()
                from_version = str_to_tuple (guess)
 
        if __main__.to_version:
@@ -2170,17 +2424,20 @@ assume_old = 0
 to_version = ()
 from_version = ()
 outfile_name = ''
+show_rules_p = 0
 
-(options, files) = getopt.getopt (
-       sys.argv[1:], 'ao:f:t:senh', ['no-version', 'version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to='])
+(options, files) = getopt.getopt (sys.argv[1:], 'ao:f:t:senh',
+                                 ['no-version', 'version', 'output',
+                                 'show-rules', 'help', 'edit',
+                                 'from=', 'to='])
 
 for opt in options:
        o = opt[0]
        a = opt[1]
-       if o== '--help' or o == '-h':
+       if o == '--help' or o == '-h':
                usage ()
                sys.exit (0)
-       if o == '--version' or o == '-v':
+       elif o == '--version' or o == '-v':
                print_version ()
                sys.exit (0)
        elif o== '--from' or o=='-f':
@@ -2190,8 +2447,7 @@ for opt in options:
        elif o== '--edit' or o == '-e':
                edit = 1
        elif o== '--show-rules' or o == '-s':
-               show_rules (sys.stdout)
-               sys.exit(0)
+               show_rules_p = 1
        elif o == '--output' or o == '-o':
                outfile_name = a
        elif o == '--no-version' or o == '-n':
@@ -2200,6 +2456,11 @@ for opt in options:
                print o
                raise getopt.error
 
+# should parse files[] to read \version?
+if show_rules_p:
+       show_rules (sys.stdout)
+       sys.exit (0)
+               
 identify ()
 for f in files:
        if f == '-':
@@ -2214,7 +2475,7 @@ for f in files:
                sys.stderr.write ('\n')
                if assume_old:
                        fv = from_version
-                       from_version = (0,0,0)
+                       from_version = (0, 0, 0)
                        do_one_file (f)
                        from_version = fv
                else: