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 CC 46 SCAR 30.4
CC
46
Cov
89%
process
34.7
new
8.0
processTextRefs
8.0
CRAP 21.0
↑ 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.000';
33 # This file is part of HTML-Embellish 1.000 (February 1, 2014)
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 64 ▶ my $html = shift @_;
Subroutine embellish: called CC=3 SCAR=11.0
Pod documented
70  
71 64 ▶ croak "First parameter of embellish must be an HTML::Element"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $html and $html->can("content_refs_list") 1 63 64
Condition66%2/3
ref $html and $html->can("content_refs_list")
!ll&&!rl&&r
1063
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 63 my $e = HTML::Embellish->new(@_);
75 62 $e->process($html);
76 } # end embellish
77  
78 #=====================================================================
79 # Class Methods:
80 #---------------------------------------------------------------------
81 sub new
82 {
83 64 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
84 64 ▶ croak "Odd number of parameters passed to HTML::Embellish->new" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 63 64
85 63 my %parms = @_;
86  
87 63 my $self = [ (undef) x totalFields ];
88 63 bless $self, $class;
89  
90 63 ▶ my $def = (exists $parms{default} ? $parms{default} : 1);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'default'} ? : 9 54 63
91  
92 63 $self->[textRefs] = undef;
93 63 ▶ $self->[fixDashes] = (exists $parms{dashes} ? $parms{dashes} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'dashes'} ? : 1 62 63
94 63 ▶ $self->[fixEllipses] = (exists $parms{ellipses} ? $parms{ellipses} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'ellipses'} ? : 4 59 63
95 63 ▶ $self->[fixQuotes] = (exists $parms{quotes} ? $parms{quotes} : $def);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'quotes'} ? : 2 61 63
96  
97 $self->[fixHellip] = (exists $parms{hellip}
98 63 ▶ ? $parms{hellip} : $self->[fixEllipses]);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'hellip'} ? : 0 63 63
99 $self->[fixEllipseSpace] = (exists $parms{space_ellipses}
100 63 ▶ ? $parms{space_ellipses} : $self->[fixEllipses]);
Branch: 1 branch
BranchTrueFalseTotal
exists $parms{'space_ellipses'} ? : 0 63 63
101  
102 63 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 60 ▶ my ($self, $refs) = @_;
Subroutine processTextRefs: called CC=8 SCAR=20.8
Pod undocumented
115  
116 60 local $_ = join('', map { $$_ } @$refs);
117 60 utf8::upgrade($_);
118  
119 60 my $fixQuotes = $self->[fixQuotes];
120 60 ▶ if ($fixQuotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixQuotes) 56 4 60
121 56 s/\("/($ldquo/g;
122 56 s/"\)/$rdquo)/g;
123  
124 56 s/^([\xA0\s]*)"/$1$ldquo/;
125 56 s/(?<=[\s\pZ])"(?=[^\s\pZ])/$ldquo/g;
126 56 s/(?<=\pP)"(?=\w)/$ldquo/g;
127 56 s/(?<=[ \t\n\r])"(?=\xA0)/$ldquo/g;
128  
129 56 s/"[\xA0\s]*$/$rdquo/;
130 56 s/(?<![\s\pZ])"(?=[\s\pZ])/$rdquo/g;
131 56 s/(?<=\w)"(?=\pP)/$rdquo/g;
132 56 s/(?<=\xA0)"(?=[ \t\n\r]|[\s\xA0]+$)/$rdquo/g;
133 56 s/(?<=[,;.!?])"(?=[-$mdash])/$rdquo/go;
134  
135 56 s/'(?=(?:cause|cept|d|e[mr]?e?|fraidy?|gainst|im|m|n|nam|nothers?|nuff|re?|rithmetic|s|scuse|spects?|t|til|tisn?|tw(?:asn?|ere?n?|ould\w*)|ud|uns?)\b|\d\d\W?s|\d\d(?!\w))/$rsquo/ig;
136  
137 56 s/'([ \xA0]?$rdquo)/$rsquo$1/go;
138  
139 56 s/`/$lsquo/g;
140 56 s/^'/$lsquo/;
141 56 s/(?<=[\s\pZ])'(?=[^\s\pZ])/$lsquo/g;
142 56 s/(?<=\pP)(?<![.!?])'(?=\w)/$lsquo/g;
143 56 s/(?<=[ \t\n\r])'(?=\xA0)/$lsquo/g;
144  
145 56 s/'/$rsquo/g;
146  
147 56 s/(?<!\PZ)"([\xA0\s]+$lsquo)/$ldquo$1/go;
148 56 s/(${rsquo}[\xA0\s]+)"(?!\PZ)/$1$rdquo/go;
149  
150 56 ▶ if (/"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/"/) 1 55 56
151 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
152 /$1$ldquo/xo
153 or s/^($balancedQuoteString $ldquo $notQuote*) "/$1$rdquo/xo;
154 } # end if straight quotes remaining in string
155  
156 #s/(?<=\p{IsPunct})"(?=\p{IsAlpha})/$ldquo/go;
157 56 s/(?<=[[:punct:]])"(?=[[:alpha:]])/$ldquo/go;
158  
159 56 s/${ldquo}\s([$lsquo$rsquo])/$ldquo\xA0$1/go;
160 56 s/${rsquo}\s$rdquo/$rsquo\xA0$rdquo/go;
161 } # end if fixQuotes
162  
163 60 ▶ if ($self->[fixEllipses]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 58 2 60
164 58 s/( [\"$ldquo$lsquo] \.(?:\xA0\.)+ ) \s /$1\xA0/xog;
165 58 s/\s (?= \. (?:\xA0[.,!?])+ [$rdquo$rsquo\xA0\"]* $)/\xA0/xo;
166 }
167  
168 # Return the text to where it came from:
169 # This only works because the replacement text is always
170 # the same length as the original.
171 60 foreach my $r (@$refs) {
172 61 $$r = substr($_, 0, length($$r), '');
173 61 ▶ if ($fixQuotes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fixQuotes) 57 4 61
174 # Since the replacement text isn't the same length,
175 # these can't be done on the string as a whole:
176 57 $$r =~ s/(?<=[$ldquo$rdquo])(?=[$lsquo$rsquo])/\xA0/go;
177 57 $$r =~ s/(?<=[$lsquo$rsquo])(?=[$ldquo$rdquo])/\xA0/go;
178 57 $$r =~ s/(?<=[$ldquo$lsquo])\xA0(?=\.\xA0\.)//go;
179 } # end if fixQuotes
180 } # end foreach @$refs
181 } # end processTextRefs
182  
183 #---------------------------------------------------------------------
184 # Recursively process an HTML::Element tree:
185  
186 sub process
187 {
188 65 ▶ my ($self, $elt) = @_;
Subroutine process: called CC=29 SCAR=35.5
Pod documented
189  
190 65 ▶ croak "HTML::Embellish->process must be passed an HTML::Element"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $elt and $elt->can("content_refs_list") 1 64 65
Condition66%2/3
ref $elt and $elt->can("content_refs_list")
!ll&&!rl&&r
1064
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
191 unless ref $elt and $elt->can('content_refs_list');
192  
193 64 ▶ return if $elt->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $elt->is_empty 0 64 64
194  
195 64 my $parentRefs;
196 64 my $isP = ($elt->tag =~ /^(?: p | h\d | d[dt] | div | blockquote | title )$/x);
197  
198 64 ▶ if ($isP and ($self->[fixQuotes] or $self->[fixEllipses])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isP and $self->[1] || $self->[3]) 60 4 64
Condition100%6/6
$isP and $self->[1] || $self->[3]
!ll&&!rl&&r
15013
$self->[1] or $self->[3]
l!l&&r!l&&!r
5643
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
199 60 $parentRefs = $self->[textRefs];
200 60 $self->[textRefs] = []
201 } # end if need to collect text refs
202  
203 64 $elt->normalize_content;
204 64 my @content = $elt->content_refs_list;
205  
206 64 ▶ if ($self->[fixQuotes] and $self->[textRefs] and @content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1] and $self->[0] and @content) 57 7 64
Condition66%4/6
$self->[1] and $self->[0] and @content
!ll&&!rl&&r
7057
$self->[1] and $self->[0]
!ll&&!rl&&r
7057
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
207 # A " that opens a tag can be assumed to be a left quote
208 57 ▶ ${$content[ 0]} =~ s/^"/$ldquo/ unless ref ${$content[ 0]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$content[0];} 56 1 57
209 # A " that ends a tag can be assumed to be a right quote
210 57 ▶ ${$content[-1]} =~ s/"$/$rdquo/ unless ref ${$content[-1]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$content[-1];} 56 1 57
211 }
212  
213 64 foreach my $r (@content) {
214 65 ▶ if (ref $$r) { # element node
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$r) { } 2 63 65
215 2 my $tag = $$r->tag;
216 2 ▶ next if $tag =~ /^(?: ~comment | script | style )$/x;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^(?: ~comment | script | style )$/x 0 2 2
217  
218 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
219 0 my $break = "\n";
220 0 push @{$self->[textRefs]}, \$break;
221 }
222 2 $self->process($$r);
223 } else { # text node
224 # Convert -- to em-dash:
225 63 utf8::upgrade($$r);
226 63 ▶ if ($self->[fixDashes]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 55 8 63
227 55 $$r =~ s/(?<!-)---?(?!-)/$mdash/g; # &mdash;
228 55 $$r =~ s/(?<!-)----(?!-)/$mdash$mdash/g;
229 } # end if fixDashes
230  
231 63 ▶ $$r =~ s/$hellip/.../go if $self->[fixHellip];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 58 5 63
232  
233 # Fix ellipses:
234 63 ▶ if ($self->[fixEllipses]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3]) 58 5 63
235 58 $$r =~ s/(?<!\.)\.\.\.([.?!;:,])(?!\.)/.\xA0.\xA0.\xA0$1/g;
236 58 $$r =~ s/(?<!\.)\.\.\.(?!\.)/.\xA0.\xA0./g;
237 58 $$r =~ s/(?<= \.) [^\PZ\x{200B}] (?=[.,?!])/\xA0/gx;
238 58 $$r =~ s/(?:(?<=\w)|\A) (\.\xA0\.\xA0\.|\.\.\.)(?=[ \xA0\n\"\'?!$rsquo$rdquo])(?![ \xA0\n]+\w)/\xA0$1/go;
239 } # end if fixEllipses
240  
241 63 ▶ if ($self->[fixEllipseSpace]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4]) 58 5 63
242 58 $$r =~ s/(?<=\w) (\.(?:\xA0\.)+) (?=\w)/ $1 /gx;
243 58 $$r =~ s/(?<=\w[!?,;]) (\.(?:\xA0\.)+) (?=\w)/ $1 /gx;
244 58 $$r =~ s/( [\"$ldquo$lsquo] \.(?:\xA0\.)+ ) (?=\w) /$1\xA0/xog;
245 58 $$r =~ s/(?<=\w) (\.\xA0\.\xA0\.) (?![\xA0\w])/\xA0$1/gx;
246  
247 58 ▶ if ($self->[textRefs] and @{$self->[textRefs]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] and @{$self->[0];}) 0 58 58
Condition33%1/3
$self->[0] and @{$self->[0];}
!ll&&!rl&&r
0580
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
248 $$r =~ s/^(\.(?:\xA0\.)+) (?=\w)/ $1 /gx
249 0 ▶ if ${$self->[textRefs][-1]} =~ /\w[!?,;]?$/;
Branch: 1 branch
BranchTrueFalseTotal
if ${$self->[0][-1];} =~ /\w[!?,;]?$/ 0 0 0
250  
251 0 ▶ ${$self->[textRefs][-1]} =~ s/(?<=\w)\xA0(\.\xA0\.\xA0\.)$/ $1 /
Branch: 1 branch
BranchTrueFalseTotal
if $$r =~ /^\w/ 0 0 0
252 if $$r =~ /^\w/;
253 }
254 } # end if fixEllipseSpace
255  
256 63 ▶ push @{$self->[textRefs]}, $r if $self->[textRefs];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[0] 60 3 63
257 } # end else text node
258 } # end foreach $r
259  
260 64 ▶ if ($isP and $self->[textRefs]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isP and $self->[0]) 60 4 64
Condition100%3/3
$isP and $self->[0]
!ll&&!rl&&r
1360
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
261 ### print LOG (map { utf8::is_utf8($$_) . "{$$_}" } @{ $self->[textRefs] }), "\n";
262 60 $self->processTextRefs($self->[textRefs]);
263 60 ▶ push @$parentRefs, @{$self->[textRefs]} if $parentRefs;
Branch: 1 branch
BranchTrueFalseTotal
if $parentRefs 1 59 60
264 60 $self->[textRefs] = $parentRefs;
265 } # end if this was a paragraph-like element
266 } # end process
267  
268 #=====================================================================
269 # Package Return Value:
270  
271 4 1;
272  
273 __END__
↑ Summary