| 1 | package Text::TOC::InputHandler::HTML; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 2 | $Text::TOC::InputHandler::HTML::VERSION = '0.10'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use HTML::DOM; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | ▶ | use HTML::Entities qw( encode_entities ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 2 | ▶ | use Text::TOC::Node::HTML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | ▶ | use Text::TOC::Types qw( Int ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 2 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 2 | with 'Text::TOC::Role::InputHandler'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 2 | has _counter => ( | |||||||||||||||||||
| 21 | traits => ['Counter'], | ||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||
| 23 | isa => Int, | ||||||||||||||||||||
| 24 | default => 0, | ||||||||||||||||||||
| 25 | handles => { _inc_counter => 'inc' }, | ||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 2 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | ▶ | sub _process_file { | ||||||||||||||||||
|
Subroutine
_process_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||
| 32 | 0 | my $file = shift; | |||||||||||||||||||
| 33 | 0 | my $content = shift; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | my $dom = HTML::DOM->new(); | |||||||||||||||||||
| 36 | 0 | $dom->write($content); | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | ▶ | $self->_walk_nodes( $dom->body() || $dom, $file ); | ||||||||||||||||||
|
Condition0%0/3
$dom->body || $dom
MC/DC0%0/2
$dom->body || $dom
$dom->body
$dom
Truth table0%0/3
A || B
A= $dom->bodyB= $dom
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | return $dom; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | ▶ | sub _walk_nodes { | ||||||||||||||||||
|
Subroutine
_walk_nodes: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||||||||||
| 45 | 0 | my $parent = shift; | |||||||||||||||||||
| 46 | 0 | my $file = shift; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | for my $node ( grep { $_->isa('HTML::DOM::Element') } | |||||||||||||||||||
| 49 | $parent->childNodes() ) { | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | if ( $self->_filter()->node_is_interesting($node) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | $self->_save_node( $node, $file ); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||
| 55 | 0 | $self->_walk_nodes( $node, $file ); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | ▶ | sub _save_node { | ||||||||||||||||||
|
Subroutine
_save_node: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||||||||||
| 64 | 0 | my $node = shift; | |||||||||||||||||||
| 65 | 0 | my $file = shift; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | my $wrapped = Text::TOC::Node::HTML->new( | |||||||||||||||||||
| 68 | type => lc $node->tagName(), | ||||||||||||||||||||
| 69 | contents => $node, | ||||||||||||||||||||
| 70 | anchor_name => $self->_anchor_name($node), | ||||||||||||||||||||
| 71 | source_file => $file, | ||||||||||||||||||||
| 72 | ); | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | $self->_add_node($wrapped); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | return; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 0 | ▶ | sub _anchor_name { | ||||||||||||||||||
|
Subroutine
_anchor_name: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 80 | 0 | my $self = shift; | |||||||||||||||||||
| 81 | 0 | my $domlet = shift; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 0 | my $text_contents = $domlet->as_text(); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $text_contents =~ s/\s+/_/g; | |||||||||||||||||||
| 86 | # These are the only characters allowed in a name according to the HTML | ||||||||||||||||||||
| 87 | # spec. | ||||||||||||||||||||
| 88 | 0 | $text_contents =~ s/[^A-Za-z0-9-_:.]//g; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | my $name = encode_entities($text_contents) . q{-} . $self->_counter(); | |||||||||||||||||||
| 91 | # Anchors must begin with a letter. | ||||||||||||||||||||
| 92 | 0 | ▶ | $name = 'A-' . $name unless $name =~ /^[A-Za-z]/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | $self->_inc_counter(); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | return $name; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | 1; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | # ABSTRACT: Implements an input handler for HTML documents | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||