blib/lib/HTML/Embellish.pm

filter: Statementstmt 95.0% 134 / 141 Branchbran 84.3% 54 / 64 Conditioncond 70.0% 21 / 30 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 10 / 10 Podpod 75.0% 3 / 4 totaltotal 86.1% 230 / 267 SCAR 38.9
CC
46
Cov
89%
process
34.7
new
8.0
processTextRefs
8.0
CRAP 48.9
↑ Summary
1 #---------------------------------------------------------------------
2 package HTML::Embellish;
3 #
4 # Copyright 2010 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: October 8, 2006
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Typographically enhance HTML trees
18 #---------------------------------------------------------------------
19  
20 4 ▶use 5.008; # Need good Unicode support; Perl 5.10 recommended but 5.8 may work
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use Exporter 5.57 'import'; # exported import method
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ###open(LOG, '>:utf8', 'em.log');
28  
29 #=====================================================================
30 # Package Global Variables:
31  
32 4 our $VERSION = '1.001';
33 # This file is part of HTML-Embellish 1.001 (April 11, 2015)
34  
35 4 our @EXPORT = qw(embellish);
36  
37 4 my $mdash = chr(0x2014);
38 4 my $lsquo = chr(0x2018);
39 4 my $rsquo = chr(0x2019);
40 4 my $ldquo = chr(0x201C);
41 4 my $rdquo = chr(0x201D);
42 4 my $hellip = chr(0x2026);
43  
44 4 my $notQuote = qq/[^\"$ldquo$rdquo]/;
45 4 my $balancedQuoteString = qr/(?: (?>[^ \t\n\r\pP]+)
46 | (?= [ \t\n\r\pP])$notQuote
47 | $ldquo (?>$notQuote*) $rdquo )*/x;
48  
49 #=====================================================================
50 # Constants:
51 #---------------------------------------------------------------------
52  
53 BEGIN
54 {
55 4 ▶ my $i = 0;
Subroutine BEGIN: called CC=2 SCAR=6.9
56 4 for (qw(textRefs fixQuotes fixDashes fixEllipses fixEllipseSpace fixHellip
57 totalFields)) {
58 ## no critic (ProhibitStringyEval)
59 28 eval "sub $_ () { $i }";
60 28 ++$i;
61 }
62 } # end BEGIN
63  
64 #=====================================================================
65 # Exported functions:
66 #---------------------------------------------------------------------
67 sub embellish
68 {
69 73 ▶ my $html = shift @_;
Subroutine embellish: called CC=3 SCAR=11.0
Pod documented
70  
71 73 ▶ croak "First parameter of embellish must be an HTML::Element"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $html and $html->can("content_refs_list") 1 72 73
Condition66%2/3
ref $html and $html->can("content_refs_list")
!ll&&!rl&&r
1072
MC/DC50%1/2
ref $html and $html->can("content_refs_list")
ref $html $html->can("content_refs_list")
Truth table66%2/3
A and B
A= ref $htmlB= $html->can("content_refs_list")
ABresult
0X0
100
111
72 unless ref $html and $html->can('content_refs_list');
73  
74 72 my $e = HTML::Embellish->new(@_);
75 71 $e->process($html);
76 } # end embellish
77  
78 #=====================================================================
79 # Class Methods:
80 #---------------------------------------------------------------------
81 sub new
82 {
83 73 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
84 73 ▶ croak "Odd number of parameters passed to HTML::Embellish->new" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 72 73
85 72 my %parms = @_;
86  
87 72 my $self = [ (undef) x totalFields ];
88 72 bless $self, $class;
89  
90 72 ▶ my $def = (exists $parms{default} ? $parms{default} : 1);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'default'} ? : 9 63 72
91  
92 72 $self->[textRefs] = undef;
93 72 ▶ $self->[fixDashes] = (exists $parms{dashes} ? $parms{dashes} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'dashes'} ? : 1 71 72
94 72 ▶ $self->[fixEllipses] = (exists $parms{ellipses} ? $parms{ellipses} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'ellipses'} ? : 4 68 72
95 72 ▶ $self->[fixQuotes] = (exists $parms{quotes} ? $parms{quotes} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'quotes'} ? : 2 70 72
96  
97 $self->[fixHellip] = (exists $parms{hellip}
98 72 ▶ ? $parms{hellip} : $self->[fixEllipses]);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'hellip'} ? : 0 72 72
99 $self->[fixEllipseSpace] = (exists $parms{space_ellipses}
100 72 ▶ ? $parms{space_ellipses} : $self->[fixEllipses]);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'space_ellipses'} ? : 0 72 72
101  
102 72 return $self;
103 } # end new
104  
105 #---------------------------------------------------------------------
106 # Convert quotes & apostrophes into curly quotes:
107 #
108 # Input:
109 # self: The HTML::Embellish object
110 # refs: Arrayref of stringrefs to the text of this paragraph
111  
112 sub processTextRefs
113 {
114 69 ▶ my ($self, $refs) = @_;
Subroutine processTextRefs: called CC=8 SCAR=20.8
Pod undocumented
115  
116 69 local $_ = join('', map { $$_ } @$refs);
117 69 utf8::upgrade($_);
118  
119 69 my $fixQuotes = $self->[fixQuotes];
120 69 ▶ if ($fixQuotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixQuotes) 65 4 69
121 65 s/\("/($ldquo/g;
122 65 s/"\)/$rdquo)/g;
123  
124 65 s/^([\xA0\s]*)"/$1$ldquo/;
125 65 s/(?<=[\s\pZ])"(?=[^\s\pZ])/$ldquo/g;
126 65 s/(?<=\pP)"(?=\w)/$ldquo/g;
127 65 s/(?<=[ \t\n\r])"(?=\xA0)/$ldquo/g;
128  
129 65 s/"[\xA0\s]*$/$rdquo/;
130 65 s/(?<![\s\pZ])"(?=[\s\pZ])/$rdquo/g;
131 65 s/(?<=\w)"(?=\pP)/$rdquo/g;
132 65 s/(?<=\xA0)"(?=[ \t\n\r]|[\s\xA0]+$)/$rdquo/g;
133 65 s/(?<=[,;.!?])"(?=[-$mdash])/$rdquo/go;
134  
135 65 s/'(?=
136 (?: angmans?
137 | aves?
138 | cause
139 | cept
140 | d
141 | e[mr]?e?
142 | fraidy?
143 | gainst
144 | igh\w*
145 | im
146 | m
147 | n
148 | nam
149 | nothers?
150 | nuff
151 | onou?rs?
152 | re?
153 | rithmetic
154 | s
155 | scuse
156 | spects?
157 | t
158 | til
159 | tisn?
160 | tw(?:asn?|ere?n?|ould\w*)
161 | ud
162 | uns?
163 ) \b
164 | \d\d\W?s
165 | \d\d(?!\w)
166 )
167 /$rsquo/igx;
168  
169 65 s/'([ \xA0]?$rdquo)/$rsquo$1/go;
170  
171 65 s/`/$lsquo/g;
172 65 s/^'/$lsquo/;
173 65 s/(?<=[\s\pZ])'(?=[^\s\pZ])/$lsquo/g;
174 65 s/(?<=\pP)(?<![.!?])'(?=\w)/$lsquo/g;
175 65 s/(?<=[ \t\n\r])'(?=\xA0)/$lsquo/g;
176  
177 65 s/'/$rsquo/g;
178  
179 65 s/(?<!\PZ)"([\xA0\s]+$lsquo)/$ldquo$1/go;
180 65 s/(${rsquo}[\xA0\s]+)"(?!\PZ)/$1$rdquo/go;
181  
182 65 ▶ if (/"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/"/) 1 64 65
183 1 ▶ 1 while s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) "
Branch: 1 branch
BranchTrueFalseTotal
s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) " /$1$ldquo/ox || s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/ox and do { '???' } 1 1 2
Condition66%2/3
s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) " /$1$ldquo/ox or s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/ox
l!l&&r!l&&!r
011
MC/DC50%1/2
s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) " /$1$ldquo/ox or s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/ox
s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) " /$1$ldquo/ox s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/ox
Truth table66%2/3
A or B
A= s/^($balancedQuoteString (?![\"$ldquo$rdquo])[ \t\n\r\pP]) " /$1$ldquo/oxB= s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/ox
ABresult
1X1
011
000
184 /$1$ldquo/xo
185 or s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/xo;
186 } # end if straight quotes remaining in string
187  
188 #s/(?<=\p{IsPunct})"(?=\p{IsAlpha})/$ldquo/go;
189 65 s/(?<=[[:punct:]])"(?=[[:alpha:]])/$ldquo/go;
190  
191 65 s/${ldquo}\s([$lsquo$rsquo])/$ldquo\xA0$1/go;
192 65 s/${rsquo}\s$rdquo/$rsquo\xA0$rdquo/go;
193 } # end if fixQuotes
194  
195 69 ▶ if ($self->[fixEllipses]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 67 2 69
196 67 s/( [\"$ldquo$lsquo] \.(?:\xA0\.)+ ) \s /$1\xA0/xog;
197 67 s/\s (?= \. (?:\xA0[.,!?])+ [$rdquo$rsquo\xA0\"]* $)/\xA0/xo;
198 }
199  
200 # Return the text to where it came from:
201 # This only works because the replacement text is always
202 # the same length as the original.
203 69 foreach my $r (@$refs) {
204 70 $$r = substr($_, 0, length($$r), '');
205 70 ▶ if ($fixQuotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixQuotes) 66 4 70
206 # Since the replacement text isn't the same length,
207 # these can't be done on the string as a whole:
208 66 $$r =~ s/(?<=[$ldquo$rdquo])(?=[$lsquo$rsquo])/\xA0/go;
209 66 $$r =~ s/(?<=[$lsquo$rsquo])(?=[$ldquo$rdquo])/\xA0/go;
210 66 $$r =~ s/(?<=[$ldquo$lsquo])\xA0(?=\.\xA0\.)//go;
211 } # end if fixQuotes
212 } # end foreach @$refs
213 } # end processTextRefs
214  
215 #---------------------------------------------------------------------
216 # Recursively process an HTML::Element tree:
217  
218 sub process
219 {
220 74 ▶ my ($self, $elt) = @_;
Subroutine process: called CC=29 SCAR=35.5
Pod documented
221  
222 74 ▶ croak "HTML::Embellish->process must be passed an HTML::Element"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elt and $elt->can("content_refs_list") 1 73 74
Condition66%2/3
ref $elt and $elt->can("content_refs_list")
!ll&&!rl&&r
1073
MC/DC50%1/2
ref $elt and $elt->can("content_refs_list")
ref $elt $elt->can("content_refs_list")
Truth table66%2/3
A and B
A= ref $eltB= $elt->can("content_refs_list")
ABresult
0X0
100
111
223 unless ref $elt and $elt->can('content_refs_list');
224  
225 73 ▶ return if $elt->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $elt->is_empty 0 73 73
226  
227 73 my $parentRefs;
228 73 my $isP = ($elt->tag =~ /^(?: p | h\d | d[dt] | div | blockquote | title )$/x);
229  
230 73 ▶ if ($isP and ($self->[fixQuotes] or $self->[fixEllipses])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isP and $self->[1] || $self->[3]) 69 4 73
Condition100%6/6
$isP and $self->[1] || $self->[3]
!ll&&!rl&&r
15913
$self->[1] or $self->[3]
l!l&&r!l&&!r
6543
MC/DC66%2/3
$isP and $self->[1] || $self->[3]
$isP $self->[1] $self->[3]
Truth table75%3/4
A and B or C
A= $isPB= $self->[1]C= $self->[3]
ABCresult
0XX0
1000
11X1
1011
231 69 $parentRefs = $self->[textRefs];
232 69 $self->[textRefs] = []
233 } # end if need to collect text refs
234  
235 73 $elt->normalize_content;
236 73 my @content = $elt->content_refs_list;
237  
238 73 ▶ if ($self->[fixQuotes] and $self->[textRefs] and @content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1] and $self->[0] and @content) 66 7 73
Condition66%4/6
$self->[1] and $self->[0] and @content
!ll&&!rl&&r
7066
$self->[1] and $self->[0]
!ll&&!rl&&r
7066
MC/DC33%1/3
$self->[1] and $self->[0] and @content
$self->[1] $self->[0] @content
Truth table50%2/4
A and B and C
A= $self->[1]B= $self->[0]C= @content
ABCresult
0XX0
10X0
1100
1111
239 # A " that opens a tag can be assumed to be a left quote
240 66 ▶ ${$content[ 0]} =~ s/^"/$ldquo/ unless ref ${$content[ 0]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$content[0];} 65 1 66
241 # A " that ends a tag can be assumed to be a right quote
242 66 ▶ ${$content[-1]} =~ s/"$/$rdquo/ unless ref ${$content[-1]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$content[-1];} 65 1 66
243 }
244  
245 73 foreach my $r (@content) {
246 74 ▶ if (ref $$r) { # element node
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$r) { } 2 72 74
247 2 my $tag = $$r->tag;
248 2 ▶ next if $tag =~ /^(?: ~comment | script | style )$/x;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^(?: ~comment | script | style )$/x 0 2 2
249  
250 2 ▶ if ($self->[textRefs] and $tag eq 'br') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] and $tag eq "br") 0 2 2
Condition33%1/3
$self->[0] and $tag eq "br"
!ll&&!rl&&r
020
MC/DC0%0/2
$self->[0] and $tag eq "br"
$self->[0] $tag eq "br"
Truth table33%1/3
A and B
A= $self->[0]B= $tag eq "br"
ABresult
0X0
100
111
251 0 my $break = "\n";
252 0 push @{$self->[textRefs]}, \$break;
253 }
254 2 $self->process($$r);
255 } else { # text node
256 # Convert -- to em-dash:
257 72 utf8::upgrade($$r);
258 72 ▶ if ($self->[fixDashes]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 64 8 72
259 64 $$r =~ s/(?<!-)---?(?!-)/$mdash/g; # &mdash;
260 64 $$r =~ s/(?<!-)----(?!-)/$mdash$mdash/g;
261 } # end if fixDashes
262  
263 72 ▶ $$r =~ s/$hellip/.../go if $self->[fixHellip];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 67 5 72
264  
265 # Fix ellipses:
266 72 ▶ if ($self->[fixEllipses]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 67 5 72
267 67 $$r =~ s/(?<!\.)\.\.\.([.?!;:,])(?!\.)/.\xA0.\xA0.\xA0$1/g;
268 67 $$r =~ s/(?<!\.)\.\.\.(?!\.)/.\xA0.\xA0./g;
269 67 $$r =~ s/(?<= \.) [^\PZ\x{200B}] (?=[.,?!])/\xA0/gx;
270 67 $$r =~ s/(?:(?<=\w)|\A) (\.\xA0\.\xA0\.|\.\.\.)(?=[ \xA0\n\"\'?!$rsquo$rdquo])(?![ \xA0\n]+\w)/\xA0$1/go;
271 } # end if fixEllipses
272  
273 72 ▶ if ($self->[fixEllipseSpace]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4]) 67 5 72
274 67 $$r =~ s/(?<=\w) (\.(?:\xA0\.)+) (?=\w)/ $1 /gx;
275 67 $$r =~ s/(?<=\w[!?,;]) (\.(?:\xA0\.)+) (?=\w)/ $1 /gx;
276 67 $$r =~ s/( [\"$ldquo$lsquo] \.(?:\xA0\.)+ ) (?=\w) /$1\xA0/xog;
277 67 $$r =~ s/(?<=\w) (\.\xA0\.\xA0\.) (?![\xA0\w])/\xA0$1/gx;
278  
279 67 ▶ if ($self->[textRefs] and @{$self->[textRefs]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] and @{$self->[0];}) 0 67 67
Condition33%1/3
$self->[0] and @{$self->[0];}
!ll&&!rl&&r
0670
MC/DC0%0/2
$self->[0] and @{$self->[0];}
$self->[0] @{$self->[0];}
Truth table33%1/3
A and B
A= $self->[0]B= @{$self->[0];}
ABresult
0X0
100
111
280 $$r =~ s/^(\.(?:\xA0\.)+) (?=\w)/ $1 /gx
281 0 ▶ if ${$self->[textRefs][-1]} =~ /\w[!?,;]?$/;
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->[0][-1];} =~ /\w[!?,;]?$/ 0 0 0
282  
283 0 ▶ ${$self->[textRefs][-1]} =~ s/(?<=\w)\xA0(\.\xA0\.\xA0\.)$/ $1 /
Branch: 1 branch
BranchTrueFalseTotal
if $$r =~ /^\w/ 0 0 0
284 if $$r =~ /^\w/;
285 }
286 } # end if fixEllipseSpace
287  
288 72 ▶ push @{$self->[textRefs]}, $r if $self->[textRefs];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[0] 69 3 72
289 } # end else text node
290 } # end foreach $r
291  
292 73 ▶ if ($isP and $self->[textRefs]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isP and $self->[0]) 69 4 73
Condition100%3/3
$isP and $self->[0]
!ll&&!rl&&r
1369
MC/DC100%2/2
$isP and $self->[0]
$isP $self->[0]
Truth table100%3/3
A and B
A= $isPB= $self->[0]
ABresult
0X0
100
111
293 ### print LOG (map { utf8::is_utf8($$_) . "{$$_}" } @{ $self->[textRefs] }), "\n";
294 69 $self->processTextRefs($self->[textRefs]);
295 69 ▶ push @$parentRefs, @{$self->[textRefs]} if $parentRefs;
Branch: 1 branch
BranchTrueFalseTotal
if $parentRefs 1 68 69
296 69 $self->[textRefs] = $parentRefs;
297 } # end if this was a paragraph-like element
298 } # end process
299  
300 #=====================================================================
301 # Package Return Value:
302  
303 4 1;
304  
305 __END__
↑ Summary