######################################################################
# LinkTrans.pm
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

######################################################################
  package LinkTrans;
######################################################################
use HTML::Parser;                      # parser for HTML
use HTML::Entities;                    # codes special characters

@ISA = qw(HTML::Parser);               # base class: HTML::Parser

######################################################################
# constructor: LinkTrans->new(\&callback);
######################################################################
sub new {        
  my ($class, $callback) = @_;

  my $self = $class->SUPER::new();     # call base class constructor
                                       # own instance variables:
  $self->{href_callback} = $callback;  # callback function for URLs
  $self->{linktrans_result} = "";      # result string
  $self;                               # return object reference
}

######################################################################
# ... is called for things like "<!DOCTYPE ...>"
######################################################################
sub declaration {
  my ($self, $declaration) = @_;
  $self->{linktrans_result} .= "<!$declaration>"; # take over
}

######################################################################
# ... is called for each start tag, such as "<A HREF=...>"
######################################################################
sub start {
  my ($self, $tag, $attrhr, $attrseq, $origtext) = @_;

  $self->{linktrans_result} .= "<$tag";           # copy tag name

  foreach $key (keys %$attrhr) {       # iterate over attribute keys
                              
    my $val = $attrhr->{$key};

    if($tag eq "a"   && $key eq "href" ||    # call callback for 
       $tag eq "img" && $key eq "src") {     # '<A HREF' or '<IMG SRC'
                                             # and substitute value
$val = $self->{href_callback}->($val); 
    }
                                             # mask special characters
    $val = HTML::Entities::encode($val, '<>&"');
    $self->{linktrans_result} .= " $key=\"$val\"";   # append
  }

  $self->{linktrans_result} .= ">";          # terminate
}

######################################################################
# ... is called for each end tag, such as "</A>"
######################################################################
sub end {
  my ($self, $tag) = @_;
  $self->{linktrans_result} .= "</$tag>";    # simply take over
}

######################################################################
# ... is called for each piece of text (no tags)
######################################################################
sub text {
  my ($self, $text) = @_;
  $self->{linktrans_result} .= "$text";      # simply take over
}

######################################################################
# ... is called for each comment "<!-- ... -->"
######################################################################
sub comment {
  my ($self, $comment) = @_;                 # simply take over
  $self->{linktrans_result} .= "<!-- $comment -->";
}

######################################################################
# return result
######################################################################
sub get_result {
  my ($self) = @_;
  $self->{linktrans_result};
}

1;

