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
18 use CGI qw(:standard);
19 use CGI::Carp qw (fatalsToBrowser);
24 my $documentRoot = "/web";
25 my $debug = param ('debug');
26 my $infile = param ('chordpro');
29 $infile = '/opt/songbook/Andrew/' . $infile;
32 $infile = '/web/xmas/' . param ('chordpro');
35 print "Unable to open $infile";
46 print "<font color=red><b>Debug:</b></font> $msg<br>";
56 print "<font color=orange><b>Warning</b></font> $msg<br>";
64 print "<html><head><title>Web Chord: Error</title></head>" .
65 "<body><h1>Error</h1><p>\n$msg\n</p>" .
74 return fileparse ($song, qr/\.pro/);
77 sub musicFileExists ($) {
80 debug "ENTER musicFileExists ($song)";
82 my $title = getTitle ($song);
83 my $musicfile = "/opt/media/$title.mp3";
88 sub updateMusicpath ($$) {
89 my ($chopro, $song) = @_;
91 # If there's no corresponding music file then do nothing
92 return unless musicFileExists $song;
94 # If the .pro file already has musicpath then do nothing
95 return if $chopro =~ /\{musicpath:.*\}/;
97 # Otherwise append the musicpath
100 open $songfile, '>>', $song
103 unless (defined $songfile) {
104 my $msg = "Unable to open $song for append - $!<br>";
105 $msg .= "<br>Please notify <a href=\"mailto:adefaria\@gmail.com?subject=Please chmod 666 $song\">Andrew DeFaria</a> so this can be corrected.<br>";
106 $msg .= "<br>Thanks";
112 my $songbase = '/sdcard';
113 my $title = getTitle $song;
115 print $songfile "{musicpath:$songbase/SongBook/Media/$title.mp3}\n";
122 # Outputs the HTML code of the chordpro file in the first parameter
123 sub chopro2html ($$) {
124 my ($chopro, $song) = @_;
126 $chopro =~ s/\</\</g; # replace < with <
127 $chopro =~ s/\>/\>/g; # replace > with >
128 $chopro =~ s/\&/\&/g; # replace & with &
130 my $title = "ChordPro Song";
131 my $artist = "Unknown";
133 if (($chopro =~ /^{title:(.*)}/mi) || ($chopro =~ /^{t:(.*)}/mi)) {
137 if (($chopro =~ /^{subtitle:(.*)}/mi) || ($chopro =~ /^{st:(.*)}/mi)) {
144 <title>$title</title>
145 <link rel="stylesheet" type="text/css" href="songbook.css">
146 <link rel="stylesheet" type="text/css" href="question.mark.css">
147 <script src="songbook.js"></script>
148 <script src="question.mark.js"></script>
154 $title = getTitle $song;
157 updateMusicpath $chopro, $song;
160 my $titleLink = "<a href=\"/songbook/pro/$title.pro\">$title</a>";
165 <td align="left"><a href="/songbook"><img src="/Icons/Home.png" alt="Home"></a></td>
166 <td><div id="title">$titleLink</div>
167 <div id="artist"><a href="/songbook/displayartist.php?artist=$artist">$artist</a></div></td>
168 <td align="right" width="300px">
169 <audio id="song" controls autoplay style="padding:0; margin:0">
170 <source src="https://defaria.com/Media/$title.mp3" style="padding:0; margin:0" type='audio/mp3'>
171 Your user agent does not support the HTML5 Audio element.
173 <p align="center" <font size=-1><b>Mark A:</b></font>
174 <font size=-1 color=#666><span id="a"><i>not set</i></span></font>
175 <font size=-1><b>Mark B:</b></font>
176 <font size=-1 color=#666><span id="b">not set</span></font></p>
183 my $mode = 0; # mode defines which class to use
186 # normal chorus normal+tab chorus+tab
187 my @lClasses = ('lyrics', 'lyrics_chorus', 'lyrics_tab', 'lyrics_chorus_tab' );
188 my @cClasses = ('chords', 'chords_chorus', 'chords_tab', 'chords_chorus_tab' );
190 while($chopro ne '') {
191 $chopro =~ s/(.*)\n?//; # extract and remove first line
195 if (/^#(.*)/) { # a line starting with # is a comment
196 print "<!--$1-->\n"; # insert as HTML comment
197 } elsif (/{(.*)}/) { # this is a command
199 if (/^start_of_chorus/i || /^soc/i) { # start_of_chorus
201 } elsif (/^end_of_chorus/i || /^eoc/i) { # end_of_chorus
203 } elsif (/^comment:/i || /^c:/i) { # comment
204 print "<span class=\"comment\">($')</span>\n";
205 } elsif (/^comment_italic:/i || /^ci:/i) { # comment_italic
206 print "<span class=\"comment_italic\">($')</span>\n";
207 } elsif (/^comment_box:/i || /^cb:/i) { # comment_box
208 print "<P class=\"comment_box\">$'</P>\n";
209 } elsif (/^start_of_tab/i || /^sot/i) { # start_of_tab
211 } elsif (/^end_of_tab/i || /^eot/i) { # end_of_tab
214 print "<!--Unsupported command: $_-->\n";
216 } else { # this is a line with chords and lyrics
220 s/\s/\ /g; # replace spaces with hard spaces
221 while(s/(.*?)\[(.*?)\]//) {
223 push(@chords,$2 eq '\'|' ? '|' : $2);
225 push(@lyrics,$_); # rest of line (after last chord) into @lyrics
227 if ($lyrics[0] eq "") { # line began with a chord
228 shift(@chords); # remove first item
229 shift(@lyrics); # (they are both empty)
232 if (@lyrics==0) { # empty line?
234 } elsif (@lyrics==1 && $chords[0] eq "") { # line without chords
235 print "<div class=\"$lClasses[$mode]\">$lyrics[0]</div>\n";
237 print "<table cellpadding=0 cellspacing=0>";
239 for(my $i = 0; $i < @chords; $i++) {
240 print "<td class=\"$cClasses[$mode]\">$chords[$i]</td>";
242 print "</tr>\n<tr>\n";
243 for($i = 0; $i < @lyrics; $i++) {
244 print "<td class=\"$lClasses[$mode]\">$lyrics[$i]</td>";
246 print "</tr></table>\n";
258 error "No chordpro parameter";
261 open my $file, '<', $infile
262 or error "Unable to open file $infile - $!";
269 chopro2html ($chopro, $infile);