Turndown/Rules.pm

filter: Statementstmt 86.3% 57 / 66 Branchbran 70.0% 14 / 20 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 10 totaltotal 75.6% 93 / 123 CC 15 SCAR 10.9
CC
15
Cov
82%
forNode
9.3
filterValue
4.0
__ANON__
3.0
CRAP 3.0
« Turndown.pm ↑ Summary Turndown/HighlightedCodeBlock.pm »
1 package Text::HTML::Turndown::Rules 0.12;
2 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 has 'options' => (
9 is => 'ro',
10 required => 1,
11 );
12  
13 6 has 'rules' => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 has '_preprocess' => (
19 is => 'lazy',
20 167 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 6 );
22  
23 has '_keep' => (
24 is => 'lazy',
25 22 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 6 );
27  
28 has '_remove' => (
29 is => 'lazy',
30 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 6 );
32  
33 has 'array' => (
34 is => 'lazy',
35 167 ▶ default => sub($self) {
Subroutine __ANON__: called CC=3 SCAR=11.1
36 167 my @arr;
37 167 ▶ my $r = $self->rules // {};
Condition50%1/2
$self->rules // {}
l!l
1670
MC/DC0%0/1
$self->rules // {}
$self->rules
Truth table50%1/2
A
A= $self->rules
Aresult
11
00
38 167 for my $rule ( sort keys $r->%*) {
39 2505 push @arr, $r->{$rule};
40 }
41 167 return \@arr;
42 },
43 6 );
44  
45 has 'defaultReplacement' => (
46 is => 'lazy',
47 165 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 371 ▶ sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
49 371 ▶ $node->isBlock ? "\n\n" . $content . "\n\n" : $content
Branch: 1 branch
BranchTrueFalseTotal
$node->isBlock ? : 344 27 371
50 }
51 165 }
52 6 );
53  
54 has 'blankReplacement' => (
55 is => 'lazy',
56 7 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 9 ▶ sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
58 9 ▶ $node->isBlock ? "\n\n" : ''
Branch: 1 branch
BranchTrueFalseTotal
$node->isBlock ? : 4 5 9
59 }
60 7 }
61 6 );
62  
63 154 ▶sub add( $self, $key, $rule ) {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
64 154 unshift $self->array->@*, $rule
65 }
66  
67 189 ▶sub preprocess( $self, $processor ) {
Subroutine preprocess: called CC=1 SCAR=0.0
Pod undocumented
68 189 unshift $self->_preprocess->@*, $processor
69 }
70  
71 22 ▶sub keep( $self, $filter ) {
Subroutine keep: called CC=1 SCAR=0.0
Pod undocumented
72 22 unshift $self->_keep->@*, $filter
73 }
74  
75 0 ▶sub remove( $self, $filter ) {
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
76 unshift $self->_remove->@*, {
77 filter => $filter,
78 0 ▶ replacement => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 0 return '';
80 },
81 0 };
82 }
83  
84 9 ▶sub blankRule( $self ) {
Subroutine blankRule: called CC=1 SCAR=0.0
Pod undocumented
85 return {
86 9 replacement => $self->blankReplacement,
87 }
88 }
89  
90 371 ▶sub defaultRule( $self ) {
Subroutine defaultRule: called CC=1 SCAR=0.0
Pod undocumented
91 return {
92 371 replacement => $self->defaultReplacement
93 }
94 }
95  
96 757 ▶sub forNode( $self, $node ) {
Subroutine forNode: called CC=5 SCAR=22.3
Pod undocumented
97 757 ▶ if ($node->isBlank) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isBlank) 9 748 757
98 9 return $self->blankRule };
99 748 my $rule;
100 748 ▶ if ($rule = $self->findRule($self->array, $node, $self->options)) { return $rule; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rule = $self->findRule($self->array, $node, $self->options)) 748 0 748
101 0 ▶ if ($rule = $self->findRule($self->_keep, $node, $self->options)) { return $rule; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rule = $self->findRule($self->_keep, $node, $self->options)) 0 0 0
102 0 ▶ if ($rule = $self->findRule($self->_remove, $node, $self->options)) { return $rule; }
Branch: 1 branch
BranchTrueFalseTotal
if ($rule = $self->findRule($self->_remove, $node, $self->options)) 0 0 0
103  
104 0 return $self->defaultRule
105 };
106  
107 167 ▶sub forEach($self, $fn) {
Subroutine forEach: called CC=2 SCAR=6.9
Pod undocumented
108 167 my $arr = $self->array;
109 $fn->( $arr->[ $_ ] )
110 167 for (0..$#{$arr});
111 }
112  
113 748 ▶sub findRule( $self, $rules, $node, $options ) {
Subroutine findRule: called CC=3 SCAR=11.0
Pod undocumented
114 748 for my $r ($rules->@*) {
115 8976 ▶ return $r
Branch: 1 branch
BranchTrueFalseTotal
if $self->filterValue($r, $node, $options) 377 8599 8976
116 if( $self->filterValue( $r, $node, $options ))
117 }
118 371 return $self->defaultRule
119 }
120  
121 8976 ▶sub filterValue( $self, $rule, $node, $options ) {
Subroutine filterValue: called CC=4 SCAR=14.0
Pod undocumented
122 8976 my $filter = $rule->{filter};
123 8976 ▶ if( ! ref $filter ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $filter) { } 3169 5807 8976
elsif (ref $filter eq 'ARRAY') { } 2795 3012 5807
elsif (ref $filter eq 'CODE') { } 3012 0 3012
124 3169 return $filter eq lc($node->nodeName)
125 } elsif( ref $filter eq 'ARRAY' ) {
126 2795 my $nn = lc $node->nodeName;
127 2795 return scalar grep { $nn eq $_ } $filter->@*
128 } elsif( ref $filter eq 'CODE' ) {
129 3012 return $filter->( $rule, $node, $options )
130 } else {
131 0 croak "Unknown filter type '$filter'";
132 }
133 }
134  
135 6 1;
136 =head1 REPOSITORY
137  
138 The public repository of this module is
139 L<https://github.com/Corion/Text-HTML-Turndown>.
140  
141 =head1 SUPPORT
142  
143 The public support forum of this module is L<https://perlmonks.org/>.
144  
145 =head1 BUG TRACKER
146  
147 Please report bugs in this module via the Github bug queue at
148 L<https://github.com/Corion/Text-HTML-Turndown/issues>
149  
150 =head1 AUTHOR
151  
152 Max Maischein C<corion@cpan.org>
153  
154 =head1 COPYRIGHT (c)
155  
156 Copyright 2025- by Max Maischein C<corion@cpan.org>.
157  
158 =head1 LICENSE
159  
160 This module is released under the Artistic License 2.0.
161  
162 =cut
« Turndown.pm ↑ Summary Turndown/HighlightedCodeBlock.pm »