Advertisement
dougllio

macbethsearch.pl

Jul 31st, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.75 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #################
  4. ## Searching MacBeth for text
  5. ## MacBeth text is here: http://pastebin.com/PV5YUBuA
  6. #################
  7.  
  8. # Using the CGI.pm module in a non-OO manner
  9. use CGI qw(:standard);
  10.  
  11. #If the form was posted, grab the form variables
  12. if ($ENV{'REQUEST_METHOD'} eq "POST") {
  13.     $search = param('search');
  14.     $showall = param('showall');
  15. }
  16.  
  17. # For Testing via perl on the commandline
  18. # $search="toil";
  19.  
  20. # Print the standard content type for the page
  21. print header;
  22.  
  23. # Print the HTML header
  24. print start_html("Search Macbeth! - Coding 101 Episode 27");
  25. print h1("William Shakespeare's Macbeth");
  26.  
  27. # Print out the elements of the search form
  28. print start_form, strong("What do you want to search for in MacBeth?"),
  29.     textfield(-name => "search", -default => $search), p,
  30.     checkbox(-name => "showall", -value => "YES", -label => "Show Entire Document"),
  31.     p, submit, end_form, hr;
  32.  
  33. # If the length of search string is 0 then we have nothing to do.
  34. if ($search){
  35.  
  36.     # This is the Macbeth text file
  37.     $file = "macbeth.txt";
  38.  
  39.     # Search results header
  40.     print h2("Search Results (bolded):");
  41.  
  42.     # Open the file or give an error message
  43.     open MY_FILE, "<", $file or die "Could not open \"$file\".";
  44.        
  45.     # Loop through until the end of the file
  46.     while(<MY_FILE>) {
  47.         # Remove the \n newline character at the end of each line and
  48.         # replace with the HTML <br> tag
  49.         s/\n$/<br>/;
  50.            
  51.         # Append each line read from $_ (Perl's default variable when
  52.         # reading from a file) into my variable called $fulltext
  53.         $fulltext .= $_;
  54.     }
  55.        
  56.     # Close the file
  57.     close MY_FILE;
  58.        
  59.     # Remove the Project Gutenberg pre and post text
  60.     #$fulltext =~ /---BEGIN---(.*)---END---/;
  61.     $fulltext =~ s/.*---BEGIN---(.*)---END---.*/$1/;
  62.     #$fulltext = $1;
  63.  
  64.     # The file is formatted with lots of line breaks within lines and a double linebreak
  65.     # between them so these regexes will help to fix that on the fly without altering the file.
  66.     # Use split to create an array of paragraphs, splitting on the double new line character
  67.     # which should now put each line of dialog in a separate paragraph. The file is
  68.     # formatted with lots of line breaks within paragraphs and a double linebreak
  69.     # between paragraphs. This will help to fix that on the fly without altering the file.
  70.     @paragraphs =  split/<br>(.<br>)+/gs, $fulltext;
  71.    
  72.     # Keep a count of number of matches
  73.     $matchcount = 0;
  74.  
  75.     # Loop through the paragraph array and count the number of occurrances of the search term
  76.     for $paragraph (@paragraphs) {
  77.         # Grep is useful for counting matches and placing them in a scalar variable
  78.         # or actually putting the matches into an array. I don't need the matches here.
  79.         $count = grep /$search/gis, $paragraph;
  80.         $matchcount += $count;
  81.            
  82.         # If there is at least one match in this paragraph, I want to print it out.
  83.         if ($count > 0) {
  84.             # Format the text so that the name of the character speaking is underlined.
  85.             $paragraph =~ s/([A-Z\ ]+)\./<u>$1<\/u> /gs;
  86.  
  87.             # We're going to do a substitution in the paragraph to add HTML bolding and
  88.             # a yellow background to the search term and print out the paragraph.
  89.             $paragraph =~ s/($search)/<span style='font-weight: bold;background-color: #ffff42;'>$1<\/span>/ugis;
  90.  
  91.             # Now print out the result bordered by an HTML hard rule line
  92.             print p($paragraph), hr;
  93.         } else {
  94.             # If the Show all checkbox was checked, print out the paragraph anyway.
  95.             if ($showall eq "YES") {
  96.                 print p($paragraph);
  97.             }
  98.         }
  99.         }
  100.        
  101.         # If no matches were found AND the Show all checkbox is empty, print a message
  102.         if ($matchcount < 1 && $showall ne "YES") {
  103.             print p("Sorry. No matches.");
  104.         }
  105.            
  106. }
  107.  
  108. # End the HTML
  109. print end_html;
  110. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement