Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 <head> 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).
- #
- # <style type="text/css">
- # .lineNum {
- # position: absolute;
- # left: 550px;
- # }
- # </style>
- 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 (<INPUT>) {
- my $line = $_;
- chomp $line;
- if ($line =~ /^<a name=\"[B]*\d?\.\d?\.(\d+)\">/
- || $line =~ /^<a name=\"(\d+)\">/) {
- # 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><br>$/<span class=\"lineNum\">$line_num<\/span><\/a><br>/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";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement