lilypond-1.5.13
[lilypond.git] / bin / convert-mudela.py
blob9654db93779f98af355f7916f5863c91619a018e
1 #!@PYTHON@ -w
2 # -*-perl-*-
4 version = "0.2";
6 ################################################################
8 sub no_conv
12 sub convert_0_0_52_to_0_0_53
15 s/include \"/$1\\include \"/g;
19 sub convert_0_0_54_to_0_0_55
21 s/%{/% {/g;
25 sub convert_0_0_53_to_0_0_54
27 print STDERR "Not smart enough to convert \\transpose\n" if (/\\transpose/) ;
30 # we-re not at 58 yet, but this is at least one of the rules
31 sub convert_0_0_55_to_0_0_56
33 s/\"\|\|\"/\"|.\"/g;
36 sub convert_0_0_56_to_0_0_57
38 s/\(([ \]\[|\t-\.>]|\\[<!>a-z]+)*\)/\~ $1/g;
41 sub convert_0_0_57_to_0_0_58
43 s/\[ *([^\[\]]*)\] *([1-9]*) *\/ *([1-9]*)/[$2\/$3 $1]1\/1/g;
46 sub convert_0_0_58_to_0_0_59
48 die "Not smart enough to convert 0.0.58 to 0.0.59\n";
51 sub convert_0_0_59_to_0_0_60
53 s/(\\unitspace [0-9.mcptin\\ ]+|\\geometric [0-9.]+|\\width [0-9.mcp\\tin]+)/$1;/g;
54 s/(\\output \"[^\"]+\")/$1;/;
55 s/(\\tempo [0-9: ]+)/$1;/;
58 sub convert_0_0_60_to_0_0_61
60 s/(\\unitspace|\\geometric|\\width)/$1=/g;
64 sub convert_0_1_0_to_0_1_1
66 s/\\tempo (.*):(.*);/\\tempo $1 = $2;/g
69 sub convert_0_1_2_to_0_1_3
71 s/\\stem *(\\up|1) *;/\\stemup/g;
72 s/\\stem *(\\down|-1) *;/\\stemdown/g;
73 s/\\stem *0 *;/\\stemboth/g;
74 s/\\hshift ([^;]+) *;/\\property Voice.hshift = $1/g;
77 my $header_b = 0;
79 sub generic_conversion_scan
81 if (/\\header *\{/)
83 $header_b = 1;
85 if ($header_b && /^ *\}/)
87 $header_b = 0;
90 sub convert_0_1_4_to_0_1_5
92 s/([<{]) *\\id "Piano" (.+);/\\type Grandstaff = $3 $1/;
93 s/([<{]) *\\id (.+) (.+);/\\type $2 = $3 $1/;
97 sub convert_0_1_5_to_0_1_6
99 s/< *\\multi (.*);/\\multi $1 </;
102 sub convert_0_1_6_to_0_1_7
104 if ($header_b)
106 s/^([a-zA-z]+)[ \t]+(.*)$/$1 =\t \"$2\";/;
107 s/^([ \t])+(.*)$/$1 \"$2\";/;
111 sub convert_0_1_7_to_0_1_8
113 s/\\plet *1 *\/ *1 *;/\\]/;
114 s/\\plet *([1-9][0-9]*) *\/ *([2-9][0-9]*) *;/\\[$1\/$2/;
117 sub convert_0_1_8_to_0_1_9
119 # sticky plet shorthand...
120 # print "introduced plet and finger shorthands...\n";
123 ###############################################################
125 sub last_conversion
127 my @v = &versions;
128 return pop @v;
130 sub identify
133 print STDERR "This is convert-mudela " . $convert_mudela_version .
134 " (up to mudela version ", last_conversion, ")\n";
138 sub usage
140 print STDERR "Usage: convert-mudela [options] [mudela-file]...\n"
141 . "Convert old mudela source from mudela-file or stdin\n\n"
142 . "Options:\n"
143 . " -e, --edit perform in-place conversion\n"
144 . " -f, --from=PATHLEVEL use source version 0.0.PATCHLEVEL\n"
145 . " -h, --help print this help\n"
146 . " -o, --output=FILE name output file\n"
147 . " -s, --show-rules print all known conversion rules\n"
148 . " -t, --to=VERSION convert to version VERSION\n"
152 my %minor_conversions = ("0.0.50" => \&no_conv,
153 "0.0.52" => \&convert_0_0_50_to_0_0_52,
154 "0.0.53" => \&convert_0_0_52_to_0_0_53,
155 "0.0.54" => \&convert_0_0_53_to_0_0_54,
156 "0.0.55" => \&convert_0_0_54_to_0_0_55,
157 "0.0.56" => \&convert_0_0_55_to_0_0_56,
158 "0.0.57" => \&convert_0_0_56_to_0_0_57,
159 "0.0.58" => \&convert_0_0_57_to_0_0_58,
160 "0.0.59" => \&convert_0_0_58_to_0_0_59,
161 "0.0.60" => \&convert_0_0_59_to_0_0_60,
162 "0.0.61" => \&convert_0_0_60_to_0_0_61,
163 "0.1.1" => \&convert_0_1_0_to_0_1_1,
164 "0.1.2" => \&no_conv,
165 "0.1.3" => \&convert_0_1_2_to_0_1_3,
166 "0.1.4" => \&no_conv,
167 "0.1.5" => \&convert_0_1_4_to_0_1_5,
168 "0.1.6" => \&convert_0_1_5_to_0_1_6
169 ,"0.1.7" => \&convert_0_1_6_to_0_1_7
170 ,"0.1.8" => \&convert_0_1_7_to_0_1_8
171 ,"0.1.9" => \&convert_0_1_8_to_0_1_9
176 sub versions
178 return (sort { cmpver; } (keys %minor_conversions));
182 sub show_rules
184 my (@v) = versions;
186 print "Rules: ", join(", ", @v), "\n";
190 sub do_conversion
192 my ($from,$to) = @_;
194 my @applicable_conversion;
195 my @mudela_levels;
197 my @v = versions;
198 foreach $ver (@v) {
199 if (version_compare($ver, $from) > 0 && version_compare($ver,$to) <= 0 ){
200 push @applicable_conversion, $minor_conversions{$ver};
201 push @mudela_levels, $ver;
205 print STDERR "Applying following rules: ", join(", ", @mudela_levels) , "\n";
207 while (<INLY>) {
208 generic_conversion_scan;
209 foreach $subroutine (@applicable_conversion) {
211 &$subroutine;
214 version_string_conv $from, $to;
215 print OUTLY;
219 sub get_auto_from
221 my ($fn)=@_;
222 my ($ver);
223 open INLY, $fn || die "Can't open";
225 while (<INLY>) {
226 s/^.*\\version \"([^\"]*)\".*$//;
227 if (defined ($1)) {
228 print STDERR "Guessing version: ", $1, ".. ";
229 $ver = $1;
230 last;
233 if (!defined($ver)){
234 print STDERR "can't determine mudela version in $fn.\n";
235 my $u;
236 return $u;
238 close INLY;
239 return $ver;
242 sub set_files
244 $infile = "-";
245 $outfile = "-";
246 $outfile = $opt_output if (defined($opt_output));
248 if ($ARGV [0]) {
249 $infile = $ARGV[0];
251 if (!(-f $infile) && !($infile =~ /\.ly$/s)) {
252 $infile .= ".ly";
254 if ($opt_edit && $infile ne "-") {
255 $opt_edit = 1;
256 $outfile = "$infile.NEW";
257 $infile = "$infile";
259 print STDERR "Input ", (($infile eq "-") ?"STDIN" : $infile), " .. ";
263 sub do_one_arg
265 set_files;
267 local ($from_version, $to_version);
268 $from_version = $opt_from;
269 $to_version = $opt_to;
271 ($from_version = get_auto_from $infile) unless defined($opt_from);
272 return if (!defined($from_version));
274 ($to_version = last_conversion) unless (defined($opt_to));
276 die "can't open \`$infile\'" unless open INLY,$infile ;
277 die "can't open \`$outfile\'" unless open OUTLY, ">$outfile";
279 do_conversion $from_version, $to_version;
280 close INLY;
281 close OUTLY;
283 if ($opt_edit) {
284 rename $infile, "$infile~";
285 rename $outfile, "$infile";
289 ## "main"
291 identify;
294 GetOptions ("help", "output=s", "from=i", "to=i", "minor=i", "edit", "show-rules");
296 if ($opt_help) {
297 usage();
298 $opt_help = 0; # to extinguish typo check.
299 exit 0;
302 if ($opt_show_rules) {
303 show_rules ;
304 $opt_show_rules = 0; # to extinguish typo check.
305 exit 0;
308 local ( $infile,$outfile);
309 my $processed_one=0;
311 while (defined($ARGV[0])) {
312 do_one_arg;
313 shift @ARGV;
314 $processed_one = 1;
316 do_one_arg unless ($processed_one);