]> git.donarmstrong.com Git - lilypond.git/blob - scripts/convert-ly.py
d35e379b41e1bdb257d2fe8df4065dc3b591ebb1
[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
529 # TODO: lots of other syntax change should be done here as well
530 if 1:
531         def conv (str):
532                 str = re.sub ('basicCollisionProperties', 'NoteCollision', str)
533                 str = re.sub ('basicVoltaSpannerProperties' , "VoltaBracket", str)
534                 str = re.sub ('basicKeyProperties' , "KeySignature", str)
535
536                 str = re.sub ('basicClefItemProperties' ,"Clef", str)
537
538
539                 str = re.sub ('basicLocalKeyProperties' ,"Accidentals", str)
540                 str = re.sub ('basicMarkProperties' ,"Accidentals", str)
541                 str = re.sub ('basic([A-Za-z_]+)Properties', '\\1', str)
542
543                 str = re.sub ('Repeat_engraver' ,'Volta_engraver', str)
544                 return str
545         
546         conversions.append (((1,3,92), conv, 'basicXXXProperties -> XXX, Repeat_engraver -> Volta_engraver'))
547
548 if 1:
549         def conv (str):
550                 # Ugh, but meaning of \stemup changed too
551                 # maybe we should do \stemup -> \stemUp\slurUp\tieUp ?
552                 str = re.sub ('\\\\stemup', '\\\\stemUp', str)
553                 str = re.sub ('\\\\stemdown', '\\\\stemDown', str)
554                 str = re.sub ('\\\\stemboth', '\\\\stemBoth', str)
555                 
556                 str = re.sub ('\\\\slurup', '\\\\slurUp', str)
557                 str = re.sub ('\\\\slurboth', '\\\\slurBoth', str)
558                 str = re.sub ('\\\\slurdown', '\\\\slurDown', str)
559                 str = re.sub ('\\\\slurdotted', '\\\\slurDotted', str)
560                 str = re.sub ('\\\\slurnormal', '\\\\slurNoDots', str)          
561                 
562                 str = re.sub ('\\\\shiftoff', '\\\\shiftOff', str)
563                 str = re.sub ('\\\\shifton', '\\\\shiftOn', str)
564                 str = re.sub ('\\\\shiftonn', '\\\\shiftOnn', str)
565                 str = re.sub ('\\\\shiftonnn', '\\\\shiftOnnn', str)
566
567                 str = re.sub ('\\\\onevoice', '\\\\oneVoice', str)
568                 str = re.sub ('\\\\voiceone', '\\\\voiceOne', str)
569                 str = re.sub ('\\\\voicetwo', '\\\\voiceTwo', str)
570                 str = re.sub ('\\\\voicethree', '\\\\voiceThree', str)
571                 str = re.sub ('\\\\voicefour', '\\\\voiceFour', str)
572
573                 # I don't know exactly when these happened...
574                 # ugh, we loose context setting here...
575                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\stemUp\\\\slurUp\\\\tieUp', str)
576                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\stemDown\\\\slurDown\\\\tieDown', str)
577                 str = re.sub ('\\\\property *[^ ]*verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\stemBoth\\\\slurBoth\\\\tieBoth', str)
578
579                 str = re.sub ('verticalDirection[^=]*= *#?"?(1|(\\\\up))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #1', str)
580                 str = re.sub ('verticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #-1', str)
581                 str = re.sub ('verticalDirection[^=]*= *#?"?(0|(\\\\center))"?', 'Stem \\\\override #\'direction = #0\nSlur \\\\override #\'direction = #0\n Tie \\\\override #\'direction = #0', str)
582                 
583                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\\\1Up', str)
584                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\\\1Down', str)
585                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\\\\\1Both', str)
586
587                 # (lacks capitalisation slur -> Slur)
588                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\1 \\\\override #\'direction = #1', str)
589                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?((-1)|(\\\\down))"?', '\\1 \\override #\'direction = #-1', str)
590                 str = re.sub ('([a-z]+)VerticalDirection[^=]*= *#?"?(0|(\\\\center))"?', '\\1 \\\\override #\'direction = #0', str)
591
592                 ## dynamic..
593                 str = re.sub ('\\\\property *[^ .]*[.]?dynamicDirection[^=]*= *#?"?(1|(\\\\up))"?', '\\\\dynamicUp', str)
594                 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?((-1)|(\\\\down))"?', '\\\\dynamicDown', str)
595                 str = re.sub ('\\\\property *[^ .]*[.]?dyn[^=]*= *#?"?(0|(\\\\center))"?', '\\\\dynamicBoth', str)
596
597                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?(0|(""))"?', '\\\\\\1NoDots', str)
598                 str = re.sub ('\\\\property *[^ .]*[.]?([a-z]+)Dash[^=]*= *#?"?([1-9]+)"?', '\\\\\\1Dotted', str)
599
600                 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?(0|(""))"?', '\\\\autoBeamOn', str)
601                 str = re.sub ('\\\\property *[^ .]*[.]?noAutoBeaming[^=]*= *#?"?([1-9]+)"?', '\\\\autoBeamOff', str)
602
603
604
605                 return str
606         
607         conversions.append (((1,3,93), conv,
608                 'property definiton case (eg. onevoice -> oneVoice)'))
609
610
611 if 1:
612         def conv (str):
613                 str = re.sub ('ChordNames*', 'ChordNames', str)
614                 if re.search ('\\\\textscript "[^"]* *"[^"]*"', str):
615                         sys.stderr.write ('\nNot smart enough to convert to new \\textscript markup text')
616
617                 str = re.sub ('\\textscript +("[^"]*")', '\\textscript #\\1', str)
618
619                 return str
620         
621         conversions.append (((1,3,97), conv, 'ChordName -> ChordNames'))
622
623
624 # TODO: add lots of these
625         
626 if 1:
627         def conv (str):
628                 str = re.sub ('\\\\property *"?Voice"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Voice.TextScript \\\\set #\'font-style = #\'\\1', str)
629                 str = re.sub ('\\\\property *"?Lyrics"? *[.] *"?textStyle"? *= *"([^"]*)"', '\\\\property Lyrics.LyricText \\\\set #\'font-style = #\'\\1', str)
630
631                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?timeSignatureStyle"? *= *"([^"]*)"', '\\\\property \\1.TimeSignature \\\\override #\'style = #\'\\2', str) 
632
633                 str = re.sub ('"?timeSignatureStyle"? *= *#?""', 'TimeSignature \\\\override #\'style = ##f', str)
634                 
635                 str = re.sub ('"?timeSignatureStyle"? *= *#?"([^"]*)"', 'TimeSignature \\\\override #\'style = #\'\\1', str)
636                 
637                 str = re.sub ('#\'style *= #*"([^"])"', '#\'style = #\'\\1', str)
638                 
639                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?horizontalNoteShift"? *= *"?#?([-0-9]+)"?', '\\\\property \\1.NoteColumn \\\\override #\'horizontal-shift = #\\2', str) 
640
641                 # ugh
642                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *""', '\\\\property \\1.Stem \\\\override #\'flag-style = ##f', str)
643                 
644                 str = re.sub ('\\\\property *"?([^.]+)"? *[.] *"?flagStyle"? *= *"([^"]*)"', '\\\\property \\1.Stem \\\\override #\'flag-style = #\'\\2', str) 
645                 return str
646         
647         conversions.append (((1,3,98), conv, 'CONTEXT.textStyle -> GROB.#font-style '))
648
649 if 1:
650         def conv (str):
651                 str = re.sub ('"?beamAutoEnd_([0-9]*)"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end 1 \\1 * *) = \\2', str)
652                 str = re.sub ('"?beamAutoBegin_([0-9]*)"? *= *(#\\([^)]*\))', 'autoBeamSettings \\push #\'(begin 1 \\1 * *) = \\2', str)
653                 str = re.sub ('"?beamAutoEnd"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(end * * * *) = \\1', str)
654                 str = re.sub ('"?beamAutoBegin"? *= *(#\\([^)]*\\))', 'autoBeamSettings \\push #\'(begin * * * *) = \\1', str)
655
656
657                 return str
658         
659         conversions.append (((1,3,102), conv, 'beamAutoEnd -> autoBeamSettings \\push (end * * * *)'))
660
661
662 if 1:
663         def conv (str):
664                 str = re.sub ('\\\\push', '\\\\override', str)
665                 str = re.sub ('\\\\pop', '\\\\revert', str)
666
667                 return str
668         
669         conversions.append (((1,3,111), conv, '\\push -> \\override, \\pop -> \\revert'))
670
671 if 1:
672         def conv (str):
673                 str = re.sub ('LyricVoice', 'LyricsVoice', str)
674                 # old fix
675                 str = re.sub ('Chord[Nn]ames*.Chord[Nn]ames*', 'ChordNames.ChordName', str)
676                 str = re.sub ('Chord[Nn]ames([ \t\n]+\\\\override)', 'ChordName\\1', str)
677                 return str
678         
679         conversions.append (((1,3,113), conv, 'LyricVoice -> LyricsVoice'))
680
681 def regularize_id (str):
682         s = ''
683         lastx = ''
684         for x in str:
685                 if x == '_':
686                         lastx = x
687                         continue
688                 elif x in string.digits:
689                         x = chr(ord (x) - ord ('0')  +ord ('A'))
690                 elif x not in string.letters:
691                         x = 'x'
692                 elif x in string.lowercase and lastx == '_':
693                         x = string.upper (x)
694                 s = s + x
695                 lastx = x
696         return s
697
698 if 1:
699         def conv (str):
700                 
701                 def regularize_dollar_reference (match):
702                         return regularize_id (match.group (1))
703                 def regularize_assignment (match):
704                         return '\n' + regularize_id (match.group (1)) + ' = '
705                 str = re.sub ('\$([^\t\n ]+)', regularize_dollar_reference, str)
706                 str = re.sub ('\n([^ \t\n]+) = ', regularize_assignment, str)
707                 return str
708         
709         conversions.append (((1,3,117), conv, 'identifier names: $!foo_bar_123 -> xfooBarABC'))
710
711
712 if 1:
713         def conv (str):
714                 def regularize_paper (match):
715                         return regularize_id (match.group (1))
716                 
717                 str = re.sub ('(paper_[a-z]+)', regularize_paper, str)
718                 str = re.sub ('sustainup', 'sustainUp', str)
719                 str = re.sub ('nobreak', 'noBreak', str)
720                 str = re.sub ('sustaindown', 'sustainDown', str)
721                 str = re.sub ('sostenutoup', 'sostenutoUp', str)
722                 str = re.sub ('sostenutodown', 'sostenutoDown', str)
723                 str = re.sub ('unachorda', 'unaChorda', str)
724                 str = re.sub ('trechorde', 'treChorde', str)
725         
726                 return str
727         
728         conversions.append (((1,3,120), conv, 'paper_xxx -> paperXxxx, pedalup -> pedalUp.'))
729
730 if 1:
731         def conv (str):
732                 str = re.sub ('drarnChords', 'chordChanges', str)
733                 str = re.sub ('\\musicalpitch', '\\pitch', str)
734                 return str
735         
736         conversions.append (((1,3,122), conv, 'drarnChords -> chordChanges, \\musicalpitch -> \\pitch'))
737
738 if 1:
739         def conv (str):
740                 str = re.sub ('ly-([sg])et-elt-property', 'ly-\\1et-grob-property', str)
741                 return str
742         
743         conversions.append (((1,3,136), conv, 'ly-X-elt-property -> ly-X-grob-property'))
744
745 if 1:
746         def conv (str):
747                 str = re.sub ('point-and-click +#t', 'point-and-click line-column-location', str)
748                 return str
749         
750         conversions.append (((1,3,138), conv, 'point-and-click argument changed to procedure.'))
751
752 if 1:
753         def conv (str):
754                 str = re.sub ('followThread', 'followVoice', str)
755                 str = re.sub ('Thread.FollowThread', 'Voice.VoiceFollower', str)
756                 str = re.sub ('FollowThread', 'VoiceFollower', str)
757                 return str
758         
759         conversions.append (((1,3,138), conv, 'followThread -> followVoice.'))
760
761 if 1:
762         def conv (str):
763                 str = re.sub ('font-point-size', 'font-design-size', str)
764                 return str
765         
766         conversions.append (((1,3,139), conv, 'font-point-size -> font-design-size.'))
767
768 if 1:
769         def conv (str):
770                 str = re.sub ('([a-zA-Z]*)NoDots', '\\1Solid', str)
771                 return str
772         
773         conversions.append (((1,3,141), conv, 'xNoDots -> xSolid'))
774
775 if 1:
776         def conv (str):
777                 str = re.sub ('([Cc])horda', '\\1orda', str)
778                 return str
779         
780         conversions.append (((1,3,144), conv, 'Chorda -> Corda'))
781
782
783 if 1:
784         def conv (str):
785                 str = re.sub ('([A-Za-z]+)MinimumVerticalExtent', 'MinimumV@rticalExtent', str)
786                 str = re.sub ('([A-Za-z]+)ExtraVerticalExtent', 'ExtraV@rticalExtent', str)
787                 str = re.sub ('([A-Za-z]+)VerticalExtent', 'VerticalExtent', str)
788                 str = re.sub ('ExtraV@rticalExtent', 'ExtraVerticalExtent', str)
789                 str = re.sub ('MinimumV@rticalExtent', 'MinimumVerticalExtent', str)            
790                 return str
791
792         conversions.append (((1,3,145), conv,
793         'ContextNameXxxxVerticalExtent -> XxxxVerticalExtent'))
794
795 ################################
796 #       END OF CONVERSIONS      
797 ################################
798
799 def get_conversions (from_version, to_version):
800         def version_b (v, f = from_version, t = to_version):
801                 return version_cmp (v[0], f) > 0 and version_cmp (v[0], t) <= 0
802         return filter (version_b, conversions)
803
804
805 def latest_version ():
806         return conversions[-1][0]
807
808 def do_conversion (infile, from_version, outfile, to_version):
809         conv_list = get_conversions (from_version, to_version)
810
811         sys.stderr.write ('Applying conversions: ')
812         str = infile.read ()
813         last_conversion = ()
814         try:
815                 for x in conv_list:
816                         sys.stderr.write (tup_to_str (x[0])  + ', ')
817                         str = x[1] (str)
818                         last_conversion = x[0]
819
820         except FatalConversionError:
821                 sys.stderr.write ('Error while converting; I won\'t convert any further')
822
823         if last_conversion:
824                 sys.stderr.write ('\n')
825                 new_ver =  '\\version \"%s\"' % tup_to_str (last_conversion)
826                 # JUNKME?
827                 # ugh: this all really doesn't help
828                 # esp. as current conversion rules are soo incomplete
829                 if re.search (lilypond_version_re_str, str):
830                         str = re.sub (lilypond_version_re_str,'\\'+new_ver , str)
831                 #else:
832                 #       str = new_ver + '\n' + str
833
834                 outfile.write(str)
835
836         return last_conversion
837         
838 class UnknownVersion:
839         pass
840
841 def do_one_file (infile_name):
842         sys.stderr.write ('Processing `%s\' ... '% infile_name)
843         outfile_name = ''
844         if __main__.edit:
845                 outfile_name = infile_name + '.NEW'
846         elif __main__.outfile_name:
847                 outfile_name = __main__.outfile_name
848
849         if __main__.from_version:
850                 from_version = __main__.from_version
851         else:
852                 guess = guess_lilypond_version (infile_name)
853                 if not guess:
854                         raise UnknownVersion()
855                 from_version = str_to_tuple (guess)
856
857         if __main__.to_version:
858                 to_version = __main__.to_version
859         else:
860                 to_version = latest_version ()
861
862
863         if infile_name:
864                 infile = open (infile_name,'r')
865         else:
866                 infile = sys.stdin
867
868         if outfile_name:
869                 outfile =  open (outfile_name, 'w')
870         else:
871                 outfile = sys.stdout
872
873         touched = do_conversion (infile, from_version, outfile, to_version)
874
875         if infile_name:
876                 infile.close ()
877
878         if outfile_name:
879                 outfile.close ()
880
881         if __main__.edit and touched:
882                 try:
883                         os.remove(infile_name + '~')
884                 except:
885                         pass
886                 os.rename (infile_name, infile_name + '~')
887                 os.rename (infile_name + '.NEW', infile_name)
888
889         sys.stderr.write ('\n')
890         sys.stderr.flush ()
891
892 edit = 0
893 assume_old = 0
894 to_version = ()
895 from_version = ()
896 outfile_name = ''
897
898 (options, files) = getopt.getopt (
899         sys.argv[1:], 'ao:f:t:seh', ['assume-old', 'version', 'output', 'show-rules', 'help', 'edit', 'from=', 'to='])
900
901 for opt in options:
902         o = opt[0]
903         a = opt[1]
904         if o== '--help' or o == '-h':
905                 usage ()
906                 sys.exit (0)
907         if o == '--version' or o == '-v':
908                 print_version ()
909                 sys.exit (0)
910         elif o== '--from' or o=='-f':
911                 from_version = str_to_tuple (a)
912         elif o== '--to' or o=='-t':
913                 to_version = str_to_tuple (a)
914         elif o== '--edit' or o == '-e':
915                 edit = 1
916         elif o== '--show-rules' or o == '-s':
917                 show_rules (sys.stdout)
918                 sys.exit(0)
919         elif o == '--output' or o == '-o':
920                 outfile_name = a
921         elif o == '--assume-old' or o == '-a':
922                 assume_old = 1
923         else:
924                 print o
925                 raise getopt.error
926
927 identify ()
928 for f in files:
929         if f == '-':
930                 f = ''
931         try:
932                 do_one_file (f)
933         except UnknownVersion:
934                 sys.stderr.write ('\n')
935                 sys.stderr.write ("%s: can't determine version for %s" % (program_name, f))
936                 sys.stderr.write ('\n')
937                 if assume_old:
938                         fv = from_version
939                         from_version = (0,0,0)
940                         do_one_file (f)
941                         from_version = fv
942                 else:
943                         sys.stderr.write ("%s: skipping: %s " % (program_name,  f))
944                 pass
945 sys.stderr.write ('\n')