]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.pl
9aec75cb60a40c9ca6f870a8557e25b2b970b8ea
[lilypond.git] / bin / mudela-book.pl
1 #!@PERL@ -w             
2 # -*-Perl-*-
3 my $mudcount = 0;
4 my $mudela_b = 0;
5 my $outname = "-";
6 my $outdir;
7 use Getopt::Long;
8
9
10 # do something, check return status
11 sub my_system
12 {
13     my (@cmds) = @_;
14     foreach $cmd (@cmds) {
15         my ($ignoreret)=0;
16         if ( $cmd  =~ /^-/ ) {
17             $ignoreret = 1;
18             $cmd = substr ($cmd, 1);
19         }
20         
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;
28             }
29         }
30     }
31 }
32 sub gen_mufile
33 {
34     return "$outdir/$outname$mudcount.ly";
35 }
36
37 sub gen_texfile
38 {
39     return "$outdir/$outname$mudcount.tex";
40 }
41
42 sub close_mudela
43 {
44     $mudela_b = 0;
45     if ($fragment_b) {
46         print MUDELA "}\n \\paper { linewidth = -1.0\\cm; castingalgorithm = \\Wordwrap; } }\n";
47         $fragment_b =0;
48     }
49     if ( $verbatim_b)  {
50         print BOOK "\\end{verbatim}\n\\interexample";
51         $verbatim_b =0;
52     }
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;
59         }
60     if ( $status ) {
61         rename "$outdir/book-mudela.ly", gen_mufile;
62         unlink gen_texfile;
63     }
64     
65     if ( ! -f gen_texfile) {
66         my_system "lilypond ". gen_mufile;
67         rename "lelie.tex", gen_texfile;
68     }
69     print BOOK "\\preexample\\input " . gen_texfile . "\n\\postexample\n";
70         
71 }
72
73 sub open_mudela
74 {
75     $mudcount++;
76     $mudela_b = 1       ;
77     open MUDELA, ">$outdir/book-mudela.ly";
78     if ($verbatim_b) {
79         print BOOK "\\begin{verbatim}\n";
80     }
81     if ($fragment_b) {
82         print MUDELA "\\score { \\melodic {";
83     }
84
85 }
86
87 sub begin_b
88 {
89     my ($s) = @_;
90     return (/^\\begin{$s}/) ;    
91 }
92
93 sub end_b
94 {
95     my ($s) = @_;
96     return (/^\\end{$s}/) ;    
97 }
98 sub parse_mudela_opts
99 {
100    my ($s) = @_;
101    $s =~ s/[\[\]]//g;
102
103    $verbatim_b =1 if ($s =~ /verbatim/ );
104    $fragment_b = 1 if ($s =~ /fragment/ );
105 }   
106
107 sub help
108 {
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
114 ";
115     exit;
116 }
117     
118 sub main
119 {
120     GetOptions( 'outdir=s', 'outname=s', 'help');
121     help    if ( $opt_help ) ;
122
123     if  (defined ($opt_outdir)) {
124         $outdir = $opt_outdir .  "/";
125     } else {
126         $outdir = ".";
127     }
128    
129     if (defined ($ARGV[0])) {
130         $infile = $ARGV[0] ;
131     } else {
132         $infile = "-";
133     }
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";
139     }
140
141     my $openout ="$outdir$outname"; 
142     if  ( $infile eq $openout ) {
143         die "The input can't be the output\n";
144     }
145
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;
153             }
154             print MUDELA;
155             if ( $verbatim_b ) {
156                 my $s = $_;
157                 $s =~ s/\t/    /g; #shit
158                 print BOOK $s;
159             }
160             
161         } else {
162             if (/^\\begin(\[.*\])?{mudela}/ ) {
163                 my $opts ="";
164                 $opts = $1 if ( defined ($1));
165
166                 parse_mudela_opts($opts);
167                 open_mudela;
168                 next;  
169             } 
170             print BOOK;
171         }
172     }
173     close INFILE;
174     close BOOK;
175 }
176
177
178 main;