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;
150 font-family: Arial, Helvetica;
153 .lyrics, .lyrics_chorus {
156 .lyrics_tab, .lyrics_chorus_tab {
157 font-family: "Courier New", Courier;
160 .lyrics_chorus, .lyrics_chorus_tab, .chords_chorus, .chords_chorus_tab {
163 .chords, .chords_chorus, .chords_tab, .chords_chorus_tab {
169 .comment, .comment_italic {
174 background-color: #ffbbaa;
188 $title = musicFileExists $song;
191 updateMusicpath $chopro, $song;
195 <table border="0" width="100%">
198 <td align="left"><a href="/songbook"><img src="/Icons/Home.png" alt="Home"></a></td>
204 <audio controls autoplay>
205 <source src="http://defaria.com/songbook/Media/$title.mp3" type='audio/mp3'>
206 <p>Your user agent does not support the HTML5 Audio element.</p>
216 my $mode = 0; # mode defines which class to use
219 # normal chorus normal+tab chorus+tab
220 my @lClasses = ('lyrics', 'lyrics_chorus', 'lyrics_tab', 'lyrics_chorus_tab' );
221 my @cClasses = ('chords', 'chords_chorus', 'chords_tab', 'chords_chorus_tab' );
223 while($chopro ne '') {
224 $chopro =~ s/(.*)\n?//; # extract and remove first line
228 if(/^#(.*)/) { # a line starting with # is a comment
229 print "<!--$1-->\n"; # insert as HTML comment
230 } elsif(/{(.*)}/) { # this is a command
232 if(/^title:/i || /^t:/i) { # title
233 print "<H1>$'</H1>\n";
234 } elsif(/^subtitle:/i || /^st:/i) { # subtitle
235 print "<H2>$'</H2>\n";
236 } elsif(/^start_of_chorus/i || /^soc/i) { # start_of_chorus
238 } elsif(/^end_of_chorus/i || /^eoc/i) { # end_of_chorus
240 } elsif(/^comment:/i || /^c:/i) { # comment
241 print "<span class=\"comment\">($')</span>\n";
242 } elsif(/^comment_italic:/i || /^ci:/i) { # comment_italic
243 print "<span class=\"comment_italic\">($')</span>\n";
244 } elsif(/^comment_box:/i || /^cb:/i) { # comment_box
245 print "<P class=\"comment_box\">$'</P>\n";
246 } elsif(/^start_of_tab/i || /^sot/i) { # start_of_tab
248 } elsif(/^end_of_tab/i || /^eot/i) { # end_of_tab
251 print "<!--Unsupported command: $_-->\n";
253 } else { # this is a line with chords and lyrics
257 s/\s/\ /g; # replace spaces with hard spaces
258 while(s/(.*?)\[(.*?)\]//) {
260 push(@chords,$2 eq '\'|' ? '|' : $2);
262 push(@lyrics,$_); # rest of line (after last chord) into @lyrics
264 if($lyrics[0] eq "") { # line began with a chord
265 shift(@chords); # remove first item
266 shift(@lyrics); # (they are both empty)
269 if(@lyrics==0) { # empty line?
271 } elsif(@lyrics==1 && $chords[0] eq "") { # line without chords
272 print "<DIV class=\"$lClasses[$mode]\">$lyrics[0]</DIV>\n";
274 print "<TABLE cellpadding=0 cellspacing=0>";
277 for($i = 0; $i < @chords; $i++) {
278 print "<TD class=\"$cClasses[$mode]\">$chords[$i]</TD>";
280 print "</TR>\n<TR>\n";
281 for($i = 0; $i < @lyrics; $i++) {
282 print "<TD class=\"$lClasses[$mode]\">$lyrics[$i]</TD>";
284 print "</TR></TABLE>\n";
294 error "No chordpro parameter";
297 open my $file, '<', $infile
298 or error "Unable to open file $infile - $!";
305 chopro2html ($chopro, $infile);