]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.in
partial: 0.1.57.jcn
[lilypond.git] / bin / mudela-book.in
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     }
101     if ($verbatim_b) {
102         print BOOK "\\begin{verbatim}\n";
103     }
104     if ($fragment_b) {
105         print MUDELA "\\score { \\melodic { ";
106     } else {
107         print MUDELA "default_paper = \\paper { \\paper_sixteen ";
108         print MUDELA "linewidth = 7.\\cm;}";
109     }
110
111 }
112
113 sub begin_b
114 {
115     my ($s) = @_;
116     return (/^\\begin{$s}/) ;    
117 }
118
119 sub end_b
120 {
121     my ($s) = @_;
122     return (/^\\end{$s}/) ;    
123 }
124 sub parse_mudela_opts
125 {
126    my ($s) = @_;
127    $s =~ s/[\[\]]//g;
128
129    $verbatim_b =1 if ($s =~ /verbatim/ );
130    $fragment_b = 1 if ($s =~ /fragment/ );
131    $center_b = 1 if ($s =~ /center/ );
132 }   
133
134 sub help
135 {
136     print  "usage: convert-mudela [options] [file]
137 options: 
138 --help
139 --outdir=DIRECTORY      write all files in directory DIRECTORY
140 --outname=NAME          use NAME as base  for the output
141 ";
142     exit;
143 }
144     
145 sub main
146 {
147     GetOptions( 'outdir=s', 'outname=s', 'help');
148     if ( $opt_help ) {
149         help();
150         $opt_help = 0;  # to extinguish typo check. brr, what a language
151     }
152
153     if  (defined ($opt_outdir)) {
154         $outdir = $opt_outdir .  "/";
155     } else {
156         $outdir = ".";
157     }
158    
159     if (defined ($ARGV[0])) {
160         $infile = $ARGV[0] ;
161     } else {
162         $infile = "-";
163     }
164     if (defined ($opt_outname)) {
165         $outname = $opt_outname ;
166     } else { 
167         die "Need to have an output name, use --outname" if ( $infile eq "-");
168         $outname = "$infile.tex";
169     }
170
171     my $openout ="$outdir$outname"; 
172     if  ( $infile eq $openout ) {
173         die "The input can't be the output\n";
174     }
175
176     open INFILE, "<$infile";
177     open BOOK, ">$openout";
178     while (<INFILE>) {
179         if ($mudela_b) {
180             if (end_b "mudela") {
181                 close_mudela;
182                 next;
183             }
184             print MUDELA;
185             if ( $verbatim_b ) {
186                 my $s = $_;
187                 $s =~ s/\t/    /g; #shit
188                 print BOOK $s;
189             }
190             
191         } else {
192             if (/^\\begin(\[.*\])?{mudela}/ ) {
193                 my $opts ="";
194                 $opts = $1 if ( defined ($1));
195
196                 parse_mudela_opts($opts);
197                 open_mudela;
198                 next;  
199             } 
200             print BOOK;
201         }
202     }
203     close INFILE;
204     close BOOK;
205 }
206
207
208 main;