| 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
$CCCP::HTML::Truncate::enc or $node->ownerDocument->encoding
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | $elips ||= "..."; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$elips ||= "..."
MC/DC100%1/1
$elips ||= "..."
$elips
Truth table100%2/2
A
A= $elips
| |||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | $length ||= 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$length ||= 0
MC/DC0%0/1
$length ||= 0
$length
Truth table50%1/2
A
A= $length
| |||||||||||||||||||||||||||||||||||
| 44 | 3 | $length =~ /(\d+)/; | |||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | $length = $1 ? $1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 46 | 3 | ▶ | return '' unless $length; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | 3 | $html_str =~ s/&/&/gm; | |||||||||||||||||||||||||||||||||
| 48 | 3 | ▶ | return $html_str if length $html_str < $length; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | 3 | my $add_elips = 0; | |||||||||||||||||||||||||||||||||
| 60 | 3 | foreach ($body->ownerDocument->findnodes('//child::text()')) { | |||||||||||||||||||||||||||||||||
| 61 | 12 | ▶ | if ($length>0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$length < 1 and not $add_elips
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||