]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.in
release: 0.0.78
[lilypond.git] / bin / mudela-book.in
1 #!@PERL@ -w
2 my $mudcount = 0;
3 my $mudela_b = 0;
4 my $outdir = "";
5 my $outname = "";
6 use Getopt::Long;
7
8 sub gen_mufile
9 {
10     return "$outdir/$outname$mudcount.ly";
11 }
12
13 sub gen_texfile
14 {
15     return "$outdir/$outname$mudcount.tex";
16 }
17
18 sub close_mudela
19 {
20     $mudela_b = 0;
21     if ($fragment_b) {
22         print MUDELA "}\n \\paper { linewidth = -1.0\\cm; } }\n";
23         $fragment_b =0;
24     }
25     if ( $verbatim_b)  {
26         print BOOK "\\end{verbatim}\n\\interexample";
27         $verbatim_b =0;
28     }
29     close MUDELA;
30     my $status =0;
31     if ( -f gen_mufile ) {
32         $status = system "diff -q $outdir/book-mudela.ly " . gen_mufile;
33     } else {
34         $status = 1;
35         }
36     if ( $status ) {
37         rename "$outdir/book-mudela.ly", gen_mufile;
38         unlink gen_texfile;
39     }
40     
41     if ( ! -f gen_texfile) {
42         system "lilypond ". gen_mufile;
43         rename "lelie.tex", gen_texfile;
44     }
45     print BOOK "\\preexample\\input " . gen_texfile . "\n\\postexample\n";
46         
47 }
48
49 sub open_mudela
50 {
51     $mudcount++;
52     $mudela_b = 1       ;
53     open MUDELA, ">$outdir/book-mudela.ly";
54     if ($verbatim_b) {
55         print BOOK "\\begin{verbatim}\n";
56     }
57     if ($fragment_b) {
58         print MUDELA "\\score { \\melodic {";
59     }
60
61 }
62
63 sub begin_b
64 {
65     my ($s) = @_;
66     return (/^\\begin{$s}/) ;    
67 }
68
69 sub end_b
70 {
71     my ($s) = @_;
72     return (/^\\end{$s}/) ;    
73 }
74 sub parse_mudela_opts
75 {
76    my ($s) = @_;
77    $s =~ s/[\[\]]//g;
78
79    $verbatim_b =1 if ($s =~ /verbatim/ );
80    $fragment_b = 1 if ($s =~ /fragment/ );
81 }   
82
83 sub main
84 {
85     GetOptions( 'outdir=s', 'outname=s');
86     $outdir = $opt_outdir;
87     $outname = $opt_outname if (defined ($opt_outname) && $opt_outname);
88     open INFILE, $ARGV[0];
89     
90     open BOOK, ">$outdir/$outname";
91     while (<INFILE>) {
92         if ($mudela_b) {
93             if (end_b "mudela") {
94                 close_mudela;
95                 next;
96             }
97             print MUDELA;
98             if ( $verbatim_b ) {
99                 my $s = $_;
100                 $s =~ s/\t/    /g; #shit
101                 print BOOK $s;
102             }
103             
104         } else {
105             if (/^\\begin(\[.*\])?{mudela}/ ) {
106                 my $opts ="";
107                 $opts = $1 if ( defined ($1));
108
109                 parse_mudela_opts($opts);
110                 open_mudela;
111                 next;  
112             } 
113             print BOOK;
114         }
115     }
116     close INFILE;
117     close BOOK;
118 }
119
120 main;