Aozora2Epub

 view release on metacpan or  search on metacpan

lib/Aozora2Epub/XHTML/Tree.pm  view on Meta::CPAN

package Aozora2Epub::XHTML::Tree;
use strict;
use warnings;
use utf8;
use HTML::TreeBuilder::XPath;
use HTML::Selector::XPath qw/selector_to_xpath/;

our $VERSION = '0.05';

sub new {
    my ($class, $str) = @_;

    my $tree= HTML::TreeBuilder::XPath->new;
    $tree->ignore_unknown(0);
    $tree->store_comments(1);
    $tree->parse($str);
    $tree->eof;
    my $dummy_node = HTML::Element->new_from_lol(['dummy', $tree->guts]);
    my $obj = bless {tree=>$tree, result=>$dummy_node}, $class;
}

sub _selector {
    my $selector = shift;
    if ($selector =~ m{(?:/|id\()}) {
        # XPath
        $selector =~ s{^/([^/])}{/dummy/$1};
        return $selector;
    }
    return selector_to_xpath($selector);
}

sub _select {
    my ($self, $selector) = @_;
    $selector = _selector($selector);
    return [ $self->_result->findnodes($selector) ];
}

sub _apply(&$) { ## no critic (ProhibitSubroutinePrototypes)
    _apply0(@_);
}

sub _apply0 {
    my ($sub, $elem) = @_;
    if ($elem->isa('HTML::Element')) {
        return $sub->($elem);
    }
    return $elem;
}

sub _map_apply(&@) { ## no critic (ProhibitSubroutinePrototypes)
    my ($sub, @nodes) = @_;
    return map { _apply0($sub, $_) } @nodes;
}

sub _result {
    my ($self, $nodes) = @_;
    return $self->{result} unless $nodes;
    _apply { $_->detach } $_ for @$nodes;
    $self->{result} = HTML::Element->new_from_lol(['dummy', @$nodes]);
}


sub select {
    my ($self, $selector) = @_;



( run in 1.432 second using v1.01-cache-2.11-cpan-99c4e6809bf )