Tripletail
view release on metacpan or search on metacpan
ext/Tripletail-HtmlFilter/HtmlFilter.pm view on Meta::CPAN
$XS_LOADERROR = 'disabled';
}
do
{
no strict 'refs';
#$err and chomp $err;
#warn "warning: $err";
foreach my $name (@XSUBS)
{
my $xsub = __PACKAGE__.'::'.$name;
if( !defined(&$xsub) )
{
(my $ppsub = $xsub) =~ s/(\w+)$/_$1_pp/;
*$xsub = \&$ppsub;
}
}
}
}
1;
sub _new {
my $class = shift;
my $opts = { @_ };
my $this = bless [] => $class;
$this->[INTEREST] = $opts->{interest};
$this->[TRACK] = $opts->{track};
$this->[FILTER_TEXT] = $opts->{filter_text};
$this->[FILTER_COMMENT] = $opts->{filter_comment};
$this->[CONTEXT] = Tripletail::HtmlFilter::Context->_new;
$this->[HTML] = undef; # æåå
$this->[OUTPUT] = []; # Tripletail::HtmlFilter::{Element,Text,Comment}
# interest, trackã«æ¸¡ãããæ£è¦è¡¨ç¾ã¯ãã®æç¹ã§ CODE ã«ã³ã³ãã¤ã«ãã¦ããã
if ($this->[INTEREST]) {
$this->[INTEREST] = $this->_compile_matcher($this->[INTEREST]);
}
if ($this->[TRACK]) {
$this->[TRACK] = $this->_compile_matcher($this->[TRACK]);
}
$this;
}
sub set {
my $this = shift;
my $html = shift;
if (not defined $html) {
die __PACKAGE__."#set: ARG[1] is not defined.\n";
}
elsif (ref $html) {
die __PACKAGE__."#set: ARG[1] is a Ref.\n";
}
#@{$this->[HTML]} = split m/(<.+?>)/s, $html;
# âã§ã¯ã<!-- <hoge> -->ãæ£ããè§£æã§ããªããçé¢ç®ã«ãã¼ãºããå¿
è¦ããã
# ããããperlã§çé¢ç®ã«ãã¼ã¶ãæ¸ãã®ã¯é常ã«é¢åãªã®ã§æ£è¦è¡¨ç¾ã§èª¤éåã
# NB: ä»ã«ããæ£ããè§£æã§ããªããã¿ã¼ã³ãåå¨ãããã
@{$this->[HTML]} = split m/((?:<!--.*?-->)|(?:<.+?>))/s, $html;
@{$this->[OUTPUT]} = ();
$this;
}
sub toStr {
my $this = shift;
$this->[CONTEXT]->_flush($this); # æªç¢ºå®ã®é¨åã確å®ãã
join('', map {ref($_)?$_->toStr:$_} @{$this->[OUTPUT]});
}
sub _compile_matcher {
my $this = shift;
my $regexes = shift;
my $joined = join('', @$regexes);
if (my $cached = $_MATCHER_CACHE{$joined}) {
return $cached;
}
my $ret = [];
foreach my $reg (@$regexes) {
if (ref($reg) eq 'Regexp') {
# ã³ã³ãã¤ã«æ¸ã¿æ£è¦è¡¨ç¾ã ã£ãã
push @$ret, sub {
return 1 if $_[0] =~ $reg;
};
}
else {
# åç´ãªæååã ã£ãã
push @$ret, lc $reg;
}
}
$_MATCHER_CACHE{$joined} = $ret;
$ret;
}
sub _next_pp {
my $this = shift;
$this->[CONTEXT]->_flush($this); # æªç¢ºå®ã®é¨åã確å®ãã
while (@{$this->[HTML]}) {
my $str = shift @{$this->[HTML]};
my $parsed;
my $interested;
if ($str =~ m/^<!--\s*(.+?)\s*-->$/) {
# ã³ã¡ã³ã
if ($this->[FILTER_COMMENT]) {
$interested = $this->[CONTEXT]->newComment($1);
}
} elsif ($str =~ m/^</) {
# è¦ç´
if ($this->[TRACK] or $this->[INTEREST]) {
($interested,$parsed) = $this->_next_elem($str);
}
} else {
# ããã¹ã
( run in 0.939 second using v1.01-cache-2.11-cpan-71847e10f99 )