lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Extracting parts from a .ly file?


From: Joseph Haig
Subject: Re: Extracting parts from a .ly file?
Date: Tue, 5 Dec 2006 13:27:23 +0000

On 04/12/06, Eduardo Vieira <address@hidden> wrote:

Hello users, does anybody have a Python or Perl script to extract parts from a
.ly file and save them into a different file?


It is not pretty and it doesn't to a great deal of syntax checking but
below is a perl script I've just thrown together which extract the
parts as you require.  It should pick out things like 'soprano' while
ignoring things like '\soprano'.  You should save it in a file called
ly-extract and call it with:

$ ly-extract infile.ly outfile.ly label

where label is soprano, alto, etc.

I don't know how perl works with Windows, but I would guess that you
would need to change the first line in some way.

Regards,

Joe

----------------------------
#!/usr/bin/perl

# ly-extract
#
# Extract a named section from a lilypond file

use strict;

my($fin,$fout,$label)address@hidden;
if (!$label) {
 print "Please supply input file, output file and label\n";
 exit 0;
}

open(FIN,'<',$fin) or die "Cannot find input file";
open(FOUT,'>',$fout) or die "Cannot open output file";

my($line);
my($notfound)=1;

# Find start of the required block
while($notfound and chomp($line=<FIN>)) {
 # This will match:
 #   $label
 #   $label =
 #   $label = {
 #   $label = { <stuff>
 # but it will ignore completely any mistakes, such as
 #   $label {
 #   $label = <stuff>
 if ($line =~ /((?<!\S)$label\s*(?:(=)\s*(?:({)(.*))?)?)$/) {
   $notfound=0;
   $line=$1; # Only use relevant part
 }
}

# This will now check to see if the opening { has been found
$notfound=1;

# Keep track of how many parentheses are open
# Counting { and << together
# Badly matched brackets will really mess this up
my($parcount)=0;
# Find the rest of the block and write it out to the output file
while ($line and ($notfound or $parcount!=0)) {
 if ($line=~/^(.*)({|}|<<|>>)(.*)/) {
   print FOUT $1;
   if ($2 eq '{' or $2 eq '<<') {
     $notfound=0;
     $parcount++;
     print FOUT $2;
   } elsif ($2 eq '}' or $2 eq '>>') {
     $parcount--;
     print FOUT $2;
     if ($parcount < 0) {
       die "Something has gone horribly wrong!";
     }
   }
   $line=$3;
 } else {
   chomp($line);
   print FOUT $line;
   $line='';
 }
 if (!$line) {
   print FOUT "\n";
   $line=<FIN>;
 }
}

close FIN;
close FOUT;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]