]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.in
release: 0.1.7
[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 sub gen_mufile
10 {
11     return "$outdir/$outname$mudcount.ly";
12 }
13
14 sub gen_texfile
15 {
16     return "$outdir/$outname$mudcount.tex";
17 }
18
19 sub close_mudela
20 {
21     $mudela_b = 0;
22     if ($fragment_b) {
23         print MUDELA "}\n \\paper { linewidth = -1.0\\cm; castingalgorithm = \\Wordwrap; } }\n";
24         $fragment_b =0;
25     }
26     if ( $verbatim_b)  {
27         print BOOK "\\end{verbatim}\n\\interexample";
28         $verbatim_b =0;
29     }
30     close MUDELA;
31     my $status =0;
32     if ( -f gen_mufile ) {
33         $status = system "diff -q $outdir/book-mudela.ly " . gen_mufile;
34     } else {
35         $status = 1;
36         }
37     if ( $status ) {
38         rename "$outdir/book-mudela.ly", gen_mufile;
39         unlink gen_texfile;
40     }
41     
42     if ( ! -f gen_texfile) {
43         system "lilypond ". gen_mufile;
44         rename "lelie.tex", gen_texfile;
45     }
46     print BOOK "\\preexample\\input " . gen_texfile . "\n\\postexample\n";
47         
48 }
49
50 sub open_mudela
51 {
52     $mudcount++;
53     $mudela_b = 1       ;
54     open MUDELA, ">$outdir/book-mudela.ly";
55     if ($verbatim_b) {
56         print BOOK "\\begin{verbatim}\n";
57     }
58     if ($fragment_b) {
59         print MUDELA "\\score { \\melodic {";
60     }
61
62 }
63
64 sub begin_b
65 {
66     my ($s) = @_;
67     return (/^\\begin{$s}/) ;    
68 }
69
70 sub end_b
71 {
72     my ($s) = @_;
73     return (/^\\end{$s}/) ;    
74 }
75 sub parse_mudela_opts
76 {
77    my ($s) = @_;
78    $s =~ s/[\[\]]//g;
79
80    $verbatim_b =1 if ($s =~ /verbatim/ );
81    $fragment_b = 1 if ($s =~ /fragment/ );
82 }   
83
84 sub help
85 {
86     print  "usage: convert-mudela [options] [file]
87 options: 
88 --help
89 --outdir=DIRECTORY      write all files in directory DIRECTORY
90 --outname=NAME          use NAME as base  for the output
91 ";
92     exit;
93 }
94     
95 sub main
96 {
97     GetOptions( 'outdir=s', 'outname=s', 'help');
98     help    if ( $opt_help ) ;
99
100     if  (defined ($opt_outdir)) {
101         $outdir = $opt_outdir .  "/";
102     } else {
103         $outdir = ".";
104     }
105    
106     if (defined ($ARGV[0])) {
107         $infile = $ARGV[0] ;
108     } else {
109         $infile = "-";
110     }
111     if (defined ($opt_outname)) {
112         $outname = $opt_outname ;
113     } else { 
114         die "Need to have an output name, use --outname" if ( $infile eq "-");
115         $outname = "$infile.tex";
116     }
117
118     my $openout ="$outdir$outname"; 
119     if  ( $infile eq $openout ) {
120         die "The input can't be the output\n";
121     }
122
123     open INFILE, "<$infile";
124     open BOOK, ">$openout";
125     while (<INFILE>) {
126         if ($mudela_b) {
127             if (end_b "mudela") {
128                 close_mudela;
129                 next;
130             }
131             print MUDELA;
132             if ( $verbatim_b ) {
133                 my $s = $_;
134                 $s =~ s/\t/    /g; #shit
135                 print BOOK $s;
136             }
137             
138         } else {
139             if (/^\\begin(\[.*\])?{mudela}/ ) {
140                 my $opts ="";
141                 $opts = $1 if ( defined ($1));
142
143                 parse_mudela_opts($opts);
144                 open_mudela;
145                 next;  
146             } 
147             print BOOK;
148         }
149     }
150     close INFILE;
151     close BOOK;
152 }
153
154
155 main;