#!/usr/bin/perl # ==================================================================== # @(#) grab the contents of an html doc within a given element class # ==================================================================== # $Id:$ # # Written by: David Boldt Sept 4, 1998 # Revisions: # -------------------------------------------------------------------- $DocumentRoot = $ENV{'DOCUMENT_ROOT'}; $Env_Path_Translated = $ENV{'PATH_TRANSLATED'}; print &classgrep(@ARGV); # pull out the text surrounded by an element of a given class # from one or more html files, with option for random selection # of the matches # # USAGE: classgrep [-r[n]] class (file|virtual)="file" ... # n - number of random matches to keep (1 by default) # class - classname to search # file - fullpathname or relative pathname of file, # (must be in quotes) sub classgrep { local($/); # input record separator, read whole file at once local($args,@args);# the list of arguments local($class); # class to search for local($ref); # file reference local($type); # reference type, 'file' or 'virtual' local($file); # pathname local(*DOC); # file handle local($piece); # bit of doc we have found as we are parsing local($tag); # element name local($starting); # boolean: new element of the right class local($count); # start tag counter local(@results); # array of matching pieces local($random); # boolean: random selection of matching pieces local($selection); # text we will return local($index); # array index for random selection # -- deal with args passed any old way $args = "@_"; $args =~ s/^\s+//; $args =~ s/\s+$//; @args = split(/\s+/, $args); # -- command line option for random selections if ($args[0] =~ /^-r/) { $random = 1; if ($args[0] =~ /(\d+)$/) { $random = $1; shift(@args); } else { shift (@args); $random = shift(@args) if ($args[0] =~ /^\d+$/); } } $results[0] = ''; $class = shift(@args); foreach $ref (@args) { # for each file listed # -- is the file a full pathname or relative (virtual) $ref =~ /(virtual|file)=\"([^\"]+)\"/; ($type, $file) = ($1, $2); $type =~ tr/A-Z/a-z/; $file = &FullPath($file) if ($type eq 'virtual'); open(DOC, "<$file"); $text = ; close(DOC); foreach $piece ( split(/(<\/?[^>]+>)/, $text) ) { if ($piece =~ /^<(\w+).*[cC][lL][aA][sS][sS]="$class"/o) { $tag = $1; $starting = 1; $count = 1; } elsif ($tag) { ++$count if ($piece =~ /^<$tag[\W]/i); # nested elements if ($piece =~ /^<\/$tag>/i) { # closing an element --$count; if ($count == 0) { $tag = '' ; $results[$#results] .= "$piece\n"; push(@results,''); } } } $results[$#results] .= $piece if ($tag); } } pop(@results) unless ($results[$#results]); # -- random selection of results, or all of them if ($random) { srand(time||$$); for ($i=1;$i<=$random;++$i) { $index = int(rand($#results+1)); # random array index $selection .= $results[$index]; # basket of goodies to keep splice(@results,$index,1); # remove selected item from list } } else { # no random arg $selection = join('', @results); # return everything } return $selection; } sub FullPath { #Finds full path of a file. local ($file) = $_[0]; local ($basedir); return $file if ($file =~ /^$DocumentRoot/); return "$DocumentRoot$file" if ($file =~ /^\//); ($basedir = $Env_Path_Translated) =~ s/[^\/]+$//; while ($file =~ s/^\.\.\///) {$basedir =~ s/[^\/]+\/$//} $file =~ s/\.\///; $file = $basedir . $file; $file =~ s#//#/#g; return $file; }