Script: HTML/ASP search highlighter.  (284 bytes)

Author: Steve Combs, 1998 <scombs@usweb.com>

Usage:  perl -sp00F PROGRAM -s=word{,word_n} [-p] infile.html

Where:   -s specifies word(s) to search for 
         -p allows searching of partial words

Input is any html (or .asp) file. Output is the filtered 
text with found words highlighted, and <a name="_n"> and
<a href=#_n> links created to bounce the surfer to their
first and subsequent occurences.

Although this standalone version meets O.P.C. constraints,
the real power of this script is realized when included
within the standard header of a PerlScript'ed web site.

An online production example may be seen at
http://www.judicial.state.ia.us/rules/local/d3.asp?search=perl&p=1#_1

In this case, "search=perl" and "&p=1" are passed via the
URL instead of the command line's "-s=perl" and "-p".  The
"#_1" jumps you to the first word (or partial word) found.
(See also http://www.judicial.state.ia.us/search)

Brevity is next to cleanliness. Obfuscuity follows. -sac


# PerlScript ASP <!--#include--> file: stdhead.inc
#################################################################
<%  @language=vbscript %>
<html>
<!------------------------------------------------
    Bunch more stuff for page header and menu.
------------------------------------------------->
<%
    if Request.QueryString("search")<>"" then
        search_n_hylite() 'Call PerlScript procedure.
        Response.End      'Toss original HTML source page.
    end if
%>
<script language=PerlScript runat=server>sub search_n_hylite{
    my $search,$i;
    ($search=$Request->QueryString("search")->item)=~s/ /|/g;
    open(SOURCE,$Request->ServerVariables(PATH_TRANSLATED)->item);
    $_=join('',<SOURCE>); #Reread entire ASP source file.
    s/(<)%.*?%(>)//gs;    #Strip out "<% ASP %>" tags.
    s/(<)!--#include.*?--(>)//gs; #Strip out "<!--#include-->"s.
    if ($Request->QueryString("p")->item){ #Partial word flag.
        s/($search)(?=[^>]*(?=<|$))/'<a\ name="_'.++$i.'">
        <a\ href="#_'.($i+1).'"><font\ color=#ff0066><b>'.$1.
        '<\/b><\/font><\/a><\/a>'/eigx;    #Loved your book J.F.
    }else{
        s/(\W)($search)(?=(?=<|$)|(?=[^>\w][^>]*(?=<|$)))/$1.
        '<a\ name="_'.++$i.'"><a href="#_'.($i+1).'"><font
        \ color=#ff0066><b>'.$2.'<\/b><\/font><\/a><\/a>'/eigx;
    }
    $i++; s/#_$i"/#_1"/;  #Replace last href with link to first.
    $Response->Write($_); #Send modified page to surfer dude.
}</script>
#################################################################
