#!/usr/bin/perl
# Web Chord v1.1 - A CGI script to convert a ChordPro file to HTML
# Copyright 1998-2003 Martin Vilcans (martin@mamaviol.org)
#
# CGI parameters:
# chopro - This parameter can be submitted by a form as a text field or file
# upload.
#
# History:
# 1998-07-20 Version 1.0
# 2003-08-03 Version 1.1 Uses stylesheets
# 2014-02-05 Added things particular to my implementation of Songbook at
# http://defaria.com/songbook
use strict;
use warnings;
use CGI qw(:standard);
use CGI::Carp qw (fatalsToBrowser);
use File::Basename;
my ($chopro, $output, $i);
my $documentRoot = "/web";
my $debug = param ('debug');
my $infile = param ('chordpro');
unless (-f $infile) {
$infile = '/opt/clearscm/songbook/Songs/' . $infile;
unless (-f $infile) {
print "Unable to $infile";
exit 1;
} # unless
} # unless
sub debug ($) {
my ($msg) = @_;
return unless $debug;
print "Debug: $msg
";
return;
} # debug
sub warning ($) {
my ($msg) = @_;
debug "warning";
print "Warning $msg
";
return;
} # warning
sub error {
my ($msg) = @_;
print "
\n$msg\n
" . ""; exit; } # error sub musicFileExists ($) { my ($song) = @_; debug "ENTER musicFileExists ($song)"; my $title = fileparse ($song, qr/\.pro/); my $musicfile = "/opt/clearscm/songbook/Media/$title.mp3"; if (-r $musicfile) { debug "Exists!"; return $title; } else { debug "Could not find $musicfile"; return undef; } # if } # musicFileExists sub updateMusicpath ($$) { my ($chopro, $song) = @_; my $title = musicFileExists $song; # If there's no corresponding music file then do nothing return unless $title; # If the .pro file already has musicpath then do nothing if ($chopro =~ /\{musicpath:.*\}/) { debug "$song already has musicpath"; } # if return if $chopro =~ /\{musicpath:.*\}/; # Otherwise append the musicpath my $songfile; open $songfile, '>>', $song or undef $songfile; unless (defined $songfile) { my $msg = "Unable to open $song for append - $!END if ($title) { print <<"END"; | END } # if print <<"END"; |
$'
\n"; } elsif(/^start_of_tab/i || /^sot/i) { # start_of_tab $mode |= 2; } elsif(/^end_of_tab/i || /^eot/i) { # end_of_tab $mode &= ~2; } else { print "\n"; } } else { # this is a line with chords and lyrics my(@chords,@lyrics); @chords=(""); @lyrics=(); s/\s/\ /g; # replace spaces with hard spaces while(s/(.*?)\[(.*?)\]//) { push(@lyrics,$1); push(@chords,$2 eq '\'|' ? '|' : $2); } push(@lyrics,$_); # rest of line (after last chord) into @lyrics if($lyrics[0] eq "") { # line began with a chord shift(@chords); # remove first item shift(@lyrics); # (they are both empty) } if(@lyrics==0) { # empty line? print "$chords[$i] | "; } print "
$lyrics[$i] | "; } print "