]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
583d7f5f9e90559e3ac64e91b4737e460128ca59
[lilypond.git] / scripts / convert-ly.py
1 #!@PYTHON@
2 #
3 # convert-ly.py -- convertor for lilypond versions
4
5 # source file of the GNU LilyPond music typesetter
6
7 # (c) 1998--2001
8
9 # TODO
10 #   use -f and -t for -s output
11
12 # NEWS
13 # 0.2
14 #  - rewrite in python
15
16 program_name = 'convert-ly'
17 version = '@TOPLEVEL_VERSION@'
18
19 import os
20 import sys
21 import __main__
22 import getopt
23 import  string
24 import re
25 import time
26
27 # Did we ever have \mudela-version?  I doubt it.
28 # lilypond_version_re_str = '\\\\version *\"(.*)\"'
29 lilypond_version_re_str = '\\\\(mudela-)?version *\"(.*)\"'
30 lilypond_version_re = re.compile (lilypond_version_re_str)
31
32 def program_id ():
33         return '%s (GNU LilyPond) %s' %(program_name,  version);
34
35 def identify ():
36         sys.stderr.write (program_id () + '\n')
37
38 def usage ():
39         sys.stdout.write (
40                 r"""Usage: %s [OPTION]... [FILE]... 
41 Try to convert to newer lilypond-versions.  The version number of the
42 input is guessed by default from \version directive
43
44 Options:
45   -a, --assume-old       apply all conversions to unversioned files
46   -h, --help             print this help
47   -e, --edit             in place edit
48   -f, --from=VERSION     start from version
49   -s, --show-rules       print all rules.
50   -t, --to=VERSION       target version
51       --version          print program version
52
53 Report bugs to bugs-gnu-music@gnu.org
54
55 """ % program_name)
56         
57         
58         sys.exit (0)
59
60 def print_version ():
61         
62         sys.stdout.write (r"""%s
63
64 This is free software.  It is covered by the GNU General Public
65 License, and you are welcome to change it and/or distribute copies of
66 it under certain conditions.  invoke as `%s --warranty' for more
67 information.
68
69 """ % (program_id() , program_name))
70         
71 def gulp_file(f):
72         try:
73                 i = open(f)
74                 i.seek (0, 2)
75                 n = i.tell ()
76                 i.seek (0,0)
77         except:
78                 print 'can\'t open file: ' + f + '\n'
79                 return ''
80         s = i.read (n)
81         if len (s) <= 0:
82                 print 'gulped empty file: ' + f + '\n'
83         i.close ()
84         return s
85
86 def str_to_tuple (s):
87         return tuple (map (string.atoi, string.split (s,'.')))
88
89 def tup_to_str (t):
90         return string.join (map (lambda x: '%s' % x, list (t)), '.')
91
92 def version_cmp (t1, t2):
93         for x in [0,1,2]:
94                 if t1[x] - t2[x]:
95                         return t1[x] - t2[x]
96         return 0
97
98 def guess_lilypond_version (filename):
99         s = gulp_file (filename)
100         m = lilypond_version_re.search (s)
101         if m:
102                 return m.group (2)
103         else:
104                 return ''
105
106 class FatalConversionError:
107         pass
108
109 conversions = []
110
111 def show_rules (file):
112         for x in conversions:
113                 file.write  ('%s: %s\n' % (tup_to_str (x[0]), x[2]))
114
115 ############################
116                 
117 if 1:
118         def conv(str):
119                 if re.search ('\\\\multi', str):
120                         sys.stderr.write ('\nNot smart enough to convert \\multi')
121                 return str
122         
123         conversions.append (((0,1,9), conv, '\\header { key = concat + with + operator }'))
124
125 if 1:                                   # need new a namespace
126         def conv (str):
127                 if re.search ('\\\\octave', str):
128                         sys.stderr.write ('\nNot smart enough to convert \\octave')
129                 #       raise FatalConversionError()
130                 
131                 return str
132
133         conversions.append ((
134                 ((0,1,19), conv, 'deprecated \\octave; can\'t convert automatically')))
135
136
137 if 1:                                   # need new a namespace
138         def conv (str):
139                 str = re.sub ('\\\\textstyle([^;]+);',
140                                          '\\\\property Lyrics . textstyle = \\1', str)
141                 # harmful to current .lys
142                 # str = re.sub ('\\\\key([^;]+);', '\\\\accidentals \\1;', str)
143                         
144                 return str
145
146         conversions.append ((
147                 ((0,1,20), conv, 'deprecated \\textstyle, new \key syntax')))
148
149
150 if 1:
151         def conv (str):
152                 str = re.sub ('\\\\musical_pitch', '\\\\musicalpitch',str)
153                 str = re.sub ('\\\\meter', '\\\\time',str)
154                         
155                 return str
156
157         conversions.append ((
158                 ((0,1,21), conv, '\\musical_pitch -> \\musicalpitch, '+
159                  '\\meter -> \\time')))
160
161 if 1:
162         def conv (str):
163                 return str
164
165         conversions.append ((
166                 ((1,0,0), conv, '0.1.21 -> 1.0.0 ')))
167
168
169 if 1:
170         def conv (str):
171                 str = re.sub ('\\\\accidentals', '\\\\keysignature',str)
172                 str = re.sub ('specialaccidentals *= *1', 'keyoctaviation = 0',str)
173                 str = re.sub ('specialaccidentals *= *0', 'keyoctaviation = 1',str)
174                         
175                 return str
176
177         conversions.append ((
178                 ((1,0,1), conv, '\\accidentals -> \\keysignature, ' +
179                  'specialaccidentals -> keyoctaviation')))
180
181 if 1:
182         def conv(str):
183                 if re.search ('\\\\header', str):
184                         sys.stderr.write ('\nNot smart enough to convert to new \\header format')
185                 return str
186         
187         conversions.append (((1,0,2), conv, '\\header { key = concat + with + operator }'))
188
189 if 1:
190         def conv(str):
191                 str =  re.sub ('\\\\melodic([^a-zA-Z])', '\\\\notes\\1',str)
192                 return str
193         
194         conversions.append (((1,0,3), conv, '\\melodic -> \\notes'))
195
196 if 1:
197         def conv(str):
198                 str =  re.sub ('default_paper *=', '',str)
199                 str =  re.sub ('default_midi *=', '',str)
200                 return str
201         
202         conversions.append (((1,0,4), conv, 'default_{paper,midi}'))
203
204 if 1:
205         def conv(str):
206                 str =  re.sub ('ChoireStaff', 'ChoirStaff',str)
207                 str =  re.sub ('\\\\output', 'output = ',str)
208                         
209                 return str
210         
211         conversions.append (((1,0,5), conv, 'ChoireStaff -> ChoirStaff'))
212
213 if 1:
214         def conv(str):
215                 if re.search ('[a-zA-Z]+ = *\\translator',str):
216                         sys.stderr.write ('\nNot smart enough to change \\translator syntax')
217                 #       raise FatalConversionError()
218                 return str
219         
220         conversions.append (((1,0,6), conv, 'foo = \\translator {\\type .. } ->\\translator {\\type ..; foo; }'))
221
222
223 if 1:
224         def conv(str):
225                 str =  re.sub ('\\\\lyrics*', '\\\\lyrics',str)
226                         
227                 return str
228         
229         conversions.append (((1,0,7), conv, '\\lyric -> \\lyrics'))
230
231 if 1:
232         def conv(str):
233                 str =  re.sub ('\\\\\\[/3+', '\\\\times 2/3 { ',str)
234                 str =  re.sub ('\\[/3+', '\\\\times 2/3 { [',str)
235                 str =  re.sub ('\\\\\\[([0-9/]+)', '\\\\times \\1 {',str)
236                 str =  re.sub ('\\[([0-9/]+)', '\\\\times \\1 { [',str)
237                 str =  re.sub ('\\\\\\]([0-9/]+)', '}', str)
238                 str =  re.sub ('\\\\\\]', '}',str)
239                 str =  re.sub ('\\]([0-9/]+)', '] }', str)
240                 return str
241         
242         conversions.append (((1,0,10), conv, '[2/3 ]1/1 -> \\times 2/3 '))
243
244 if 1:
245         def conv(str):
246                 return str
247         conversions.append (((1,0,12), conv, 'Chord syntax stuff'))
248
249
250 if 1:
251         def conv(str):
252                 
253                 
254                 str =  re.sub ('<([^>~]+)~([^>]*)>','<\\1 \\2> ~', str)
255                         
256                 return str
257         
258         conversions.append (((1,0,13), conv, '<a ~ b> c -> <a b> ~ c'))
259
260 if 1:
261         def conv(str):
262                 str =  re.sub ('<\\[','[<', str)
263                 str =  re.sub ('\\]>','>]', str)
264                         
265                 return str
266         
267         conversions.append (((1,0,14), conv, '<[a b> <a b]>c -> [<a b> <a b>]'))
268
269
270 if 1:
271         def conv(str):
272                 str =  re.sub ('\\\\type([^\n]*engraver)','\\\\TYPE\\1', str)
273                 str =  re.sub ('\\\\type([^\n]*performer)','\\\\TYPE\\1', str)
274                 str =  re.sub ('\\\\type','\\\\context', str)
275                 str =  re.sub ('\\\\TYPE','\\\\type', str)
276                 str =  re.sub ('textstyle','textStyle', str)
277                         
278                 return str
279         
280         conversions.append (((1,0,16), conv, '\\type -> \\context, textstyle -> textStyle'))
281
282
283 if 1:
284         def conv(str):
285                 if re.search ('\\\\repeat',str):
286                         sys.stderr.write ('\nNot smart enough to convert \\repeat')
287                 #       raise FatalConversionError()
288                 return str
289         
290         conversions.append (((1,0,18), conv,
291                 '\\repeat NUM Music Alternative -> \\repeat FOLDSTR Music Alternative'))
292
293 if 1:
294         def conv(str):
295                 str =  re.sub ('SkipBars','skipBars', str)
296                 str =  re.sub ('fontsize','fontSize', str)
297                 str =  re.sub ('midi_instrument','midiInstrument', str)                 
298                         
299                 return str
300
301         conversions.append (((1,0,19), conv,
302                 'fontsize -> fontSize, midi_instrument -> midiInstrument, SkipBars -> skipBars'))
303
304
305 if 1:
306         def conv(str):
307                 str =  re.sub ('tieydirection','tieVerticalDirection', str)
308                 str =  re.sub ('slurydirection','slurVerticalDirection', str)
309                 str =  re.sub ('ydirection','verticalDirection', str)                   
310                         
311                 return str
312
313         conversions.append (((1,0,20), conv,
314                 '{,tie,slur}ydirection -> {v,tieV,slurV}erticalDirection'))
315
316
317 if 1:
318         def conv(str):
319                 str =  re.sub ('hshift','horizontalNoteShift', str)
320                         
321                 return str
322
323         conversions.append (((1,0,21), conv,
324                 'hshift -> horizontalNoteShift'))
325
326
327 if 1:
328         def conv(str):
329                 str =  re.sub ('\\\\grouping[^;]*;','', str)
330                         
331                 return str
332
333         conversions.append (((1,1,52), conv,
334                 'deprecate \\grouping'))
335
336
337 if 1:
338         def conv(str):
339                 str =  re.sub ('\\\\wheel','\\\\coda', str)
340                         
341                 return str
342
343         conversions.append (((1,1,55), conv,
344                 '\\wheel -> \\coda'))
345
346 if 1:
347         def conv(str):
348                 str =  re.sub ('keyoctaviation','keyOctaviation', str)
349                 str =  re.sub ('slurdash','slurDash', str)
350                         
351                 return str
352
353         conversions.append (((1,1,65), conv,
354                 'slurdash -> slurDash, keyoctaviation -> keyOctaviation'))
355
356 if 1:
357         def conv(str):
358                 str =  re.sub ('\\\\repeat *\"?semi\"?','\\\\repeat "volta"', str)
359                         
360                 return str
361
362         conversions.append (((1,1,66), conv,
363                 'semi -> volta'))
364
365
366 if 1:
367         def conv(str):
368                 str =  re.sub ('\"?beamAuto\"? *= *\"?0?\"?','noAutoBeaming = "1"', str)
369                         
370                 return str
371
372         conversions.append (((1,1,67), conv,
373                 'beamAuto -> noAutoBeaming'))
374
375 if 1:
376         def conv(str):
377                 str =  re.sub ('automaticMelismas', 'automaticMelismata', str)
378                         
379                 return str
380
381         conversions.append (((1,2,0), conv,
382                 'automaticMelismas -> automaticMelismata'))
383
384 if 1:
385         def conv(str):
386                 str =  re.sub ('dynamicDir\\b', 'dynamicDirection', str)
387                         
388                 return str
389
390         conversions.append (((1,2,1), conv,
391                 'dynamicDir -> dynamicDirection'))
392
393 if 1:
394         def conv(str):
395                 str =  re.sub ('\\\\cadenza *0 *;', '\\\\cadenzaOff', str)
396                 str =  re.sub ('\\\\cadenza *1 *;', '\\\\cadenzaOn', str)               
397                         
398                 return str
399
400         conversions.append (((1,3,4), conv,
401                 '\\cadenza -> \cadenza{On|Off}'))
402
403 if 1:
404         def conv (str):
405                 str = re.sub ('"?beamAuto([^"=]+)"? *= *"([0-9]+)/([0-9]+)" *;*',
406                               'beamAuto\\1 = #(make-moment \\2 \\3)',
407                               str)
408                 return str
409
410         conversions.append (((1,3,5), conv, 'beamAuto moment properties'))
411
412 if 1:
413         def conv (str):
414                 str = re.sub ('stemStyle',
415                               'flagStyle',
416                               str)
417                 return str
418
419         conversions.append (((1,3,17), conv, 'stemStyle -> flagStyle'))
420
421 if 1:
422         def conv (str):
423                 str = re.sub ('staffLineLeading',
424                               'staffSpace',
425                               str)
426                 return str
427
428         conversions.append (((1,3,18), conv, 'staffLineLeading -> staffSpace'))
429
430
431 if 1:
432         def conv(str):
433                 if re.search ('\\\\repetitions',str):
434                         sys.stderr.write ('\nNot smart enough to convert \\repetitions')
435                 #       raise FatalConversionError()
436                 return str
437         
438         conversions.append (((1,3,23), conv,
439                 '\\\\repetitions feature dropped'))
440
441
442 if 1:
443         def conv (str):
444                 str = re.sub ('textEmptyDimension *= *##t',
445                               'textNonEmpty = ##f',
446                               str)
447                 str = re.sub ('textEmptyDimension *= *##f',
448                               'textNonEmpty = ##t',
449                               str)
450                 return str
451
452         conversions.append (((1,3,35), conv, 'textEmptyDimension -> textNonEmpty'))
453
454 if 1:
455         def conv (str):
456                 str = re.sub ("([a-z]+)[ \t]*=[ \t]*\\\\musicalpitch *{([- 0-9]+)} *\n",
457                               "(\\1 . (\\2))\n", str)
458                 str = re.sub ("\\\\musicalpitch *{([0-9 -]+)}",
459                               "\\\\musicalpitch #'(\\1)", str)
460                 if re.search ('\\\\notenames',str):
461                         sys.stderr.write ('\nNot smart enough to convert to new \\notenames format')
462                 return str
463
464         conversions.append (((1,3,38), conv, '\musicalpitch { a b c } -> #\'(a b c)'))
465
466 if 1:
467         def conv (str):
468                 def replace (match):
469                         return '\\key %s;' % string.lower (match.group (1))
470                 
471                 str = re.sub ("\\\\key ([^;]+);",  replace, str)
472                 return str
473         
474         conversions.append (((1,3,39), conv, '\\key A ;  ->\\key a;'))
475
476 if 1:
477         def conv (str):
478                 if re.search ('\\[:',str):
479                         sys.stderr.write ('\nNot smart enough to convert to new tremolo format')
480                 return str
481
482         conversions.append (((1,3,41), conv,
483                 '[:16 c4 d4 ] -> \\repeat "tremolo" 2 { c16 d16 }'))
484
485 if 1:
486         def conv (str):
487                 str = re.sub ('Staff_margin_engraver' , 'Instrument_name_engraver', str)
488                 return str
489
490         conversions.append (((1,3,42), conv,
491                 'Staff_margin_engraver deprecated, use Instrument_name_engraver'))
492
493 if 1:
494         def conv (str):
495                 str = re.sub ('note[hH]eadStyle\\s*=\\s*"?(\\w+)"?' , "noteHeadStyle = #'\\1", str)
496                 return str
497
498         conversions.append (((1,3,49), conv,
499                 'noteHeadStyle value: string -> symbol'))
500
501 if 1:
502         def conv (str):
503                 if re.search ('\\\\keysignature', str):
504                         sys.stderr.write ('\nNot smart enough to convert to new tremolo format')
505                 return str
506
507
508         conversions.append (((1,3,58), conv,
509                 'noteHeadStyle value: string -> symbol'))
510
511 if 1:
512         def conv (str):
513                 str = re.sub (r"""\\key *([a-z]+) *;""", r"""\\key \1 \major;""",str);
514                 return str
515         conversions.append (((1,3,59), conv,
516                 '\key X ; -> \key X major; ')) 
517
518 if 1:
519         def conv (str):
520                 str = re.sub (r'latexheaders *= *"\\\\input ',
521                               'latexheaders = "',
522                               str)
523                 return str
524         conversions.append (((1,3,68), conv, 'latexheaders = "\\input global" -> latexheaders = "global"'))
525
526
527
528 ################ TODO: lots of other syntax change should be done here as well
529
530
531
532 if 1:
533         def conv (str):
534                 str = re.sub ('basicCollisionProperties', 'NoteCollision', str)
535                 str = re.sub ('basicVoltaSpannerProperties' , "VoltaBracket", str)
536                 str = re.sub ('basicKeyProperties' , "KeySignature", str)
537
538                 str = re.sub ('basicClefItemProperties' ,"Clef", str)
539
540
541                 str = re.sub ('basicLocalKeyProperties' ,"Accidentals", str)
542                 str = re.sub ('basicMarkProperties' ,"Accidentals", str)
543                 str = re.sub ('basic([A-Za-z_]+)Properties', '\\1', str)
544
545                 str = re.sub ('Repeat_engraver' ,'Volta_engraver', str)
546                 return str
547         
548         conversions.append (((1,3,92), conv, 'basicXXXProperties -> XXX, Repeat_engraver -> Volta_engraver'))
549
550 if 1:
551         def conv (str):
552                 # Ugh, but meaning of \stemup changed too
553                 # maybe we should do \stemup -> \stemUp\slurUp\tieUp ?
554                 str = re.sub ('\\\\stemup', '\\\\stemUp', str)
555                 str = re.sub ('\\\\stemdown', '\\\\stemDown', str)
556                 str = re.sub ('\\\\stemboth', '\\\\stemBoth', str)
557                 
558                 str = re.sub ('\\\\slurup', '\\\\slurUp', str)
559                 str = re.sub ('\\\\slurboth', '\\\\slurBoth', str)
560                 str = re.sub ('\\\\slurdown', '\\\\slurDown', str)
561                 str = re.sub ('\\\\slurdotted', '\\\\slurDotted', str)
562                 str = re.sub ('\\\\slurnormal', '\\\\slurNoDots', str)          
563                 
564                 str = re.sub ('\\\\shiftoff', '\\\\shiftOff', str)
565                 str = re.sub ('\\\\shifton', '\\\\shiftOn', str)
566                 str = re.sub ('\\\\shiftonn', '\\\\shiftOnn', str)
567                 str = re.sub ('\\\\shiftonnn', '\\\\shiftOnnn', str)
568
569                 str = re.sub ('\\\\onevoice', '\\\\oneVoice', str)
570                 str = re.sub ('\\\\voiceone', '\\\\voiceOne', str)
571                 str = re.sub ('\\\\voicetwo', '\\\\voiceTwo', str)
572                 str = re.sub ('\\\\voicethree', '\\\\voiceThree', str)
573                 str = re.sub ('\\\\voicefour', '\\\\voiceFour', str)
574
575                 # I don't know exactly when these happened...
576                 # ugh, we loose context setting here...
577                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\stemUp\\\\slurUp\\\\tieUp', str)
578                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\stemDown\\\\slurDown\\\\tieDown', str)
579                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\stemBoth\\\\slurBoth\\\\tieBoth', str)
580
581                 str = re.sub ('verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #1', str)
582                 str = re.sub ('verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #-1', str)
583                 str = re.sub ('verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #0', str)
584                 
585                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\\\1Up', str)
586                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\\\1Down', str)
587                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\\\1Both', str)
588
589                 # (lacks capitalisation slur -> Slur)
590                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\1 \\\\override #\'direction = #1', str)
591                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\1 \\override #\'direction = #-1', str)
592                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\1 \\\\override #\'direction = #0', str)
593
594                 ## dynamic..
595                 str = re.sub ('\\\\property *[^ .]*[.]?dynamicDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\dynamicUp', str)
596                 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\dynamicDown', str)
597                 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?(0|(\\\\center))"?', '\\\\dynamicBoth', str)
598
599                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?(0|(""))"?', '\\\\\\1NoDots', str)
600                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?([1-9]+)"?', '\\\\\\1Dotted', str)
601
602                 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?(0|(""))"?', '\\\\autoBeamOn', str)
603                 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?([1-9]+)"?', '\\\\autoBeamOff', str)
604
605
606
607                 return str
608         
609         conversions.append (((1,3,93), conv,
610                 'property definiton case (eg. onevoice -> oneVoice)'))
611
612
613 if 1:
614         def conv (str):
615                 str = re.sub ('ChordNames*', 'ChordNames', str)
616                 if re.search ('\\\\textscript "[^"]* *"[^"]*"', str):
617                         sys.stderr.write ('\nNot smart enough to convert to new \\textscript markup text')
618
619                 str = re.sub ('\\textscript +("[^"]*")', '\\textscript #\\1', str)
620
621                 return str
622         
623         conversions.append (((1,3,97), conv, 'ChordName -> ChordNames'))
624
625
626 ## TODO: add lots of these
627         
628 if 1:
629         def conv (str):
630                 str = re.sub ('\\\\property *"?Voice"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Voice.TextScript \\\\set #\'font-style = #\'\\1', str)
631                 str = re.sub ('\\\\property *"?Lyrics"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Lyrics.LyricText \\\\set #\'font-style = #\'\\1', str)
632
633                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?timeSignatureStyle"? *= *"([^"]*)"', '\\\\property \\1.TimeSignature \\\\override #\'style = #\'\\2', str) 
634
635                 str = re.sub ('"?timeSignatureStyle"? *= *#?""', 'TimeSignature \\\\override #\'style = ##f', str)
636                 
637                 str = re.sub ('"?timeSignatureStyle"? *= *#?"([^"]*)"', 'TimeSignature \\\\override #\'style = #\'\\1', str)
638                 
639                 str = re.sub ('#\'style *= #*"([^"])"', '#\'style = #\'\\1', str)
640                 
641                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?horizontalNoteShift"? *= *"?#?([-0-9]+)"?', '\\\\property \\1.NoteColumn \\\\override #\'horizontal-shift = #\\2', str) 
642
643                 # ugh
644                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *""', '\\\\property \\1.Stem \\\\override #\'flag-style = ##f', str)
645                 
646                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *"([^"]*)"', '\\\\property \\1.Stem \\\\override #\'flag-style = #\'\\2', str) 
647                 return str
648         
649         conversions.append (((1,3,98), conv, 'CONTEXT.textStyle -> GROB.#font-style '))
650
651 if 1:
652         def conv (str):
653                 str = re.sub ('"?beamAutoEnd_([0-9]*)"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end 1 \\1 * *) = \\2', str)
654                 str = re.sub ('"?beamAutoBegin_([0-9]*)"? *= *(#\\([^)]*\))', 'autoBeamSettings \\push #\'(begin 1 \\1 * *) = \\2', str)
655                 str = re.sub ('"?beamAutoEnd"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end * * * *) = \\1', str)
656                 str = re.sub ('"?beamAutoBegin"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(begin * * * *) = \\1', str)
657
658
659                 return str
660         
661         conversions.append (((1,3,102), conv, 'beamAutoEnd -> autoBeamSettings \\push (end * * * *)'))
662
663
664 if 1:
665         def conv (str):
666                 str = re.sub ('\\\\push', '\\\\override', str)
667                 str = re.sub ('\\\\pop', '\\\\revert', str)
668
669                 return str
670         
671         conversions.append (((1,3,111), conv, '\\push -> \\override, \\pop -> \\revert'))
672
673 if 1:
674         def conv (str):
675                 str = re.sub ('LyricVoice', 'LyricsVoice', str)
676                 # old fix
677                 str = re.sub ('Chord[Nn]ames*.Chord[Nn]ames*', 'ChordNames.ChordName', str)
678                 str = re.sub ('Chord[Nn]ames([ \t\n]+\\\\override)', 'ChordName\\1', str)
679                 return str
680         
681         conversions.append (((1,3,113), conv, 'LyricVoice -> LyricsVoice'))
682
683 def regularize_id (str):
684         s = ''
685         lastx = ''
686         for x in str:
687                 if x == '_':
688                         lastx = x
689                         continue
690                 elif x in string.digits:
691                         x = chr(ord (x) - ord ('0')  +ord ('A'))
692                 elif x not in string.letters:
693                         x = 'x'
694                 elif x in string.lowercase and lastx == '_':
695                         x = string.upper (x)
696                 s = s + x
697                 lastx = x
698         return s
699
700 if 1:
701         def conv (str):
702                 
703                 def regularize_dollar_reference (match):
704                         return regularize_id (match.group (1))
705                 def regularize_assignment (match):
706                         return '\n' + regularize_id (match.group (1)) + ' = '
707                 str = re.sub ('\$([^\t\n ]+)', regularize_dollar_reference, str)
708                 str = re.sub ('\n([^ \t\n]+) = ', regularize_assignment, str)
709                 return str
710         
711         conversions.append (((1,3,117), conv, 'identifier names: $!foo_bar_123 -> xfooBarABC'))
712
713
714 if 1:
715         def conv (str):
716                 def regularize_paper (match):
717                         return regularize_id (match.group (1))
718                 
719                 str = re.sub ('(paper_[a-z]+)', regularize_paper, str)
720                 str = re.sub ('sustainup', 'sustainUp', str)
721                 str = re.sub ('nobreak', 'noBreak', str)
722                 str = re.sub ('sustaindown', 'sustainDown', str)
723                 str = re.sub ('sostenutoup', 'sostenutoUp', str)
724                 str = re.sub ('sostenutodown', 'sostenutoDown', str)
725                 str = re.sub ('unachorda', 'unaChorda', str)
726                 str = re.sub ('trechorde', 'treChorde', str)
727         
728                 return str
729         
730         conversions.append (((1,3,120), conv, 'paper_xxx -> paperXxxx, pedalup -> pedalUp.'))
731
732 if 1:
733         def conv (str):
734                 str = re.sub ('drarnChords', 'chordChanges', str)
735                 str = re.sub ('\\musicalpitch', '\\pitch', str)
736                 return str
737         
738         conversions.append (((1,3,122), conv, 'drarnChords -> chordChanges, \\musicalpitch -> \\pitch'))
739
740 if 1:
741         def conv (str):
742                 str = re.sub ('ly-([sg])et-elt-property', 'ly-\\1et-grob-property', str)
743                 return str
744         
745         conversions.append (((1,3,136), conv, 'ly-X-elt-property -> ly-X-grob-property'))
746
747 if 1:
748         def conv (str):
749                 str = re.sub ('point-and-click +#t', 'point-and-click line-column-location', str)
750                 return str
751         
752         conversions.append (((1,3,138), conv, 'point-and-click argument changed to procedure.'))
753
754 if 1:
755         def conv (str):
756                 str = re.sub ('followThread', 'followVoice', str)
757                 str = re.sub ('Thread.FollowThread', 'Voice.VoiceFollower', str)
758                 str = re.sub ('FollowThread', 'VoiceFollower', str)
759                 return str
760         
761         conversions.append (((1,3,138), conv, 'followThread -> followVoice.'))
762
763 if 1:
764         def conv (str):
765                 str = re.sub ('font-point-size', 'font-design-size', str)
766                 return str
767         
768         conversions.append (((1,3,139), conv, 'font-point-size -> font-design-size.'))
769
770 if 1:
771         def conv (str):
772                 str = re.sub ('([a-zA-Z]*)NoDots', '\\1Solid', str)
773                 return str
774         
775         conversions.append (((1,3,141), conv, 'xNoDots -> xSolid'))
776
777 if 1:
778         def conv (str):
779                 str = re.sub ('([Cc])horda', '\\1orda', str)
780                 return str
781         
782         conversions.append (((1,3,144), conv, 'Chorda -> Corda'))
783
784
785 ############################
786         
787
788 def get_conversions (from_version, to_version):
789         def version_b (v, f = from_version, t = to_version):
790                 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
791         return filter (version_b, conversions)
792
793
794 def latest_version ():
795         return conversions[-1][0]
796
797 def do_conversion (infile, from_version, outfile, to_version):
798         conv_list = get_conversions (from_version, to_version)
799
800         sys.stderr.write ('Applying conversions: ')
801         str = infile.read ()
802         last_conversion = ()
803         try:
804                 for x in conv_list:
805                         sys.stderr.write (tup_to_str (x[0])  + ', ')
806                         str = x[1] (str)
807                         last_conversion = x[0]
808
809         except FatalConversionError:
810                 sys.stderr.write ('Error while converting; I won\'t convert any further')
811
812         if last_conversion:
813                 sys.stderr.write ('\n')
814                 new_ver =  '\\version \"%s\"' % tup_to_str (last_conversion)
815                 # JUNKME?
816                 # ugh: this all really doesn't help
817                 # esp. as current conversion rules are soo incomplete
818                 if re.search (lilypond_version_re_str, str):
819                         str = re.sub (lilypond_version_re_str,'\\'+new_ver , str)
820                 #else:
821                 #       str = new_ver + '\n' + str
822
823                 outfile.write(str)
824
825         return last_conversion
826         
827 class UnknownVersion:
828         pass
829
830 def do_one_file (infile_name):
831         sys.stderr.write ('Processing `%s\' ... '% infile_name)
832         outfile_name = ''
833         if __main__.edit:
834                 outfile_name = infile_name + '.NEW'
835         elif __main__.outfile_name:
836                 outfile_name = __main__.outfile_name
837
838         if __main__.from_version:
839                 from_version = __main__.from_version
840         else:
841                 guess = guess_lilypond_version (infile_name)
842                 if not guess:
843                         raise UnknownVersion()
844                 from_version = str_to_tuple (guess)
845
846         if __main__.to_version:
847                 to_version = __main__.to_version
848         else:
849                 to_version = latest_version ()
850
851
852         if infile_name:
853                 infile = open (infile_name,'r')
854         else:
855                 infile = sys.stdin
856
857         if outfile_name:
858                 outfile =  open (outfile_name, 'w')
859         else:
860                 outfile = sys.stdout
861
862         touched = do_conversion (infile, from_version, outfile, to_version)
863
864         if infile_name:
865                 infile.close ()
866
867         if outfile_name:
868                 outfile.close ()
869
870         if __main__.edit and touched:
871                 try:
872                         os.remove(infile_name + '~')
873                 except:
874                         pass
875                 os.rename (infile_name, infile_name + '~')
876                 os.rename (infile_name + '.NEW', infile_name)
877
878         sys.stderr.write ('\n')
879         sys.stderr.flush ()
880
881 edit = 0
882 assume_old = 0
883 to_version = ()
884 from_version = ()
885 outfile_name = ''
886
887 (options, files) = getopt.getopt (
888         sys.argv[1:], 'ao:f:t:seh', ['assume-old', 'version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to='])
889
890 for opt in options:
891         o = opt[0]
892         a = opt[1]
893         if o== '--help' or o == '-h':
894                 usage ()
895                 sys.exit (0)
896         if o == '--version' or o == '-v':
897                 print_version ()
898                 sys.exit (0)
899         elif o== '--from' or o=='-f':
900                 from_version = str_to_tuple (a)
901         elif o== '--to' or o=='-t':
902                 to_version = str_to_tuple (a)
903         elif o== '--edit' or o == '-e':
904                 edit = 1
905         elif o== '--show-rules' or o == '-s':
906                 show_rules (sys.stdout)
907                 sys.exit(0)
908         elif o == '--output' or o == '-o':
909                 outfile_name = a
910         elif o == '--assume-old' or o == '-a':
911                 assume_old = 1
912         else:
913                 print o
914                 raise getopt.error
915
916 identify ()
917 for f in files:
918         if f == '-':
919                 f = ''
920         try:
921                 do_one_file (f)
922         except UnknownVersion:
923                 sys.stderr.write ('\n')
924                 sys.stderr.write ("%s: can't determine version for %s" % (program_name, f))
925                 sys.stderr.write ('\n')
926                 if assume_old:
927                         fv = from_version
928                         from_version = (0,0,0)
929                         do_one_file (f)
930                         from_version = fv
931                 else:
932                         sys.stderr.write ("%s: skipping: %s " % (program_name,  f))
933                 pass
934 sys.stderr.write ('\n')