TOC/OutputHandler/HTML.pm

filter: Statementstmt 38.7% 31 / 80 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 58.3% 7 / 12 Podpod 0.0% 0 / 1 totaltotal 35.1% 38 / 108 CC 11 SCAR 36.5
CC
11
Cov
33%
process_node_list
90.0
_node_level_difference
6.0
_node_level
6.0
CRAP 38.4
↑ Summary TOC/HTML.pm »
1 package Text::TOC::OutputHandler::HTML;
2 {
3 2 $Text::TOC::OutputHandler::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 List::AllUtils qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Text::TOC::Types qw( CodeRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 with 'Text::TOC::Role::OutputHandler';
17  
18 has _toc => (
19 is => 'ro',
20 isa => 'HTML::DOM',
21 init_arg => undef,
22 lazy => 1,
23 0 ▶ default => sub { HTML::DOM->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 2 );
25  
26 2 has _link_generator => (
27 is => 'ro',
28 isa => CodeRef,
29 init_arg => 'link_generator',
30 required => 1,
31 );
32  
33 2 has _style => (
34 is => 'ro',
35 isa => Str,
36 init_arg => 'style',
37 default => 'unordered',
38 );
39  
40  
41 0 ▶sub process_node_list {
Subroutine process_node_list: not called CC=9 SCAR=45.0
Pod undocumented
42 0 my $self = shift;
43 0 my $nodes = shift;
44  
45 0 my $toc = $self->_toc();
46  
47 0 ▶ my $list_tag = $self->_style() eq 'unordered' ? 'ul' : 'ol';
Branch: 1 branch
BranchTrueFalseTotal
$self->_style eq 'unordered' ? : 0 0 0
48  
49 0 my $list = $toc->createElement($list_tag);
50 0 $toc->appendChild($list);
51  
52 0 my $max_level = max map { $self->_node_level($_) } @{$nodes};
53  
54 0 my @lists = $list;
55  
56 0 my $last_node;
57  
58 0 for my $node ( @{$nodes} ) {
59 0 $self->_insert_anchor($node);
60  
61 0 ▶ my $diff
Branch: 1 branch
BranchTrueFalseTotal
$last_node ? : 0 0 0
62 = $last_node
63 ? $self->_node_level_difference( $node, $last_node )
64 : $max_level - $self->_node_level($node);
65  
66 0 ▶ if ( $diff > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 0 0 0
elsif ($diff < 0) { } 0 0 0
67 0 for ( 1..$diff ) {
68 0 my $new_list = $toc->createElement($list_tag);
69 0 my $last_li = $lists[-1]->lastChild();
70  
71 0 ▶ if ( ! $last_li ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($last_li) 0 0 0
72 0 $last_li = $toc->createElement('li');
73 0 $lists[-1]->appendChild($last_li);
74 }
75  
76 0 $last_li->appendChild($new_list);
77  
78 0 push @lists, $new_list;
79 }
80 }
81 elsif ( $diff < 0 ) {
82 0 pop @lists for 1..abs($diff);
83 }
84  
85 0 my $li = $toc->createElement('li');
86 0 my $link = $toc->createElement('a');
87 0 $link->setAttribute(
88 href => $self->_link_generator->( $node ) );
89 0 $link->appendChild( $toc->createTextNode( $node->contents()->as_text() ) );
90  
91 0 $li->appendChild($link);
92  
93 0 $lists[-1]->appendChild($li);
94  
95 0 $last_node = $node;
96 }
97  
98 0 return $toc;
99 }
100  
101 0 ▶sub _node_level_difference {
Subroutine _node_level_difference: not called CC=2 SCAR=17.9
102 0 my $self = shift;
103 0 my $this_node = shift;
104 0 my $last_node = shift;
105  
106 0 ▶ return 0 unless defined $last_node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_node 0 0 0
107  
108 0 return $self->_node_level($last_node) - $self->_node_level($this_node);
109 }
110  
111 {
112 2 my %node_levels = (
113 h1 => 7,
114 h2 => 6,
115 h3 => 5,
116 h4 => 4,
117 h5 => 3,
118 h6 => 2,
119 );
120  
121 0 ▶ sub _node_level {
Subroutine _node_level: not called CC=2 SCAR=17.9
122 0 my $self = shift;
123 0 my $node = shift;
124  
125 0 ▶ return $node_levels{ $node->type() } || 1;
Condition0%0/2
$node_levels{$node->type} || 1
l!l
00
MC/DC0%0/1
$node_levels{$node->type} || 1
$node_levels{$node->type}
Truth table0%0/2
A
A= $node_levels{$node->type}
Aresult
11
00
126 }
127 }
128  
129 2 ▶sub _insert_anchor {
Subroutine _insert_anchor: not called CC=1 SCAR=5.1
130 0 my $self = shift;
131 0 my $node = shift;
132  
133 0 my $domlet = $node->contents();
134 0 my $anchor = $domlet->ownerDocument()->createElement('a');
135 0 $anchor->setAttribute( name => $node->anchor_name() );
136  
137 0 $domlet->insertBefore( $anchor, $domlet->firstChild() );
138  
139 0 return;
140 }
141  
142 2 __PACKAGE__->meta()->make_immutable();
143  
144 2 1;
145  
146 # ABSTRACT: Implements an output handler for HTML documents
147  
148  
149 __END__
↑ Summary TOC/HTML.pm »