blib/lib/CCCP/HTML/Truncate.pm

filter: Statementstmt 89.2% 58 / 65 Branchbran 70.8% 17 / 24 Conditioncond 53.8% 7 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 78.6% 92 / 117 CC 20 SCAR 26.3
CC
20
Cov
80%
truncate
20.2
html
3.8
_init_parser
2.0
CRAP 13.9
↑ Summary
1 package CCCP::HTML::Truncate;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.04';
10  
11 1 $CCCP::HTML::Truncate::enc = 'utf-8';
12  
13 # ------------------------ EXTEND XML::LibXML::Element -----------------
14 # return serialize XML::LibXML::Element in correct encoding
15 3 ▶sub XML::LibXML::Element::html {
Subroutine html: called CC=3 SCAR=13.3
16 3 my ($node, $actualEncoding) = @_;
17
18 # correct decode
19 3 ▶ my $f = Encode::find_encoding($CCCP::HTML::Truncate::enc || $node->ownerDocument->encoding() || $node->ownerDocument->actualEncoding());
Condition33%2/6
$CCCP::HTML::Truncate::enc || $node->ownerDocument->encoding || $node->ownerDocument->actualEncoding
l!l&&r!l&&!r
300
$CCCP::HTML::Truncate::enc or $node->ownerDocument->encoding
l!l&&r!l&&!r
300
MC/DC0%0/3
$CCCP::HTML::Truncate::enc || $node->ownerDocument->encoding || $node->ownerDocument->actualEncoding
$CCCP::HTML::Truncate::enc $node->ownerDocument->encoding $node->ownerDocument->actualEncoding
Truth table25%1/4
A or B || C
A= $CCCP::HTML::Truncate::encB= $node->ownerDocument->encodingC= $node->ownerDocument->actualEncoding
ABCresult
1XX1
01X1
0011
0000
20
21 3 return $f->encode($node->toString,Encode::FB_XMLCREF);
22 }
23  
24 # ---------------------------------------- MAIN --------------------------------------------
25  
26 # parser obj
27 1 my $lx;
28  
29 3 ▶sub _init_parser {
Subroutine _init_parser: called CC=2 SCAR=6.9
30 3 ▶ return if $lx;
Branch: 1 branch
BranchTrueFalseTotal
if $lx 2 1 3
31 1 $lx = XML::LibXML->new();
32 1 $lx->recover_silently(1);
33 }
34  
35 # truncate html
36 4 ▶sub truncate {
Subroutine truncate: called CC=17 SCAR=30.0
Pod documented
37 4 my ($class,$html_str,$length,$elips) = @_;
38
39 4 ▶ return unless $html_str;
Branch: 1 branch
BranchTrueFalseTotal
unless $html_str 1 3 4
40
41 3 ▶ $elips ||= "...";
Condition100%2/2
$elips ||= "..."
l!l
21
MC/DC100%1/1
$elips ||= "..."
$elips
Truth table100%2/2
A
A= $elips
Aresult
11
00
42
43 3 ▶ $length ||= 0;
Condition50%1/2
$length ||= 0
l!l
30
MC/DC0%0/1
$length ||= 0
$length
Truth table50%1/2
A
A= $length
Aresult
11
00
44 3 $length =~ /(\d+)/;
45 3 ▶ $length = $1 ? $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 3 0 3
46 3 ▶ return '' unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 3 3
47 3 $html_str =~ s/&/&/gm;
48 3 ▶ return $html_str if length $html_str < $length;
Branch: 1 branch
BranchTrueFalseTotal
if length $html_str < $length 0 3 3
49
50 3 my $f = Encode::find_encoding($CCCP::HTML::Truncate::enc);
51 3 $html_str = $f->decode($html_str);
52 3 $elips = $f->decode($elips);
53
54 3 $class->_init_parser();
55 3 my $root = $lx->parse_html_string($html_str);
56 3 my ($body) = $root->documentElement()->findnodes('//body');
57 3 ▶ return '' unless $body;
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 3 3
58
59 3 my $add_elips = 0;
60 3 foreach ($body->ownerDocument->findnodes('//child::text()')) {
61 12 ▶ if ($length>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) { } 8 4 12
62 8 my $str = $_->to_literal;
63 8 my $new_str = substr($str,0,$length);
64 8 $length -= length $str;
65 8 ▶ if ($length < 1 and not $add_elips) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < 1 and not $add_elips) 2 6 8
Condition66%2/3
$length < 1 and not $add_elips
!ll&&!rl&&r
602
MC/DC50%1/2
$length < 1 and not $add_elips
$length < 1 not $add_elips
Truth table66%2/3
A and not(B)
A= $length < 1B= not $add_elips
ABresult
0X0
100
111
66 2 $new_str .= $elips;
67 2 $add_elips++;
68 # and skip all another text child
69 2 my $text_parent = $_->parentNode;
70 2 ▶ if ($_->nodePath =~ /\[(\d+)]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->nodePath =~ /\[(\d+)]$/) 0 2 2
71 0 foreach my $skip_text ($text_parent->findnodes(sprintf('//child::text()[position()>%d]',$1))) {
72 0 $_->setData('');
73 };
74 }
75 };
76 8 $_->setData($new_str);
77 } else {
78 4 my $parent = $_->parentNode;
79 # add elips
80 4 ▶ unless ($add_elips) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($add_elips) 0 4 4
81 0 $add_elips++;
82 0 my $elips_el = XML::LibXML::Element->new('span');
83 0 $elips_el->appendTextNode($elips);
84 0 $parent->addChild($elips_el);
85 };
86 # skip body
87 4 ▶ if ($parent->isSameNode($body)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isSameNode($body)) { } 0 4 4
88 0 $_->unbindNode();
89 } else {
90 4 my @childs = $parent->findnodes($parent->nodePath.'//child::text()');
91 4 ▶ $#childs > 0 ? $_->unbindNode() : $parent->unbindNode();
Branch: 1 branch
BranchTrueFalseTotal
$#childs > 0 ? : 2 2 4
92 }
93 }
94 };
95
96 3 my $ret = $body->html();
97 3 $ret =~ s/^<body(.*?)>(<p>)?|(<\/p>)?<\/body>$//igm;
98 3 return $ret;
99 }
100  
101 1 1;
102 __END__
↑ Summary