#!/home/rebelsky/perl/bin/perl
#
#

# Set up the package
package Search; #package ID
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(readIndex textToSearch convertSpecialChars); 
#the useful subroutines

######################################
#  MODULES
######################################
use Network; #This sets up the readInFile subroutine which talks
             # to the annotation server (thanks to Andrew!)


sub readIndex {

 ##########################################################################
 # This subroutine looks in index.txt, checks if the given URL is there   #
 # and if it is, get the name of the directory associated with it.        #
 # REQUIRES: URL                                                          #
 # RETURNS: directory                                                     #
 ##########################################################################

 #################################
 # VARIABLES
 #################################

  local(*INDEX);
  my $url = shift;
  my @lines;
  my $index;
  my $buffer;
  my $dir;

  # Open index.txt and check if the URL is there.  If it is, get the name 
  # of the directory associated with it.
  $index = "/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/index.txt";

  # Call readInFile from DataBase.pm, written by Rachel Heck.
  # It opens the given file, reads the files into a string.
  $buffer = readInFile($index);
  # Split the string into lines and store it in an array
  @lines = split /^/m, $buffer;

  # look at each line of the file index.txt
  foreach $line (@lines) {
    #if we find the url
    if ($line =~ m/$url\|/) {
      # strip off the URL and vertical bar, leaving the name of the directory
      $line =~ s/.*?\|//;
      chomp($line);
      # set the dir we are working in to be the right one
      $dir = "/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/$line";
    }#if    
  }#foreach
  #$_ = $dir;
  if ($dir =~ m/URL/) {
    return $dir;
  }
  else {
    return 0;
  }#else
}#readIndex



#This was modified from Sam Rebelsky's code by Rachel Heck
sub backslash {
  # Get the parameters
  my $str = shift;
  my @things;
  my $sym;

  # Backslash the slashes first, so that if we insert any others,
  # they don't get slashified.
  $str =~ s/\\/\\\\/g;

  # The important symbols are listed on page 59 of the book.
  # For each such symbol

  @things = ("\|","\(","\)","\[","\{","\^","\$","\*","\+","\?","\."); 
  foreach $sym (@things) {
    $sym = "\\" . $sym;
    # Replace all copies of the particular symbol by a backslash
    # and the symbol.  This looks like it doesn't do anything.  However,
    # the backslash acts as a quote symbol in the first instance of
    # sym and as a literal in the second instance.
    $str =~ s/$sym/$sym/g;
  } # foreach
  # That's it, we're done.  Return the modified string.
  return $str;
} # backslash



sub textToSearch {

  ##############################################################
  # This takes a string and modifies it to be used in as
  # a search string.  It slashifies and changes all white-spaces
  # to account for HTML tags or any number of white-spaces.
  #
  ##############################################################

  # get the string passed as a parameter
  my $text = shift;

  # Strip extra white-space characters from the beginning and end.
  # Written with help from Sam
  $text =~ s/^\s*//;
  $text =~ s/\s*$//;
  
  # call backslash to add slashes in front of all special chars
  $text = backslash($text);

##############
# These are a bunch of cases that I'm handling separately.
# It could probably be handled more elegantly. 
###############

  ###Added by Rachel Heck on July 19, 1999
  #add possible tags before <
  $text =~ s/</(<.\*\?>)\*</sig;
  #add possible tags after >
  $text =~ s/>/>(<.\*\?>)\*/sig;
  #fix some problems from replacing < that were just added
  $text =~ s/\(<\.\*\?>\(<\.\*\?>\)\*\)\*/(<.\*\?>)\*/sig;

  #####the following lines were edited by Rachel Heck on July 15-16, 1999
  # Account for possible tags after (
  $text =~ s/\\\(/\\\((<.\*\?>)\*/sig;
  # possible tags before )   
  $text =~ s/\\\)/(<.\*\?>)\*\\\)/sig;

  # Account for possible tags after [
  $text =~ s/\\\[/\\\[(<.\*\?>)\*/sig;
  # possible tags before ] 
  $text =~ s/]/(<.\*\?>)\*]/sig;

  # Account for possible tags after {
  $text =~ s/\\\{/\\\{(<.\*\?>)\*/sig;
  # possible tags before } 
  $text =~ s/}/(<.\*\?>)\*}/sig;

  # possible tags before .
  $text =~ s/\\\./(<.\*\?>)\*\\\./sgi;

  # possible tags before ,
  $text =~ s/,/(<.\*\?>)\*,/sgi;

  # possible tags before !
  $text =~ s/!/(<.\*\?>)\*!/sgi;

  # possible tags before ,
  $text =~ s/\\\?/(<.\*\?>)\*\\\?/sgi;

  # Turn all white-space sequences in the annotated text
  # string into possible html tags and white-spaces.
  # written by Rachel Heck
  $text =~ s/(\s)+/((<.\*\?>)\|(\\s))\+/gis; 

  # Add possible white-space chars between tags
  #$text =~ s/></>(\\s)\*</gi;
  # add possible spaces after <a href> tags
  $text =~ s/(<a(?:\s)*href.*?>)/$1(\\s)\*/gi;

  # Get rid of repeat stuff
  $text =~ s/\(<\.\*\?>\)\*\(<\.\*\?>\)\*/(<.\*\?>)\*/sig;
  $text =~ s/\(<\.\*\?>\)\*\(\(<\.\*\?>\)\|\(\\s\)\)\+/((<.\*\?>)\|(\\s))\+/sig;
  $text =~ s/\(\(<\.\*\?>\)\|\(\\s\)\)\+\(<\.\*\?>\)\*/((<.\*\?>)\|(\\s))\+/sig;

  return $text;

} # textToSearch


sub convertSpecialChars {
  ##############################################################
  # This takes a string and converts all the special
  # chars to their HTML interpretation. 
  # 
  # INPUT: string
  # OUTPUT: string
  ##############################################################

  # get the string passed as a parameter
  my $text = shift;

  # ampersand
  $text =~ s/&/&amp\;/sg;
  # quotation mark
  $text =~ s/\"/&quot\;/sg;
  # less-than sign
  $text =~ s/</&lt\;/sg;
  # greater-than sign
  $text =~ s/>/&gt\;/sg;
  # vertical bar
  $text =~ s/\|/&brvbar\;/sg;
  #make all the actual linebreaks be line breaks when it is displayed
  #added by Rachel Heck on 8/4/99
  $text =~ s/\n/<br>/sg;

  return $text;
} # convertSpecialChars

return 1;

