# -*- cperl -*-
# -----------------------------------------------------------------------------
# $Id: LinkExtractor.pm,v 1.3 2004/10/12 10:26:43 admin Exp $
# -----------------------------------------------------------------------------
package LinkExtractor;
use strict;
use warnings;
use LinkExtractor::HTML;
use LinkExtractor::CSS;
use Carp;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(can_extract_links extract_links);

sub can_extract_links ($) {
    my $ctype = shift;

    defined _get_extractor($ctype);
}

sub extract_links ($$) {
    # $source: 解析對象の文字列
    # $ctype : Content-Type
    # 戻り値: [] || [[offset, tagname, link, base-url?], ...]
    # tagnameはHTML向けであり、その他のパーサはundefを入れる。
    my ($source, $ctype) = @_;
    defined $source or croak "extract_links: source is undef";

    if ($_ = _get_extractor($ctype)) {
	return $_->($source);
    }
    else {
	return [];
    }
}

sub _get_extractor {
    my $ctype = shift;

    if (not defined $ctype) {
	return;
    }
    elsif ($ctype =~ m!^text/x?html! or
	  $ctype =~ m!^application/xhtml\+xml!) {
	return \&LinkExtractor::HTML::extract;
    }
    elsif ($ctype =~ m!^text/css!) {
	return \&LinkExtractor::CSS::extract;
    }
    else {
	return;
    }
}

1;
