HTMLEmbed/Entry.pm

filter: Statementstmt 74.4% 105 / 141 Branchbran 33.3% 14 / 42 Conditioncond 39.1% 9 / 23 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 76.9% 20 / 26 Podpod 100% 3 / 3 totaltotal 62.2% 153 / 246 CC 36 SCAR 31.0
CC
36
Cov
62%
_toc_in_section
110.0
_section_link
12.0
_build__tree
10.1
CRAP 22.2
↑ Summary HTMLEmbed.pm »
1 package Pod::HTMLEmbed::Entry;
2 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 4 ▶use Carp::Clan '^(Mo[ou]se::|Pod::HTMLEmbed(::)?)';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Pod::Simple::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use HTML::TreeBuilder;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use List::Util qw/min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 has file => (
12 is => 'ro',
13 isa => 'Str',
14 required => 1,
15 );
16  
17 4 has [qw/name title body toc/] => (
18 is => 'ro',
19 isa => 'Str',
20 lazy_build => 1,
21 );
22  
23 4 has _tree => (
24 is => 'ro',
25 isa => 'HTML::TreeBuilder',
26 lazy_build => 1,
27 );
28  
29 4 has _context => (
30 is => 'ro',
31 isa => 'Pod::HTMLEmbed',
32 required => 1,
33 handles => ['url_prefix', 'has_url_prefix'],
34 );
35  
36 4 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶sub sections {
Subroutine sections: called CC=1 SCAR=0.0
Pod documented
39 2 my $self = shift;
40 2 map { $_->content_list } $self->_tree->find('h2');
41 }
42  
43 16 ▶sub section {
Subroutine section: called CC=5 SCAR=16.6
Pod documented
44 16 my ($self, $section_name) = @_;
45 16 ▶ croak 'section_name is required' unless $section_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $section_name 0 16 16
46  
47 my $section = $self->_tree->look_down(
48 _tag => 'h2',
49 55 ▶ sub { $_[0]->content->[0] eq $section_name },
Subroutine __ANON__: called CC=1 SCAR=0.0
50 16 );
51  
52 16 my $content = q[];
53 16 ▶ while ($section and $section = $section->right and $section->tag ne 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($section and $section = $section->right and $section->tag ne "h2") 16 16 32
Condition50%3/6
$section and $section = $section->right and $section->tag ne "h2"
!ll&&!rl&&r
01616
$section and $section = $section->right
!ll&&!rl&&r
0032
MC/DC33%1/3
$section and $section = $section->right and $section->tag ne "h2"
$section $section = $section->right $section->tag ne "h2"
Truth table50%2/4
A and B and C
A= $sectionB= $section = $section->rightC= $section->tag ne "h2"
ABCresult
0XX0
10X0
1100
1111
54 16 $content .= $section->as_XML . "\n";
55 }
56  
57 16 $content;
58 }
59  
60 3 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.1
Pod documented
61 3 my $self = shift;
62  
63 3 ▶ if ($self->_has_tree) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_tree) 3 0 3
64 3 $self->_tree->delete;
65 }
66 }
67  
68 2 ▶sub _build_name {
Subroutine _build_name: called CC=1 SCAR=0.0
69 2 my $self = shift;
70  
71 2 (my $name = $self->section('NAME')) =~ s/\s*-.*$//s;
72 2 $name =~ s/<.*?>//gs;
73 2 $name;
74 }
75  
76 2 ▶sub _build_title {
Subroutine _build_title: called CC=2 SCAR=7.0
77 2 my $self = shift;
78  
79 2 ▶ my ($title) = $self->section('NAME') =~ / - (.*)/ or return '';
Branch: 1 branch
BranchTrueFalseTotal
my($title) = $self->section("NAME") =~ / - (.*)/ or return "" 2 0 2
80 2 $title =~ s/<.*?>//g;
81 2 $title;
82 }
83  
84 0 ▶sub _build_body {
Subroutine _build_body: not called CC=2 SCAR=17.9
85 0 my $self = shift;
86  
87 0 my $body = q[];
88 0 for my $content ($self->_tree->find('body')->content_list) {
89 0 $body .= $content->as_XML . "\n";
90 }
91 0 $body;
92 }
93  
94 0 ▶sub _build_toc {
Subroutine _build_toc: not called CC=2 SCAR=17.9
95 0 my $self = shift;
96  
97 0 my $toc = '<ul>';
98  
99 0 for my $section ($self->sections) {
100 0 $toc .= '<li>' . $self->_section_link($section);
101 0 $toc .= $self->_toc_in_section($section);
102 0 $toc .= '</li>';
103 }
104 0 $toc .= '</ul>';
105  
106 0 $toc;
107 }
108  
109 0 ▶sub _section_link {
Subroutine _section_link: not called CC=3 SCAR=24.8
110 0 my ($self, $section) = @_;
111  
112 0 ▶ unless (ref $section) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $section) 0 0 0
113 $section = $self->_tree->look_down(
114 0 ▶ _tag => 'h2', sub { $_[0]->content->[0] eq $section },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 ▶ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$section = $self->_tree->look_down("_tag", "h2", sub { $_[0]->content->[0] eq $section; } ) or return 0 0 0
116 }
117  
118 0 my $text = HTML::Entities::encode_entities($section->as_trimmed_text, '<>&"');
119 0 my $escaped_text = URI::Escape::uri_escape($section->as_trimmed_text);
120  
121 0 return join '', '<a href="#', $escaped_text, '">', $text, '</a>';
122 }
123  
124 0 ▶sub _toc_in_section {
Subroutine _toc_in_section: not called CC=10 SCAR=47.0
125 0 my ($self, $section_name) = @_;
126  
127 my $section = $self->_tree->look_down(
128 _tag => 'h2',
129 0 ▶ sub { $_[0]->content->[0] eq $section_name },
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 ▶ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->_tree->look_down("_tag", "h2", sub { $_[0]->content->[0] eq $section_name; } ) or return 0 0 0
131  
132 0 my $toc = q[];
133 0 my $num = 2;
134  
135 0 ▶ while ($section and $section = $section->right and $section->tag ne 'h2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($section and $section = $section->right and $section->tag ne "h2") 0 0 0
Condition0%0/6
$section and $section = $section->right and $section->tag ne "h2"
!ll&&!rl&&r
000
$section and $section = $section->right
!ll&&!rl&&r
000
MC/DC0%0/3
$section and $section = $section->right and $section->tag ne "h2"
$section $section = $section->right $section->tag ne "h2"
Truth table0%0/4
A and B and C
A= $sectionB= $section = $section->rightC= $section->tag ne "h2"
ABCresult
0XX0
10X0
1100
1111
136 0 ▶ next unless $section->tag =~ /^h[2-6]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $section->tag =~ /^h[2-6]$/ 0 0 0
137 0 my ($n) = $section->tag =~ /(\d)/;
138  
139 0 ▶ if ($num < $n) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num < $n) { } 0 0 0
elsif ($num == $n) { } 0 0 0
140 0 $toc .= join '', '<ul><li>', $self->_section_link($section);
141 }
142 elsif ($num == $n) {
143 0 $toc .= join '', '</li><li>', $self->_section_link($section);
144 }
145 else {
146 0 $toc .= join '', '</li></ul></li><li>', $self->_section_link($section);
147 }
148  
149 0 $num = $n;
150 }
151 0 ▶ return '' unless $toc;
Branch: 1 branch
BranchTrueFalseTotal
unless $toc 0 0 0
152  
153 0 ▶ $toc .= '</li></ul>' while $num-- > 2;
Branch: 1 branch
BranchTrueFalseTotal
$num-- > 2 and do { $toc .= '</li></ul>' } 0 0 0
154 0 $toc;
155 }
156  
157 3 ▶sub _build__tree {
Subroutine _build__tree: called CC=10 SCAR=23.1
158 3 my $self = shift;
159  
160 3 my $html = $self->_parse_pod($self->file);
161  
162 3 my $tree = HTML::TreeBuilder->new;
163 3 $tree->parse_content( $html );
164  
165 # remove white spaces in codeblocks
166 3 ▶ my @codes = $tree->look_down( _tag => 'code', sub { $_[0]->parent->tag eq 'pre' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
167 3 $self->_strip_tree($_) for @codes;
168  
169 # remove first num strings in <ol> tag
170 3 ▶ my @list = $tree->look_down( _tag => 'li', sub { $_[0]->parent->tag eq 'ol' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
171 3 for my $li (@list) {
172 4 ▶ my $first_child = shift @{ $li->content_array_ref } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $first_child = shift @{$li->content_array_ref;} or next 4 0 4
173 4 ▶ $first_child =~ s/^\d+\.\s+// unless ref $first_child;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $first_child 0 4 4
174 4 $li->unshift_content($first_child);
175 }
176  
177 # remove first <p> from lists
178 3 @list = $tree->look_down( _tag => 'li' );
179 3 for my $li (@list) {
180 12 my @children = $li->content_list;
181 12 my $num_element = grep { ref $_ } @children;
182  
183 12 ▶ if (1 == $num_element and my $p = $children[0] and $children[0]->tag eq 'p') {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == $num_element and my $p = $children[0] and $children[0]->tag eq "p") 8 4 12
Condition66%4/6
1 == $num_element and my $p = $children[0] and $children[0]->tag eq "p"
!ll&&!rl&&r
408
1 == $num_element and my $p = $children[0]
!ll&&!rl&&r
408
MC/DC33%1/3
1 == $num_element and my $p = $children[0] and $children[0]->tag eq "p"
1 == $num_element my $p = $children[0] $children[0]->tag eq "p"
Truth table50%2/4
A and B and C
A= 1 == $num_elementB= my $p = $children[0]C= $children[0]->tag eq "p"
ABCresult
0XX0
10X0
1100
1111
184 8 $p->replace_with_content;
185 }
186 }
187  
188 # shift header level, and add id attr
189 3 my @header = $tree->look_down( _tag => qr/^h[1-5]$/ );
190 3 for my $header (@header) {
191 19 my ($n) = $header->tag =~ /(\d)/;
192 19 $header->tag( 'h' . ++$n );
193 19 $header->attr( id => $header->as_trimmed_text );
194 }
195  
196 3 $tree;
197 }
198  
199 3 ▶sub _parse_pod {
Subroutine _parse_pod: called CC=3 SCAR=11.1
200 3 my ($self, $file) = @_;
201  
202 3 my $p = Pod::Simple::XHTML->new;
203 3 $p->html_header('');
204 3 $p->html_footer('');
205  
206 3 $p->output_string(\my $html);
207 3 ▶ $p->perldoc_url_prefix( $self->url_prefix ) if $self->has_url_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_url_prefix 0 3 3
208  
209 3 ▶ $p->parse_file($file)
Branch: 1 branch
BranchTrueFalseTotal
$p->parse_file($file) or croak("Pod parse error: $!") 3 0 3
210 or croak "Pod parse error: $!";
211  
212 3 $html;
213 }
214  
215 4 ▶sub _strip_tree {
Subroutine _strip_tree: called CC=2 SCAR=7.0
216 4 my ($self, $code) = @_;
217 4 my $stripped = _strip($code->content_list);
218 4 ▶ $stripped .= "\n" unless $stripped =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stripped =~ /\n$/ 4 0 4
219  
220 4 $code->delete_content;
221 4 $code->push_content($stripped);
222 }
223  
224 # copy from String::TT::strip
225 4 ▶sub _strip($){
Subroutine _strip: called CC=5 SCAR=16.6
226 4 my $lines = shift;
227  
228 4 my $trailing_newline = ($lines =~ /\n$/s);# perl silently throws away data
229 4 my @lines = split "\n", $lines;
230 4 ▶ shift @lines if $lines[0] eq ''; # strip empty leading line
Branch: 1 branch
BranchTrueFalseTotal
if $lines[0] eq "" 0 4 4
231  
232 # determine indentation level
233 4 ▶ my @spaces = map { /^(\040+)/ and length $1 or 0 } grep { !/^\s*$/ } @lines;
Condition40%2/5
/^(\040+)/ && length $1 || 0
l!l
40
/^(\040+)/ and length $1
!ll&&!rl&&r
004
MC/DC0%0/2
/^(\040+)/ && length $1 || 0
/^(\040+)/ length $1
Truth table33%1/3
A and B
A= /^(\040+)/B= length $1
ABresult
111
0X0
100
234  
235 4 my $indentation_level = min(@spaces);
236  
237 # strip off $indentation_level spaces
238 my $stripped = join "\n", map {
239 4 my $copy = $_;
240 4 substr($copy,0,$indentation_level) = "";
241 4 $copy;
242 } @lines;
243  
244 4 ▶ $stripped .= "\n" if $trailing_newline;
Branch: 1 branch
BranchTrueFalseTotal
if $trailing_newline 0 4 4
245 4 return $stripped;
246 }
247  
248 4 __PACKAGE__->meta->make_immutable;
249  
250 __END__
↑ Summary HTMLEmbed.pm »