-
Notifications
You must be signed in to change notification settings - Fork 7
/
bmcmdpp
executable file
·86 lines (79 loc) · 2.45 KB
/
bmcmdpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/perl
###############################################################################
# BMC MarkDown PreProcessor #
###############################################################################
use strict;
use warnings;
use Digest::SHA qw(sha1_hex);
use Getopt::Declare;
use URI::Escape;
my $args = Getopt::Declare->new(<<'EOARGS');
--bmc2ly <FILENAME> Path to bmc2ly [required]
EOARGS
die "bmc2ly not executable" unless -x $args->{'--bmc2ly'};
sub munge($);
print <<EOF;
<style>
.snippet-div {
text-align: center;
}
.snippet-span {
border: solid 1px;
text-align: center;
display: inline-block;
padding: 10px;
}
.brailled pre {
font-family: serif;
}
</style>
EOF
# Read MarkDown from STDIN
my $in_group = 0;
my $text = '';
while (<>) {
if (/^begin{(.*)}$/) { $in_group = 1; next; }
if (/^end{(.*)}$/) { munge($text); $in_group = 0; $text = ''; next; }
$text = $text . $_ if $in_group eq 1;
print $_ if $in_group eq 0;
}
exit 0;
sub munge($) {
my $text = shift;
my $out = "snippet_".sha1_hex($text);
unlink "$out.svg", "$out.midi", "$out.mp3";
open FILE, "|$args->{'--bmc2ly'} --no-tagline - >$out.ly"
or die "Unable to run bmc2ly";
print FILE $text;
close(FILE) or die "Error running bmc2ly: $!";
system("lilypond", "-d", "backend=svg", "-d", "preview", "-d", "no-print-pages", "-lWARNING", "$out.ly") == 0
or die "LilyPond failed";
die "No output from LilyPond" if ! -r "$out.preview.svg" or ! -r "$out.midi";
system("mv", "$out.preview.svg", "$out.svg") == 0
or die "Failed to rename SVG";
system("timidity", "-idqq", "-Ow", "$out.midi") == 0
or die "Timidity++ failed";
system("lame", "--silent", "-b", "64", "$out.wav", "$out.mp3") == 0
or die "Lame failed";
unlink "$out.wav" or warn "Could not unlink $out.wav: $!";
my $encoded = uri_escape($text);
print "<div class=\"snippet-div\">\n";
print " <span class=\"snippet-span\">\n";
print " <div class=\"brailled\"><pre>";
my $brl;
open FILE, "|brltty-trtxt -6 -i de -o unicode -";
print FILE $text;
close(FILE) or die "Error during brltty-trtxt execution: $!";
print "</pre></div>\n";
print <<EOHTML;
<div class="engraved">
<a href="$out.ly\">
<img src="$out.svg" alt="LilyPond" title="Click to see LilyPond source"/>
</a>
</div>
<a href="$out.midi">MIDI</a> | <a href="$out.mp3">MP3</a>
| <a href="http://bmc.delysid.org/?table=de&music=$encoded">Edit</a>
</span>
</div>
EOHTML
}