#!/usr/bin/perl # # addFifthLine # # Parse an HTML file formatted according to the MIT Shakespeare format, # and print every fifth line number at the end of the line. # # Input: an HTML file using the MIT Shakespeare Format # Output: that same file, but with every fifth line printed after the line. # # Add the following to the element of your resulting HTML file to # offset the line numbers to the right of the text. You may need to increase # or decrease the left offset (default is 550px). # # use strict; use warnings; my $input_file; if ($ARGV[0]) { $input_file = $ARGV[0]; } else { die "File must be given as first arg\n"; } open INPUT, "$input_file" or die "Couldn't open file: $!\n"; # Read each line of the file. If it's a line of text, we have # to do some math, but otherwise we can just print the line. while () { my $line = $_; chomp $line; if ($line =~ /^/ || $line =~ /^/) { # If the line we've read is a line of text, we need to # figure out if it's a 5th line. If it's not, we can just # print it, but if it is, we need to insert that line number. my $line_num = $1; if ($line_num % 5 == 0) { $line =~ s/<\/a>
$/$line_num<\/span><\/a>
/g; print "$line\n"; } else { print "$line\n"; } } else { # The line isn't special, so we can just print it # But not quite yet... print "$line\n"; } }