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');
30 $infile = '/opt/songbook/Songs/' . $infile;
33 $infile = '/web/xmas/' . param ('chordpro');
36 print "Unable to open $infile";
47 print "<font color=red><b>Debug:</b></font> $msg<br>";
57 print "<font color=orange><b>Warning</b></font> $msg<br>";
65 print "<html><head><title>Web Chord: Error</title></head>" .
66 "<body><h1>Error</h1><p>\n$msg\n</p>" .
72 sub musicFileExists ($) {
75 debug "ENTER musicFileExists ($song)";
77 my $title = fileparse ($song, qr/\.pro/);
78 my $musicfile = "/opt/media/$title.mp3";
85 debug "Could not find $musicfile";
91 sub updateMusicpath ($$) {
92 my ($chopro, $song) = @_;
94 my $title = musicFileExists $song;
96 # If there's no corresponding music file then do nothing
99 # If the .pro file already has musicpath then do nothing
100 if ($chopro =~ /\{musicpath:.*\}/) {
101 debug "$song already has musicpath";
104 return if $chopro =~ /\{musicpath:.*\}/;
106 # Otherwise append the musicpath
109 open $songfile, '>>', $song
112 unless (defined $songfile) {
113 my $msg = "Unable to open $song for append - $!<br>";
114 $msg .= "<br>Please notify <a href=\"mailto:adefaria\@gmail.com?subject=Please chmod 666 $song\">Andrew DeFaria</a> so this can be corrected.<br>";
115 $msg .= "<br>Thanks";
121 my $songbase = '/sdcard';
123 print $songfile "{musicpath:$songbase/SongBook/Media/$title.mp3}\n";
130 # Outputs the HTML code of the chordpro file in the first parameter
131 sub chopro2html ($$) {
132 my ($chopro, $song) = @_;
134 $chopro =~ s/\</\</g; # replace < with <
135 $chopro =~ s/\>/\>/g; # replace > with >
136 $chopro =~ s/\&/\&/g; # replace & with &
140 if(($chopro =~ /^{title:(.*)}/mi) || ($chopro =~ /^{t:(.*)}/mi)) {
143 $title = "ChordPro song";
148 <title>$title</title>
149 <style type="text/css">
151 background-image: url('/songbook/background.jpg');
156 font-family: Arial, Helvetica;
162 font-family: Arial, Helvetica;
166 .lyrics, .lyrics_chorus {
169 .lyrics_tab, .lyrics_chorus_tab {
170 font-family: "Courier New", Courier;
173 .lyrics_chorus, .lyrics_chorus_tab, .chords_chorus, .chords_chorus_tab {
176 .chords, .chords_chorus, .chords_tab, .chords_chorus_tab {
181 .comment, .comment_italic {
186 background-color: #ffbbaa;
200 $title = musicFileExists $song;
203 updateMusicpath $chopro, $song;
207 <table border="0" width="100%">
210 <td align="left"><a href="/songbook"><img src="/Icons/Home.png" alt="Home"></a></td>
216 <audio controls autoplay>
217 <source src="http://defaria.com/Media/$title.mp3" type='audio/mp3'>
218 <p>Your user agent does not support the HTML5 Audio element.</p>
228 my $mode = 0; # mode defines which class to use
231 # normal chorus normal+tab chorus+tab
232 my @lClasses = ('lyrics', 'lyrics_chorus', 'lyrics_tab', 'lyrics_chorus_tab' );
233 my @cClasses = ('chords', 'chords_chorus', 'chords_tab', 'chords_chorus_tab' );
235 while($chopro ne '') {
236 $chopro =~ s/(.*)\n?//; # extract and remove first line
240 if(/^#(.*)/) { # a line starting with # is a comment
241 print "<!--$1-->\n"; # insert as HTML comment
242 } elsif(/{(.*)}/) { # this is a command
244 if(/^title:/i || /^t:/i) { # title
245 print "<H1>$'</H1>\n";
246 } elsif(/^subtitle:/i || /^st:/i) { # subtitle
247 print "<H2>$'</H2>\n";
248 } elsif(/^start_of_chorus/i || /^soc/i) { # start_of_chorus
250 } elsif(/^end_of_chorus/i || /^eoc/i) { # end_of_chorus
252 } elsif(/^comment:/i || /^c:/i) { # comment
253 print "<span class=\"comment\">($')</span>\n";
254 } elsif(/^comment_italic:/i || /^ci:/i) { # comment_italic
255 print "<span class=\"comment_italic\">($')</span>\n";
256 } elsif(/^comment_box:/i || /^cb:/i) { # comment_box
257 print "<P class=\"comment_box\">$'</P>\n";
258 } elsif(/^start_of_tab/i || /^sot/i) { # start_of_tab
260 } elsif(/^end_of_tab/i || /^eot/i) { # end_of_tab
263 print "<!--Unsupported command: $_-->\n";
265 } else { # this is a line with chords and lyrics
269 s/\s/\ /g; # replace spaces with hard spaces
270 while(s/(.*?)\[(.*?)\]//) {
272 push(@chords,$2 eq '\'|' ? '|' : $2);
274 push(@lyrics,$_); # rest of line (after last chord) into @lyrics
276 if($lyrics[0] eq "") { # line began with a chord
277 shift(@chords); # remove first item
278 shift(@lyrics); # (they are both empty)
281 if(@lyrics==0) { # empty line?
283 } elsif(@lyrics==1 && $chords[0] eq "") { # line without chords
284 print "<DIV class=\"$lClasses[$mode]\">$lyrics[0]</DIV>\n";
286 print "<TABLE cellpadding=0 cellspacing=0>";
289 for($i = 0; $i < @chords; $i++) {
290 print "<TD class=\"$cClasses[$mode]\">$chords[$i]</TD>";
292 print "</TR>\n<TR>\n";
293 for($i = 0; $i < @lyrics; $i++) {
294 print "<TD class=\"$lClasses[$mode]\">$lyrics[$i]</TD>";
296 print "</TR></TABLE>\n";
306 error "No chordpro parameter";
309 open my $file, '<', $infile
310 or error "Unable to open file $infile - $!";
317 chopro2html ($chopro, $infile);