lilypond-0.1.56
[lilypond.git] / bin / mudela-book.pl
blobf23b428b52da0c30c3c96120be5737871b93705b
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_texbase
39 return "$outname$mudcount.tex";
41 sub gen_texfile
43 return "$outdir/" . gen_texbase;
46 sub close_mudela
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;
54 if ($verbatim_b) {
55 print BOOK "\\end{verbatim}";
57 if ($center_b) {
58 print BOOK "\\end{minipage}";
60 if ($verbatim_b) {
61 print BOOK "\\interexample";
62 $verbatim_b = 0;
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;
71 if ( $status ) {
72 rename "$outdir/book-mudela.ly", gen_mufile;
73 unlink gen_texfile;
76 if ( ! -f gen_texfile) {
77 my_system "lilypond ". gen_mufile;
78 rename gen_texbase, gen_texfile;
81 if ($center_b) {
82 print BOOK "\\begin{minipage}[c]{.5\\textwidth}\n";
84 print BOOK "\\input " . gen_texfile . "%\n";
85 if ($center_b) {
86 print BOOK "\\end{minipage}";
87 $center_b = 0;
89 print BOOK "\\postexample%\n";
92 sub open_mudela
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";
101 if ($verbatim_b) {
102 print BOOK "\\begin{verbatim}\n";
104 if ($fragment_b) {
105 print MUDELA "\\score { \\melodic {\\octave c';";
106 } else {
107 print MUDELA "default_paper = \\paper { \\paper_sixteen ";
108 print MUDELA "linewidth = 7.\\cm;}";
113 sub begin_b
115 my ($s) = @_;
116 return (/^\\begin{$s}/) ;
119 sub end_b
121 my ($s) = @_;
122 return (/^\\end{$s}/) ;
124 sub parse_mudela_opts
126 my ($s) = @_;
127 $s =~ s/[\[\]]//g;
129 $verbatim_b =1 if ($s =~ /verbatim/ );
130 $fragment_b = 1 if ($s =~ /fragment/ );
131 $center_b = 1 if ($s =~ /center/ );
134 sub help
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
142 exit;
145 sub main
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
153 if (defined ($opt_outdir)) {
154 $outdir = $opt_outdir . "/";
155 } else {
156 $outdir = ".";
159 if (defined ($ARGV[0])) {
160 $infile = $ARGV[0] ;
161 } else {
162 $infile = "-";
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";
171 my $openout ="$outdir$outname";
172 if ( $infile eq $openout ) {
173 die "The input can't be the output\n";
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;
184 print MUDELA;
185 if ( $verbatim_b ) {
186 my $s = $_;
187 $s =~ s/\t/ /g; #shit
188 print BOOK $s;
191 } else {
192 if (/^\\begin(\[.*\])?{mudela}/ ) {
193 my $opts ="";
194 $opts = $1 if ( defined ($1));
196 parse_mudela_opts($opts);
197 open_mudela;
198 next;
200 print BOOK;
203 close INFILE;
204 close BOOK;
208 main;