3 # Web Chord v1.1 - A CGI script to convert a ChordPro file to HTML
4 # Copyright 1998-2003 Martin Vilcans (martin@mamaviol.org)
7 # chopro - This parameter can be submitted by a form as a text field or file
11 # 1998-07-20 Version 1.0
12 # 2003-08-03 Version 1.1 Uses stylesheets
13 # 2014-02-05 Added things particular to my implementation of Songbook at
14 # http://defaria.com/songbook
19 use CGI qw(:standard);
20 use CGI::Carp qw (fatalsToBrowser);
23 my ($chopro, $output, $i);
25 my $documentRoot = "/web";
26 my $debug = param ('debug');
27 my $infile = param ('chordpro');
35 print "<font color=red><b>Debug:</b></font> $msg<br>";
45 print "<font color=orange><b>Warning</b></font> $msg<br>";
53 print "<html><head><title>Web Chord: Error</title></head>" .
54 "<body><h1>Error</h1><p>\n$msg\n</p>" .
60 sub musicFileExists ($) {
63 debug "ENTER musicFileExists ($song)";
65 my $title = fileparse ($song, qr/\.pro/);
66 my $musicfile = "/songbook/Media/$title.mp3";
68 if (-r "$documentRoot$musicfile") {
73 debug "Could not find $documentRoot$musicfile";
79 sub updateMusicpath ($$) {
80 my ($chopro, $song) = @_;
82 my $title = musicFileExists $song;
84 # If there's no corresponding music file then do nothing
87 # If the .pro file already has musicpath then do nothing
88 if ($chopro =~ /\{musicpath:.*\}/) {
89 debug "$song already has musicpath";
92 return if $chopro =~ /\{musicpath:.*\}/;
94 # Otherwise append the musicpath
97 open $songfile, '>>', $song
100 unless (defined $songfile) {
101 my $msg = "Unable to open $song for append - $!<br>";
102 $msg .= "<br>Please notify <a href=\"mailto:adefaria\@gmail.com?subject=Please chmod 666 $song\">Andrew DeFaria</a> so this can be corrected.<br>";
103 $msg .= "<br>Thanks";
109 my $songbase = '/sdcard';
110 $songbase = '/storage/emulated/0';
112 print $songfile "{musicpath:$songbase/SongBook/Media/$title.mp3}\n";
119 # Outputs the HTML code of the chordpro file in the first parameter
120 sub chopro2html ($$) {
121 my ($chopro, $song) = @_;
123 $chopro =~ s/\</\</g; # replace < with <
124 $chopro =~ s/\>/\>/g; # replace > with >
125 $chopro =~ s/\&/\&/g; # replace & with &
129 if(($chopro =~ /^{title:(.*)}/mi) || ($chopro =~ /^{t:(.*)}/mi)) {
132 $title = "ChordPro song";
137 <title>$title</title>
138 <style type="text/css">
140 background-image: url('/songbook/background.jpg');
145 font-family: Arial, Helvetica;
151 font-family: Arial, Helvetica;
155 .lyrics, .lyrics_chorus {
158 .lyrics_tab, .lyrics_chorus_tab {
159 font-family: "Courier New", Courier;
162 .lyrics_chorus, .lyrics_chorus_tab, .chords_chorus, .chords_chorus_tab {
165 .chords, .chords_chorus, .chords_tab, .chords_chorus_tab {
170 .comment, .comment_italic {
175 background-color: #ffbbaa;
189 $title = musicFileExists $song;
192 updateMusicpath $chopro, $song;
196 <table border="0" width="100%">
199 <td align="left"><a href="/songbook"><img src="/Icons/Home.png" alt="Home"></a></td>
205 <audio controls autoplay>
206 <source src="http://defaria.com/songbook/Media/$title.mp3" type='audio/mp3'>
207 <p>Your user agent does not support the HTML5 Audio element.</p>
217 my $mode = 0; # mode defines which class to use
220 # normal chorus normal+tab chorus+tab
221 my @lClasses = ('lyrics', 'lyrics_chorus', 'lyrics_tab', 'lyrics_chorus_tab' );
222 my @cClasses = ('chords', 'chords_chorus', 'chords_tab', 'chords_chorus_tab' );
224 while($chopro ne '') {
225 $chopro =~ s/(.*)\n?//; # extract and remove first line
229 if(/^#(.*)/) { # a line starting with # is a comment
230 print "<!--$1-->\n"; # insert as HTML comment
231 } elsif(/{(.*)}/) { # this is a command
233 if(/^title:/i || /^t:/i) { # title
234 print "<H1>$'</H1>\n";
235 } elsif(/^subtitle:/i || /^st:/i) { # subtitle
236 print "<H2>$'</H2>\n";
237 } elsif(/^start_of_chorus/i || /^soc/i) { # start_of_chorus
239 } elsif(/^end_of_chorus/i || /^eoc/i) { # end_of_chorus
241 } elsif(/^comment:/i || /^c:/i) { # comment
242 print "<span class=\"comment\">($')</span>\n";
243 } elsif(/^comment_italic:/i || /^ci:/i) { # comment_italic
244 print "<span class=\"comment_italic\">($')</span>\n";
245 } elsif(/^comment_box:/i || /^cb:/i) { # comment_box
246 print "<P class=\"comment_box\">$'</P>\n";
247 } elsif(/^start_of_tab/i || /^sot/i) { # start_of_tab
249 } elsif(/^end_of_tab/i || /^eot/i) { # end_of_tab
252 print "<!--Unsupported command: $_-->\n";
254 } else { # this is a line with chords and lyrics
258 s/\s/\ /g; # replace spaces with hard spaces
259 while(s/(.*?)\[(.*?)\]//) {
261 push(@chords,$2 eq '\'|' ? '|' : $2);
263 push(@lyrics,$_); # rest of line (after last chord) into @lyrics
265 if($lyrics[0] eq "") { # line began with a chord
266 shift(@chords); # remove first item
267 shift(@lyrics); # (they are both empty)
270 if(@lyrics==0) { # empty line?
272 } elsif(@lyrics==1 && $chords[0] eq "") { # line without chords
273 print "<DIV class=\"$lClasses[$mode]\">$lyrics[0]</DIV>\n";
275 print "<TABLE cellpadding=0 cellspacing=0>";
278 for($i = 0; $i < @chords; $i++) {
279 print "<TD class=\"$cClasses[$mode]\">$chords[$i]</TD>";
281 print "</TR>\n<TR>\n";
282 for($i = 0; $i < @lyrics; $i++) {
283 print "<TD class=\"$lClasses[$mode]\">$lyrics[$i]</TD>";
285 print "</TR></TABLE>\n";
295 error "No chordpro parameter";
298 open my $file, '<', $infile
299 or error "Unable to open file $infile - $!";
306 chopro2html ($chopro, $infile);