]> git.donarmstrong.com Git - lilypond.git/blob - bin/mudela-book.in
release: 0.1.1
[lilypond.git] / bin / mudela-book.in
1 #!@PERL@ -w             
2 # -*-Perl-*-
3 my $mudcount = 0;
4 my $mudela_b = 0;
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; castingalgorithm = \\Wordwrap; } }\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 help
84 {
85     print  "usage: convert-mudela [options] [file]
86 options: 
87 --help
88 --outdir=DIRECTORY      write all files in directory DIRECTORY
89 --outname=NAME          use NAME as base  for the output
90 ";
91     exit;
92 }
93     
94 sub main
95 {
96     GetOptions( 'outdir=s', 'outname=s', 'help');
97     help    if ( $opt_help ) ;
98
99     if  (defined ($opt_outdir)) {
100         $outdir = $opt_outdir .  "/";
101     } else {
102         $outdir = "";
103     }
104    
105     if (defined ($ARGV[0])) {
106         $infile = $ARGV[0] ;
107     } else {
108         $infile = "-";
109     }
110     if (defined ($opt_outname)) {
111         $outname = $opt_outname ;
112     } else { 
113         die "Need to have an output name, use --outname" if ( $infile == "-");
114         $outname = "$infile.tex";
115     }
116     
117     open INFILE, "<$infile";
118     open BOOK, ">$outdir$outname";
119     while (<INFILE>) {
120         if ($mudela_b) {
121             if (end_b "mudela") {
122                 close_mudela;
123                 next;
124             }
125             print MUDELA;
126             if ( $verbatim_b ) {
127                 my $s = $_;
128                 $s =~ s/\t/    /g; #shit
129                 print BOOK $s;
130             }
131             
132         } else {
133             if (/^\\begin(\[.*\])?{mudela}/ ) {
134                 my $opts ="";
135                 $opts = $1 if ( defined ($1));
136
137                 parse_mudela_opts($opts);
138                 open_mudela;
139                 next;  
140             } 
141             print BOOK;
142         }
143     }
144     close INFILE;
145     close BOOK;
146 }
147
148
149 main;