#!/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, $i);
my $documentRoot = "/web";
my $debug = param ('debug');
my $infile = param ('chordpro');
unless (-f $infile) {
$infile = '/opt/songbook/Andrew/' . $infile;
unless (-f $infile) {
$infile = '/web/xmas/' . param ('chordpro');
unless (-f $infile) {
print "Unable to open $infile";
exit 1;
} # unless
} # 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 getTitle ($) { my ($song) = @_; return fileparse ($song, qr/\.pro/); } # getTitle sub musicFileExists ($) { my ($song) = @_; debug "ENTER musicFileExists ($song)"; my $title = getTitle ($song); my $musicfile = "/opt/media/$title.mp3"; return -r $musicfile; } # musicFileExists sub updateMusicpath ($$) { my ($chopro, $song) = @_; # If there's no corresponding music file then do nothing return unless musicFileExists $song; # If the .pro file already has musicpath then do nothing 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 - $!$titleLink
|
Mark A: not set Mark B: not set |
$'
\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 "