## Abstract, start package Copyright (GPL) Steve van der Burg
package HTML::DocHead;
use HTML::Parser;
use vars qw / @ISA /;
@ISA = qw(HTML::Parser);

sub start {
   $_[0]->{saw_markup} = 1;
   $_[0]->{in_title}++  if $_[1] eq 'title';
   $_[0]->{in_script}++ if $_[1] eq 'script';
   $_[0]->{in_style}++ if $_[1] eq 'style';
}

sub end {
   $_[0]->{saw_markup} = 1;
   $_[0]->{in_title}--  if $_[1] eq 'title';
   $_[0]->{in_script}-- if $_[1] eq 'script';
   $_[0]->{in_style}-- if $_[1] eq 'style';
}

sub text {
   return if $_[0]->{in_script};
   return if $_[0]->{in_style};
   return if length($_[0]->{doc}) > 300;
   my $t = $_[1];
   my $saw_eol;
   for ( $t ) {
      s/&nbsp;/ /g;
      s/\r//g;
      $saw_eol = /\n/;
      s/\n+/ /g;
      s/^\s+//;
      s/\s+$//;
      s/\s{1,}/ /g;
   }
   return if $t eq '';

   $_[0]->{title} = $t if $_[0]->{in_title};
   return if $_[0]->{in_title};

   $_[0]->{doc} .= $t;
   $_[0]->{doc} .= ' ' if $_[0]->{doc} && ( $saw_eol || $_[0]->{saw_markup} );
   $_[0]->{saw_markup} = 0;
}

sub out {
   my $title = $_[0]->{title};
   for ( $_[0]->{doc} ) {
      s/^$title//i;
      s/^\s+//;
      s/\s+$//;
   }
   return substr($_[0]->{doc},0,300);
}
## end code Steve van der Burg
