#! /usr/local/bin/ruby

## Perl module sample
## 1998 by yoshidam
##

require 'perl'

perl = Perl.new("-MHTML::Parser", "-MRuby", "-e", "0")
perl.eval(%q!
  package NewHTMLParser;
  @ISA = qw(HTML::Parser);
  sub new {
    my $type = shift;
    my $rubyobj = shift;
    my $self = new HTML::Parser;
    $self->{'.ruby_obj'} = $rubyobj;
    bless $self, $type;
  }

  sub declaration {
    my $self = shift;
    my ($decl) = @_;
    if (Ruby::send($self->{'.ruby_obj'}, "respond_to?", "decralation")) {
      Ruby::send($self->{'.ruby_obj'}, "decralation", $decl);
    }
  }

  sub start {
    my $self = shift;
    my ($tag, $attr, $attrseq, $origtext) = @_;
    if (Ruby::send($self->{'.ruby_obj'}, "respond_to?", "start")) {
      Ruby::send($self->{'.ruby_obj'}, "start",
                 $tag, $attr, $attrseq, $origtext);
    }
  }

  sub end {
    my $self = shift;
    my ($tag, $origtext) = @_;
    if (Ruby::send($self->{'.ruby_obj'}, "respond_to?", "end")) {
      Ruby::send($self->{'.ruby_obj'}, "end", $tag, $origtext);
    }
  }

  sub text {
    my $self = shift;
    my ($text) = @_;
    if (Ruby::send($self->{'.ruby_obj'}, "respond_to?", "text")) {
      Ruby::send($self->{'.ruby_obj'}, "text", $text);
    }
  }

  sub comment {
    my $self = shift;
    my ($comment) = @_;
    if (Ruby::send($self->{'.ruby_obj'}, "respond_to?", "comment")) {
      Ruby::send($self->{'.ruby_obj'}, "comment", $comment);
    }
  }
!)

class HTMLParser
  def initialize(perl)
    @parser = perl.send("NewHTMLParser", "new", self.id);
  end

  def method_missing(mid, *args)
    @parser.send(mid.id2name, *args)
  end
end

class NewParser<HTMLParser
  def start(tag, attr, attrseq, origtext)
    print "(#{tag}\n"
  end
  def end(tag, origtext)
    print ")#{tag}\n"
  end
  def text(text)
    text.gsub!(/\\/, '\\\\\\')
    text.gsub!(/\n/, '\n')
    print "-#{text}\n"
  end
    
end

parser = NewParser.new(perl)
#parser.strict_comment(1)
parser.parse_file(ARGV[0])
