]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.pl
release: 0.1.57
[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_texbase
38 {
39     return "$outname$mudcount.tex";
40 }
41 sub gen_texfile
42 {
43     return "$outdir/" . gen_texbase;
44 }
45
46 sub close_mudela
47 {
48     $mudela_b = 0;
49     if ($fragment_b) {
50         print MUDELA "}\n \\paper { linewidth = -1.0\\cm;";
51         print MUDELA "castingalgorithm = \\Wordwrap; } }\n";
52         $fragment_b =0;
53     }
54     if ($verbatim_b)  {
55         print BOOK "\\end{verbatim}";
56     }
57     if ($center_b) {
58         print BOOK "\\end{minipage}";
59     }
60     if ($verbatim_b)  {
61         print BOOK "\\interexample";
62         $verbatim_b = 0;
63     }
64     close MUDELA;
65     my $status =0;
66     if ( -f gen_mufile ) {
67         $status = system "diff -q $outdir/book-mudela.ly " . gen_mufile;
68     } else {
69         $status = 1;
70         }
71     if ( $status ) {
72         rename "$outdir/book-mudela.ly", gen_mufile;
73         unlink gen_texfile;
74     }
75     
76     if ( ! -f gen_texfile) {
77         my_system "lilypond ". gen_mufile;
78         rename gen_texbase, gen_texfile;
79     }
80
81     if ($center_b) {
82         print BOOK "\\begin{minipage}[c]{.5\\textwidth}\n";
83     }
84     print BOOK "\\input " . gen_texfile . "\n";
85     if ($center_b) {
86         print BOOK "\\end{minipage}";
87         $center_b = 0;
88     }
89     print BOOK "\\postexample%\n";
90 }
91
92 sub open_mudela
93 {
94     $mudcount++;
95     $mudela_b = 1       ;
96     open MUDELA, ">$outdir/book-mudela.ly";
97     print BOOK "\\preexample%\n";
98     if ($center_b) {
99         print BOOK "\\begin{minipage}[c]{.5\\textwidth}\n";
100         print MUDELA "default_paper = \\paper { \\paper_sixteen ";
101         print MUDELA "linewidth = 7.\\cm;}";
102     }
103     if ($verbatim_b) {
104         print BOOK "\\begin{verbatim}\n";
105     }
106     if ($fragment_b) {
107         print MUDELA "\\score { \\melodic { ";
108     } else {
109         print MUDELA "default_paper = \\paper { \\paper_sixteen ";
110         print MUDELA "linewidth = 15.\\cm;}";
111
112     }
113
114 }
115
116 sub begin_b
117 {
118     my ($s) = @_;
119     return (/^\\begin{$s}/) ;    
120 }
121
122 sub end_b
123 {
124     my ($s) = @_;
125     return (/^\\end{$s}/) ;    
126 }
127 sub parse_mudela_opts
128 {
129    my ($s) = @_;
130    $s =~ s/[\[\]]//g;
131
132    $verbatim_b =1 if ($s =~ /verbatim/ );
133    $fragment_b = 1 if ($s =~ /fragment/ );
134    $center_b = 1 if ($s =~ /center/ );
135 }   
136
137 sub help
138 {
139     print  "usage: convert-mudela [options] [file]
140 options: 
141 --help
142 --outdir=DIRECTORY      write all files in directory DIRECTORY
143 --outname=NAME          use NAME as base  for the output
144 ";
145     exit;
146 }
147     
148 sub main
149 {
150     GetOptions( 'outdir=s', 'outname=s', 'help');
151     if ( $opt_help ) {
152         help();
153         $opt_help = 0;  # to extinguish typo check. brr, what a language
154     }
155
156     if  (defined ($opt_outdir)) {
157         $outdir = $opt_outdir .  "/";
158     } else {
159         $outdir = ".";
160     }
161    
162     if (defined ($ARGV[0])) {
163         $infile = $ARGV[0] ;
164     } else {
165         $infile = "-";
166     }
167     if (defined ($opt_outname)) {
168         $outname = $opt_outname ;
169     } else { 
170         die "Need to have an output name, use --outname" if ( $infile eq "-");
171         $outname = "$infile.tex";
172     }
173
174     my $openout ="$outdir$outname"; 
175     if  ( $infile eq $openout ) {
176         die "The input can't be the output\n";
177     }
178
179     open INFILE, "<$infile";
180     open BOOK, ">$openout";
181     while (<INFILE>) {
182         if ($mudela_b) {
183             if (end_b "mudela") {
184                 close_mudela;
185                 next;
186             }
187             print MUDELA;
188             if ( $verbatim_b ) {
189                 my $s = $_;
190                 $s =~ s/\t/    /g; #shit
191                 print BOOK $s;
192             }
193             
194         } else {
195             if (/^\\begin(\[.*\])?{mudela}/ ) {
196                 my $opts ="";
197                 $opts = $1 if ( defined ($1));
198
199                 parse_mudela_opts($opts);
200                 open_mudela;
201                 next;  
202             } 
203             print BOOK;
204         }
205     }
206     close INFILE;
207     close BOOK;
208 }
209
210
211 main;