Advertisement
tambascot

addFifthLine

Dec 1st, 2011
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.62 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # addFifthLine
  4. #
  5. # Parse an HTML file formatted according to the MIT Shakespeare format,
  6. # and print every fifth line number at the end of the line.
  7. #
  8. # Input: an HTML file using the MIT Shakespeare Format
  9. # Output: that same file, but with every fifth line printed after the line.
  10. #
  11. # Add the following to the <head> element of your resulting HTML file to
  12. # offset the line numbers to the right of the text. You may need to increase
  13. # or decrease the left offset (default is 550px).
  14. #
  15. # <style type="text/css">
  16. # .lineNum {
  17. #  position: absolute;
  18. #  left: 550px;
  19. # }
  20. # </style>
  21.  
  22. use strict;
  23. use warnings;
  24.  
  25. my $input_file;
  26.  
  27. if ($ARGV[0]) {
  28.   $input_file = $ARGV[0];
  29. }
  30.  
  31. else {
  32.   die "File must be given as first arg\n";
  33. }
  34.  
  35. open INPUT, "$input_file"
  36.   or die "Couldn't open file: $!\n";
  37.  
  38. # Read each line of the file. If it's a line of text, we have
  39. # to do some math, but otherwise we can just print the line.
  40.  
  41. while (<INPUT>) {
  42.   my $line = $_;
  43.  
  44.   chomp $line;
  45.  
  46.  
  47.   if ($line =~ /^<a name=\"[B]*\d?\.\d?\.(\d+)\">/
  48.      || $line =~ /^<a name=\"(\d+)\">/) {
  49.     # If the line we've read is a line of text, we need to
  50.     # figure out if it's a 5th line. If it's not, we can just
  51.     # print it, but if it is, we need to insert that line number.
  52.     my $line_num = $1;
  53.  
  54.     if ($line_num % 5 == 0) {
  55.       $line =~ s/<\/a><br>$/<span class=\"lineNum\">$line_num<\/span><\/a><br>/g;
  56.       print "$line\n";
  57.     }
  58.  
  59.     else {
  60.       print "$line\n";
  61.     }
  62.   }
  63.  
  64.   else {
  65.     # The line isn't special, so we can just print it
  66.     # But not quite yet...
  67.     print "$line\n";
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement