#!/usr/bin/perl
#
# cueScriptGen
#
# Generate a cue script from an MIT Shakespeare File
#
#
# Released under the GPL 2.0 and all that jazz.
#
# This is just a prototype. A more polished version will need further
# testing and development.
#
# Last Revised 12 Dec. 2012 8 PM
use strict;
use warnings;
# The user must give us at least one character name (but may give us
# any number of them), followed by a script file. If they don't,
# print a simple error and exit.
if (@ARGV < 2) {
die "usage is \"cueScriptGen character_1 [character_2 &c] file_name\"\n";
}
# We should edit this to either accept a file name or a URL.
# If we force a URL, we can explore transformations based on
# alternative methods of formatting.
# Whatever the file is, it will be the LAST thing in our
# argument list, so pop it off so we have a list of nothing
# but characters.
my $mit_shakes = pop @ARGV;
# Initialize variables.
my $cue_line = "";
my $prev_cue_line = "";
my $act = "";
my $scene = "";
my $new_act = "false";
my $new_scene = "false";
my $line = "";
my $title_of_play = "";
# Open a file or quit with a basic warning if you can't.
open PLAY, $mit_shakes || die "Could not open file: $!\n";
# Step 0.5: Print basic html header info from the file, and
# them some heading info so the reader knows what they're
# looking at.
until ($line =~ m/<\/head>/) {
if ($line =~ m/
(.*)<\/title>/) {
$title_of_play = $1;
}
print "$line\n";
$line = ;
}
print "\n\n";
print "$title_of_play
\n";
print 'Cue Script for ';
# This boolean will tell us if we're on the
# first item in the list, which should not
# be preceeded by a comma and a space when
# printing the list of characters.
my $i = 0;
foreach (@ARGV) {
if ($i == 0) {
print "$_";
$i = 1;
}
else {
print ", $_";
}
}
print ".
\n";
# Traverse the file until EOF
while () {
$line = $_;
# print "\n";
# Step 1: Find a speech block that contains a character that we're looking for.
# We need to do this for every character left in our arguument list.
foreach (@ARGV) {
my $character = $_;
if ($line =~ m/^$character<\/b>/i) {
# Print the act heading if we haven't yet.
if ($new_act eq "true") {
print "$act\n";
$new_act = "false";
}
# Print the scene headin if we haven't yet.
if ($new_scene eq "true") {
print "$scene\n";
$new_scene = "false";
}
# Step 2: We've found a line of text that matches a block of text for the
# character we're looking for, so first we need to print their cue line.
# See step 1.5 (below) for details. If the cue line is less than three
# words long, print the line that came before it, too.
$cue_line =~ m/^(.*);
my @cue_words = split /\s/, $1;
my $cue_words = @cue_words;
if ($cue_words < 2) {
print "Cue: $prev_cue_line\n$cue_line\n\n";
}
else {
#print "Cue: $cue_line\n\n";
}
# Step 3: Print the entire speech block for the character; you know you're
# at the end of the block when you reach the closing blockquote
while ($line !~ m/<\/blockquote>/) {
print "$line\n";
# We still need to keep track of the cue line, just in case the cue script
# is for an actor who plays two roles with back to back lines.
$prev_cue_line = $cue_line;
$cue_line = $line;
# Read the next line of the file.
$line = ;
}
# Step 4: Print the closing blockquote.
print "\n";
}
# Step 1.5: We need to keep track of each line of text as we go, so if the
# line was not a speech heading, see if it's a line of text. If it is, keep it
elsif ($line =~ m/^ACT \w+.*/) {
$act = $line;
$new_act = "true";
}
# Step 1.7: We should also print scene headings.
elsif ($line =~ m/.*SCENE \w+.*/) {
$scene = $line;
$new_scene = "true";
}
}
}
# Last piece of cleanup, close off our HTML elements
print "\n