lilypond-0.1.14
[lilypond.git] / bin / mudela-book.pl
blob9aec75cb60a40c9ca6f870a8557e25b2b970b8ea
1 #!@PERL@ -w
2 # -*-Perl-*-
3 my $mudcount = 0;
4 my $mudela_b = 0;
5 my $outname = "-";
6 my $outdir;
7 use Getopt::Long;
10 # do something, check return status
11 sub my_system
13 my (@cmds) = @_;
14 foreach $cmd (@cmds) {
15 my ($ignoreret)=0;
16 if ( $cmd =~ /^-/ ) {
17 $ignoreret = 1;
18 $cmd = substr ($cmd, 1);
21 my $ret = ( system ($cmd));
22 if ($ret) {
23 if ($ignoreret) {
24 print STDERR "ignoring failed command \`$cmd\' (status $ret)\n";
25 }else {
26 print STDERR "\nmudela-book: failed on command \`$cmd\' (status $ret)\n";
27 exit 2;
32 sub gen_mufile
34 return "$outdir/$outname$mudcount.ly";
37 sub gen_texfile
39 return "$outdir/$outname$mudcount.tex";
42 sub close_mudela
44 $mudela_b = 0;
45 if ($fragment_b) {
46 print MUDELA "}\n \\paper { linewidth = -1.0\\cm; castingalgorithm = \\Wordwrap; } }\n";
47 $fragment_b =0;
49 if ( $verbatim_b) {
50 print BOOK "\\end{verbatim}\n\\interexample";
51 $verbatim_b =0;
53 close MUDELA;
54 my $status =0;
55 if ( -f gen_mufile ) {
56 $status = system "diff -q $outdir/book-mudela.ly " . gen_mufile;
57 } else {
58 $status = 1;
60 if ( $status ) {
61 rename "$outdir/book-mudela.ly", gen_mufile;
62 unlink gen_texfile;
65 if ( ! -f gen_texfile) {
66 my_system "lilypond ". gen_mufile;
67 rename "lelie.tex", gen_texfile;
69 print BOOK "\\preexample\\input " . gen_texfile . "\n\\postexample\n";
73 sub open_mudela
75 $mudcount++;
76 $mudela_b = 1 ;
77 open MUDELA, ">$outdir/book-mudela.ly";
78 if ($verbatim_b) {
79 print BOOK "\\begin{verbatim}\n";
81 if ($fragment_b) {
82 print MUDELA "\\score { \\melodic {";
87 sub begin_b
89 my ($s) = @_;
90 return (/^\\begin{$s}/) ;
93 sub end_b
95 my ($s) = @_;
96 return (/^\\end{$s}/) ;
98 sub parse_mudela_opts
100 my ($s) = @_;
101 $s =~ s/[\[\]]//g;
103 $verbatim_b =1 if ($s =~ /verbatim/ );
104 $fragment_b = 1 if ($s =~ /fragment/ );
107 sub help
109 print "usage: convert-mudela [options] [file]
110 options:
111 --help
112 --outdir=DIRECTORY write all files in directory DIRECTORY
113 --outname=NAME use NAME as base for the output
115 exit;
118 sub main
120 GetOptions( 'outdir=s', 'outname=s', 'help');
121 help if ( $opt_help ) ;
123 if (defined ($opt_outdir)) {
124 $outdir = $opt_outdir . "/";
125 } else {
126 $outdir = ".";
129 if (defined ($ARGV[0])) {
130 $infile = $ARGV[0] ;
131 } else {
132 $infile = "-";
134 if (defined ($opt_outname)) {
135 $outname = $opt_outname ;
136 } else {
137 die "Need to have an output name, use --outname" if ( $infile eq "-");
138 $outname = "$infile.tex";
141 my $openout ="$outdir$outname";
142 if ( $infile eq $openout ) {
143 die "The input can't be the output\n";
146 open INFILE, "<$infile";
147 open BOOK, ">$openout";
148 while (<INFILE>) {
149 if ($mudela_b) {
150 if (end_b "mudela") {
151 close_mudela;
152 next;
154 print MUDELA;
155 if ( $verbatim_b ) {
156 my $s = $_;
157 $s =~ s/\t/ /g; #shit
158 print BOOK $s;
161 } else {
162 if (/^\\begin(\[.*\])?{mudela}/ ) {
163 my $opts ="";
164 $opts = $1 if ( defined ($1));
166 parse_mudela_opts($opts);
167 open_mudela;
168 next;
170 print BOOK;
173 close INFILE;
174 close BOOK;
178 main;