Turndown.pm

filter: Statementstmt 95.1% 234 / 246 Branchbran 79.0% 68 / 86 Conditioncond 81.2% 65 / 80 MC/DCmcdc 65.9% 29 / 44 Subroutinesub 93.5% 58 / 62 Podpod 7.1% 1 / 14 totaltotal 85.5% 455 / 532 CC 76 SCAR 13.0
CC
76
Cov
89%
__ANON__
9.4
__ANON__
6.2
__ANON__
6.0
CRAP 3.7
« ExtractInfo.pm ↑ Summary Turndown/Rules.pm »
1 package Text::HTML::Turndown 0.12;
2 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moo 2;
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 Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use List::Util 'reduce', 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::MoreUtils 'first_index';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Module::Load 'load';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Text::HTML::Turndown::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Text::HTML::Turndown::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Text::HTML::CollapseWhitespace 'collapseWhitespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Text::HTML::ExtractInfo 'extract_info';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Text::FrontMatter::YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 our @EXPORT_OK = ('html2markdown');
20  
21 =head1 NAME
22  
23 Text::HTML::Turndown - convert HTML to Markdown
24  
25 =head1 SYNOPSIS
26  
27 use Text::HTML::Turndown;
28 my $convert = Text::HTML::Turndown->new();
29 my $markdown = $convert->turndown(<<'HTML');
30 <h1>Hello world!</h1>
31 HTML
32 # Hello world!
33 # ------------
34  
35 This is an adapation of the C<turndown> libraries.
36  
37 =cut
38  
39 our %COMMONMARK_RULES = (
40 paragraph => {
41 filter => 'p',
42 30 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 30 return "\n\n" . $content . "\n\n"
44 },
45 },
46  
47 lineBreak => {
48 filter => 'br',
49  
50 10 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 10 return $options->{br} . "\n"
52 }
53 },
54  
55 heading => {
56 filter => ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
57  
58 9 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=6 SCAR=18.2
59 9 ▶ if( $node->nodeName !~ /\AH(\d)\z/i ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->nodeName =~ /\AH(\d)\z/iu) 0 9 9
60 0 croak sprintf "Unknown node name '%s' for heading", $node->nodeName;
61 }
62 9 my $hLevel = $1;
63  
64 9 ▶ if (($options->{headingStyle} // '') eq 'setext' && $hLevel < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (($options->{'headingStyle'} // '') eq 'setext' and $hLevel < 3) { } 4 5 9
Condition80%4/5
($options->{"headingStyle"} // "") eq "setext" and $hLevel < 3
!ll&&!rl&&r
234
$options->{"headingStyle"} // ""
l!l
90
MC/DC50%1/2
($options->{"headingStyle"} // "") eq "setext" and $hLevel < 3
$options->{"headingStyle"} $hLevel < 3
Truth table66%2/3
A and B
A= $options->{"headingStyle"}B= $hLevel < 3
ABresult
0X0
100
111
65 4 ▶ my $underline = ($hLevel == 1 ? '=' : '-') x length($content);
Branch: 1 branch
BranchTrueFalseTotal
$hLevel == 1 ? : 2 2 4
66 return (
67 4 "\n\n" . $content . "\n" . $underline . "\n\n"
68 )
69 } else {
70 5 return "\n\n" . ('#'x $hLevel) . ' ' . $content . "\n\n"
71 }
72 }
73 },
74  
75 blockquote => {
76 filter => 'blockquote',
77  
78 5 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
79 5 $content =~ s/^\n+|\n+$//g;
80 5 $content =~ s/^/> /gm;
81 5 return "\n\n" . $content . "\n\n"
82 }
83 },
84  
85  
86 list => {
87 filter => ['ul', 'ol'],
88  
89 24 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
90 24 my $parent = $node->parentNode;
91 24 ▶ if (uc $parent->nodeName eq 'LI' && $parent->lastChild->isEqual($node->_node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $parent->nodeName eq 'LI' and $parent->lastChild->isEqual($node->_node)) { } 5 19 24
Condition66%2/3
uc $parent->nodeName eq "LI" and $parent->lastChild->isEqual($node->_node)
!ll&&!rl&&r
1905
MC/DC50%1/2
uc $parent->nodeName eq "LI" and $parent->lastChild->isEqual($node->_node)
uc $parent->nodeName eq "LI" $parent->lastChild->isEqual($node->_node)
Truth table66%2/3
A and B
A= uc $parent->nodeName eq "LI"B= $parent->lastChild->isEqual($node->_node)
ABresult
0X0
100
111
92 5 return "\n" . $content
93 } else {
94 19 return "\n\n" . $content . "\n\n"
95 }
96 }
97 },
98  
99 listItem => {
100 filter => 'li',
101  
102 60 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=5 SCAR=16.1
103 60 my $prefix = $options->{bulletListMarker} . ' ';
104 60 my $parent = $node->parentNode;
105 60 ▶ if (uc $parent->nodeName eq 'OL') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $parent->nodeName eq "OL") 23 37 60
106 23 my $start = $parent->getAttribute('start');
107 23 my @ch = grep { $_->nodeType == 1 } $parent->childNodes;
108 #my @ch = $parent->childNodes;
109 23 ▶ my $index = first_index { $_->isEqual($node->_node) } @ch;
Subroutine __ANON__: called CC=1 SCAR=0.0
110 23 ▶ $prefix = ($start ? $start + $index : $index + 1) . '. '
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 4 19 23
111 }
112 60 $content =~ s/^\n+//; # remove leading newlines
113 60 $content =~ s/\n+$/\n/; # replace trailing newlines with just a single one
114 60 $content =~ s/\n/"\n" . (' ' x length($prefix))/gem; # indent
115 return (
116 60 ▶ $prefix . $content . ($node->nextSibling && $content !~ /\n$/ ? "\n" : '')
Branch: 1 branch
BranchTrueFalseTotal
$node->nextSibling && !($content =~ /\n$/u) ? : 36 24 60
Condition66%2/3
$node->nextSibling and not $content =~ /\n$/u
!ll&&!rl&&r
24036
MC/DC50%1/2
$node->nextSibling and not $content =~ /\n$/u
$node->nextSibling not $content =~ /\n$/u
Truth table66%2/3
A and not(B)
A= $node->nextSiblingB= not $content =~ /\n$/u
ABresult
0X0
100
111
117 )
118 }
119 },
120  
121 indentedCodeBlock => {
122 529 ▶ filter => sub ($rule, $node, $options) {
Subroutine __ANON__: called CC=4 SCAR=13.9
123 return (
124 529 ▶ $options->{codeBlockStyle} eq 'indented' &&
Condition88%8/9
$options->{'codeBlockStyle'} eq 'indented' && uc $node->nodeName eq 'PRE' && $node->firstNonBlankChild && uc $node->firstNonBlankChild->nodeName eq 'CODE'
!ll&&!rl&&r
51838
$options->{"codeBlockStyle"} eq "indented" and uc $node->nodeName eq "PRE" and $node->firstNonBlankChild
!ll&&!rl&&r
518011
$options->{"codeBlockStyle"} eq "indented" and uc $node->nodeName eq "PRE"
!ll&&!rl&&r
2749111
MC/DC75%3/4
$options->{'codeBlockStyle'} eq 'indented' && uc $node->nodeName eq 'PRE' && $node->firstNonBlankChild && uc $node->firstNonBlankChild->nodeName eq 'CODE'
$options->{"codeBlockStyle"} eq "indented" uc $node->nodeName eq "PRE" $node->firstNonBlankChild uc $node->firstNonBlankChild->nodeName eq 'CODE'
Truth table80%4/5
A and B and C && D
A= $options->{"codeBlockStyle"} eq "indented"B= uc $node->nodeName eq "PRE"C= $node->firstNonBlankChildD= uc $node->firstNonBlankChild->nodeName eq 'CODE'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
125 uc $node->nodeName eq 'PRE' &&
126 $node->firstNonBlankChild &&
127 uc $node->firstNonBlankChild->nodeName eq 'CODE'
128 )
129 },
130 8 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=4 SCAR=13.9
131 8 my @textChildren = $node->firstNonBlankChild->nonBlankChildNodes;
132 # Replace any <br> with a newline:
133 @textChildren = map {
134 8 ▶ if( $_->nodeType == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->nodeType == 3) { } 11 3 14
elsif ($_->nodeType == 1 and uc $_->nodeName eq 'BR') { } 2 1 3
135 11 $_->textContent =~ s/\n/\n /gr
136 ▶ } elsif( $_->nodeType == 1 and uc $_->nodeName eq 'BR' ) {
Condition66%2/3
$_->nodeType == 1 and uc $_->nodeName eq "BR"
!ll&&!rl&&r
102
MC/DC50%1/2
$_->nodeType == 1 and uc $_->nodeName eq "BR"
$_->nodeType == 1 uc $_->nodeName eq "BR"
Truth table66%2/3
A and B
A= $_->nodeType == 1B= uc $_->nodeName eq "BR"
ABresult
0X0
100
111
137 2 "\n "
138 } else {
139 # could be an HTML comment
140 1 ''
141 }
142 } @textChildren;
143 8 my $res = (
144 join( '',
145 "\n\n ",
146 @textChildren,
147 "\n\n",
148 )
149 );
150 8 return $res;
151 },
152 },
153  
154 fencedCodeBlock => {
155 559 ▶ filter => sub($rule, $node, $options) {
Subroutine __ANON__: called CC=4 SCAR=14.2
156 return (
157 559 ▶ $options->{codeBlockStyle} eq 'fenced' &&
Condition77%7/9
$options->{'codeBlockStyle'} eq 'fenced' && uc $node->nodeName eq 'PRE' && $node->firstNonBlankChild && uc $node->firstNonBlankChild->nodeName eq 'CODE'
!ll&&!rl&&r
55108
$options->{"codeBlockStyle"} eq "fenced" and uc $node->nodeName eq "PRE" and $node->firstNonBlankChild
!ll&&!rl&&r
55108
$options->{"codeBlockStyle"} eq "fenced" and uc $node->nodeName eq "PRE"
!ll&&!rl&&r
524278
MC/DC50%2/4
$options->{'codeBlockStyle'} eq 'fenced' && uc $node->nodeName eq 'PRE' && $node->firstNonBlankChild && uc $node->firstNonBlankChild->nodeName eq 'CODE'
$options->{"codeBlockStyle"} eq "fenced" uc $node->nodeName eq "PRE" $node->firstNonBlankChild uc $node->firstNonBlankChild->nodeName eq 'CODE'
Truth table60%3/5
A and B and C && D
A= $options->{"codeBlockStyle"} eq "fenced"B= uc $node->nodeName eq "PRE"C= $node->firstNonBlankChildD= uc $node->firstNonBlankChild->nodeName eq 'CODE'
ABCDresult
0XXX0
10XX0
110X0
11100
11111
158 uc $node->nodeName eq 'PRE' &&
159 $node->firstNonBlankChild &&
160 uc $node->firstNonBlankChild->nodeName eq 'CODE'
161 )
162 },
163  
164 8 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=9 SCAR=22.5
165 8 ▶ my $className = $node->firstChild->getAttribute('class') || '';
Condition100%2/2
$node->firstChild->getAttribute('class') || ''
l!l
17
MC/DC100%1/1
$node->firstChild->getAttribute('class') || ''
$node->firstChild->getAttribute('class')
Truth table100%2/2
A
A= $node->firstChild->getAttribute('class')
Aresult
11
00
166 8 (my $language) = ($className =~ /language-(\S+)/);
167 8 ▶ $language //= '';
Condition100%2/2
$language //= ""
l!l
17
MC/DC100%1/1
$language //= ""
$language
Truth table100%2/2
A
A= $language
Aresult
11
00
168 8 my @textChildren = $node->firstNonBlankChild->nonBlankChildNodes;
169 # Replace any <br> with a newline:
170 @textChildren = map {
171 8 ▶ if( $_->nodeType == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->nodeType == 3) { } 10 2 12
elsif ($_->nodeType == 1 and uc $_->nodeName eq 'BR') { } 2 0 2
172 10 $_->textContent
173 ▶ } elsif( $_->nodeType == 1 and uc $_->nodeName eq 'BR' ) {
Condition33%1/3
$_->nodeType == 1 and uc $_->nodeName eq "BR"
!ll&&!rl&&r
002
MC/DC0%0/2
$_->nodeType == 1 and uc $_->nodeName eq "BR"
$_->nodeType == 1 uc $_->nodeName eq "BR"
Truth table33%1/3
A and B
A= $_->nodeType == 1B= uc $_->nodeName eq "BR"
ABresult
0X0
100
111
174 2 "\n"
175 } else {
176 # could be an HTML comment
177 0 ''
178 }
179 } @textChildren;
180 8 ▶ if( @textChildren ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@textChildren) 8 0 8
181 8 $textChildren[-1] =~ s/\n\z//;
182 }
183  
184 8 my $fenceChar = substr( $options->{fence}, 0, 1 );
185 8 my $fenceSize = 3;
186 8 my $fenceInCodeRegex = qr{^${fenceChar}{$fenceSize,}};
187 8 for ($content =~ /($fenceInCodeRegex)/gm) {
188 3 ▶ if (length( $_ ) >= $fenceSize) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ >= $fenceSize) 3 0 3
189 3 $fenceSize = length( $_ ) + 1
190 }
191 }
192  
193 8 my $fence = $fenceChar x $fenceSize;
194 return (
195 8 join '',
196 "\n\n" . $fence . $language . "\n",
197 @textChildren,
198 "\n" . $fence . "\n\n"
199 )
200 }
201 },
202 horizontalRule => {
203 filter => 'hr',
204  
205 3 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 3 return "\n\n" . $options->{hr} . "\n\n"
207 }
208 },
209  
210 inlineLink => {
211 521 ▶ filter => sub ($rule, $node, $options) {
Subroutine __ANON__: called CC=3 SCAR=11.0
212 return (
213 521 ▶ $options->{linkStyle} eq 'inlined' &&
Condition100%6/6
$options->{'linkStyle'} eq 'inlined' && uc $node->nodeName eq 'A' && $node->getAttribute('href')
!ll&&!rl&&r
509111
$options->{"linkStyle"} eq "inlined" and uc $node->nodeName eq "A"
!ll&&!rl&&r
950012
MC/DC100%3/3
$options->{'linkStyle'} eq 'inlined' && uc $node->nodeName eq 'A' && $node->getAttribute('href')
$options->{"linkStyle"} eq "inlined" uc $node->nodeName eq "A" $node->getAttribute('href')
Truth table100%4/4
A and B && C
A= $options->{"linkStyle"} eq "inlined"B= uc $node->nodeName eq "A"C= $node->getAttribute('href')
ABCresult
0XX0
10X0
1100
1111
214 uc $node->nodeName eq 'A' &&
215 $node->getAttribute('href')
216 )
217 },
218  
219 11 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=4 SCAR=14.2
220 11 my $href = $node->getAttribute('href');
221 11 ▶ if ($href) { $href =~s/([\(\)])/\\$1/g };
Branch: 1 branch
BranchTrueFalseTotal
if ($href) 11 0 11
222 11 my $title = cleanAttribute($node->getAttribute('title'));
223 11 ▶ if ($title) { $title = ' "' . ( $title =~ s/"/\\"/gr ) . '"'; };
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 3 8 11
224  
225 # Don't emit a link if it has no content
226 # Content might be an image
227 11 ▶ if( length($content)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) { } 11 0 11
228 11 return "[$content]($href$title)"
229 } else {
230 0 return ""
231 }
232 }
233 },
234  
235 referenceLink => {
236 386 ▶ filter => sub ($rule, $node, $options) {
Subroutine __ANON__: called CC=3 SCAR=11.1
237 return (
238 386 ▶ $options->{linkStyle} eq 'referenced' &&
Condition83%5/6
$options->{'linkStyle'} eq 'referenced' && uc $node->nodeName eq 'A' && $node->getAttribute('href')
!ll&&!rl&&r
38303
$options->{"linkStyle"} eq "referenced" and uc $node->nodeName eq "A"
!ll&&!rl&&r
37763
MC/DC66%2/3
$options->{'linkStyle'} eq 'referenced' && uc $node->nodeName eq 'A' && $node->getAttribute('href')
$options->{"linkStyle"} eq "referenced" uc $node->nodeName eq "A" $node->getAttribute('href')
Truth table75%3/4
A and B && C
A= $options->{"linkStyle"} eq "referenced"B= uc $node->nodeName eq "A"C= $node->getAttribute('href')
ABCresult
0XX0
10X0
1100
1111
239 uc $node->nodeName eq 'A' &&
240 $node->getAttribute('href')
241 )
242 },
243  
244 3 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=4 SCAR=13.9
245 3 my $href = $node->getAttribute('href');
246 3 my $title = cleanAttribute($node->getAttribute('title'));
247 0 ▶ if ($title) { $title = ' "$title"' };
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 3 3
248 3 my $replacement;
249 my $reference;
250  
251 3 ▶ if( $options->{linkReferenceStyle} eq 'collapsed' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'linkReferenceStyle'} eq 'collapsed') { } 1 2 3
elsif ($options->{'linkReferenceStyle'} eq 'shortcut') { } 1 1 2
252 1 $replacement = '[' . $content . '][]';
253 1 $reference = '[' . $content . ']: ' . $href .$title;
254  
255 } elsif( $options->{linkReferenceStyle} eq 'shortcut' ) {
256 1 $replacement = '[' . $content . ']';
257 1 $reference = '[' . $content . ']: ' . $href .$title;
258  
259 } else {
260 1 my $id = scalar $context->{references}->@* + 1;
261 1 $replacement = '[' . $content . '][' . $id . ']';
262 1 $reference = '[' . $id . ']: ' . $href . $title;
263 }
264  
265 3 push $context->{references}->@*, $reference;
266 3 return $replacement
267 },
268  
269 167 ▶ append => sub ($options, $context) {
Subroutine __ANON__: called CC=2 SCAR=6.9
270 167 my $references = '';
271 167 ▶ if ($context->{references}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$context->{"references"}}) 3 164 167
272 3 $references = "\n\n" . join( "\n", $context->{references}->@* ) . "\n\n";
273 3 $context->{references} = []; # Reset references
274 }
275 167 return $references
276 }
277 },
278  
279  
280 emphasis => {
281 filter => ['em', 'i'],
282  
283 12 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=2 SCAR=8.1
284 0 ▶ if ($content !~ /\S/) { return '' };
Branch: 1 branch
BranchTrueFalseTotal
unless ($content =~ /\S/u) 0 12 12
285 return $options->{emDelimiter} . $content . $options->{emDelimiter}
286 12 }
287 },
288  
289 strong => {
290 filter => ['strong', 'b'],
291  
292 12 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
293 12 ▶ if ($content !~ /\S/) { return '' };
Branch: 1 branch
BranchTrueFalseTotal
unless ($content =~ /\S/u) 1 11 12
294 return $options->{strongDelimiter} . $content . $options->{strongDelimiter}
295 11 }
296 },
297  
298 code => {
299 585 ▶ filter => sub ($rule, $node, $options) {
Subroutine __ANON__: called CC=4 SCAR=13.9
300 585 ▶ my $hasSiblings = $node->previousSibling || $node->nextSibling;
Condition100%3/3
$node->previousSibling || $node->nextSibling
l!l&&r!l&&!r
8739459
MC/DC100%2/2
$node->previousSibling || $node->nextSibling
$node->previousSibling $node->nextSibling
Truth table100%3/3
A || B
A= $node->previousSiblingB= $node->nextSibling
ABresult
1X1
011
000
301 585 ▶ my $isCodeBlock = (uc $node->parentNode->nodeName eq 'PRE') && !$hasSiblings;
Condition100%3/3
uc $node->parentNode->nodeName eq 'PRE' && !$hasSiblings
!ll&&!rl&&r
566316
MC/DC100%2/2
uc $node->parentNode->nodeName eq 'PRE' && !$hasSiblings
uc $node->parentNode->nodeName eq 'PRE' !$hasSiblings
Truth table100%3/3
A && not(B)
A= uc $node->parentNode->nodeName eq 'PRE'B= !$hasSiblings
ABresult
0X0
100
111
302  
303 585 ▶ return ((uc $node->nodeName eq 'CODE') && !$isCodeBlock)
Condition100%3/3
uc $node->nodeName eq 'CODE' && !$isCodeBlock
!ll&&!rl&&r
5551614
MC/DC100%2/2
uc $node->nodeName eq 'CODE' && !$isCodeBlock
uc $node->nodeName eq 'CODE' !$isCodeBlock
Truth table100%3/3
A && not(B)
A= uc $node->nodeName eq 'CODE'B= !$isCodeBlock
ABresult
0X0
100
111
304 },
305  
306 14 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=4 SCAR=13.9
307 0 ▶ if (!$content) { return '' };
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 0 14 14
308 14 $content =~ s/\r?\n|\r/ /g;
309 14 ▶ my $extraSpace = $content =~ /^`|^ .*?[^ ].* $|`$/ ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /^`|^ .*?[^ ].* $|`$/u ? : 2 12 14
310 14 my $delimiter = '`';
311 14 my @matches = $content =~ /`+/gm;
312 14 ▶ while (grep { $_ eq $delimiter } @matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $delimiter;} @matches) 3 14 17
313 3 $delimiter .= '`';
314 }
315  
316 14 return $delimiter . $extraSpace . $content . $extraSpace . $delimiter;
317 }
318 },
319  
320 image => {
321 filter => 'img',
322  
323 10 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=4 SCAR=13.9
324 10 my $alt = Text::HTML::Turndown::->escape(cleanAttribute($node->getAttribute('alt')));
325 10 ▶ my $src = $node->getAttribute('src') || '';
Condition100%2/2
$node->getAttribute('src') || ''
l!l
91
MC/DC100%1/1
$node->getAttribute('src') || ''
$node->getAttribute('src')
Truth table100%2/2
A
A= $node->getAttribute('src')
Aresult
11
00
326 10 my $title = cleanAttribute($node->getAttribute('title'));
327 10 ▶ my $titlePart = $title ? ' "' . $title . '"' : '';
Branch: 1 branch
BranchTrueFalseTotal
$title ? : 1 9 10
328 10 ▶ return $src ? "![$alt]($src$titlePart)" : "";
Branch: 1 branch
BranchTrueFalseTotal
$src ? : 9 1 10
329 }
330 },
331 6 );
332  
333 6 has 'rules' => (
334 is => 'ro',
335 required => 1,
336 );
337  
338 has 'options' => (
339 is => 'lazy',
340 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
341 6 );
342  
343 has 'html_parser' => (
344 is => 'lazy',
345 166 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
346 166 return XML::LibXML->new();
347 },
348 6 );
349  
350 our %defaults = (
351 rules => \%COMMONMARK_RULES,
352 headingStyle => 'setext',
353 hr => '* * *',
354 bulletListMarker => '*',
355 codeBlockStyle => 'indented',
356 fence => '```',
357 emDelimiter => '_',
358 strongDelimiter => '**',
359 linkStyle => 'inlined',
360 linkReferenceStyle => 'full',
361 br => ' ',
362 preformattedCode => undef,
363 0 ▶ blankReplacement => sub ($content, $node, $options, $context ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
364 0 ▶ return $node->isBlock ? "\n\n" : ""
Branch: 1 branch
BranchTrueFalseTotal
$node->isBlock ? : 0 0 0
365 },
366 0 ▶ keepReplacement => sub ($content, $node, $options, $context) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
367 0 ▶ return $node->isBlock ? "\n\n" . $node->toString . "\n\n" : $node->toString
Branch: 1 branch
BranchTrueFalseTotal
$node->isBlock ? : 0 0 0
368 },
369 0 ▶ defaultReplacement => sub ($content, $node, $options, $context) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
370 0 ▶ return $node->isBlock ? "\n\n" . $content . "\n\n" : $content
Branch: 1 branch
BranchTrueFalseTotal
$node->isBlock ? : 0 0 0
371 }
372 6 );
373  
374 167 ▶around BUILDARGS => sub( $orig, $class, %args ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
375  
376 167 my %options;
377  
378 167 for my $k (sort keys %defaults) {
379 2505 ▶ $options{ $k } = exists $args{ $k } ? delete $args{ $k } : $defaults{ $k };
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$k} ? : 28 2477 2505
380 };
381 167 $args{ options } = \%options;
382 167 $args{ rules } = Text::HTML::Turndown::Rules->new( options => \%options, rules => $options{ rules } );
383  
384 167 ▶ $args{ rules }->preprocess(sub($tree) {
Subroutine __ANON__: called CC=2 SCAR=6.9
385 return collapseWhitespace(
386 element => $tree,
387 isBlock => \&Text::HTML::Turndown::Node::_isBlock,
388 isVoid => \&Text::HTML::Turndown::Node::_isVoid,
389 167 ▶ (isPre => $options{preformattedCode} ? \&isPreOrCode : undef),
Branch: 1 branch
BranchTrueFalseTotal
$options{'preformattedCode'} ? : 5 162 167
390 );
391 167 });
392  
393 167 return $class->$orig(\%args);
394 6 };
395  
396 6 our @escapes = (
397 [qr/\\/, 'q{\\\\\\\\}'],
398 [qr/\*/, 'q{\\\\*}'],
399 [qr/^-/, 'q{\\\\-}'],
400 [qr/^\+ /, 'q{\\\\+ }'],
401 [qr/^(=+)/, 'q{\\\\}.$1'],
402 [qr/^(#{1,6}) /, 'q{\\\\}.$1.q{ }'],
403 [qr/`/, 'q{\\\\`}'],
404 [qr/^~~~/, 'q{\\\\~~~}'],
405 [qr/\[/, 'q{\\\\[}'],
406 [qr/\]/, 'q{\\\\]}'],
407 [qr/^>/, 'q{\\\\>}'],
408 [qr/_/, 'q{\\\\_}'],
409 # Joplin uses this, but I wonder why there are underscores in the source HTML
410 # that should not be escaped?!
411 #[qr/(^|\p{Punctuation}|\p{Separator}|\p{Symbol})_(\P{Separator})/, '$1.q{\\\\_}.$2'],
412 [qr/^(\d+)\. /, '$1.q{\\. }']
413 );
414  
415 22 ▶sub keep( $self, $filter ) {
Subroutine keep: called CC=1 SCAR=0.0
Pod undocumented
416 22 $self->rules->keep($filter);
417 22 return $self
418 }
419  
420 22 ▶sub preprocess( $self, $proc ) {
Subroutine preprocess: called CC=1 SCAR=0.0
Pod undocumented
421 22 $self->rules->preprocess($proc);
422 22 return $self
423 }
424  
425 154 ▶sub addRule( $self, $name, $rule ) {
Subroutine addRule: called CC=1 SCAR=0.0
Pod undocumented
426 154 $self->rules->add( $name, $rule );
427 154 return $self
428 }
429  
430 295 ▶sub escape( $self, $str ) {
Subroutine escape: called CC=1 SCAR=0.0
Pod undocumented
431 3835 ▶ return reduce( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
432 3835 $a =~ s/$b->[0]/$b->[1]/gee;
433 3835 $a
434 295 }, $str, @escapes );
435 }
436  
437 924 ▶sub process( $self, $parentNode, $context ) {
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
438 1078 ▶ return reduce( sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
439 1078 my( $output ) = $a;
440 1078 my $node = Text::HTML::Turndown::Node->new( _node => $b, options => $self->options );
441  
442 1078 my $replacement = '';
443 1078 ▶ if( $node->nodeType == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->nodeType == 3) { } 320 758 1078
elsif ($node->nodeType == 1) { } 757 1 758
444 #say sprintf '%s %s', $node->nodeName, ($node->isCode ? '1' : '0');
445  
446 320 ▶ $replacement = $node->isCode ? $node->nodeValue : $self->escape($node->nodeValue);
Branch: 1 branch
BranchTrueFalseTotal
$node->isCode ? : 35 285 320
447  
448 } elsif( $node->nodeType == 1 ) {
449 757 $replacement = $self->replacementForNode($node, $context);
450 }
451  
452 1078 return _join( $output, $replacement )
453 924 }, '', $parentNode->childNodes->@* );
454 }
455  
456  
457 53 ▶sub isPreOrCode ($node) {
Subroutine isPreOrCode: called CC=2 SCAR=7.2
Pod undocumented
458 53 ▶ return uc($node->nodeName) eq 'PRE' || uc( $node->nodeName ) eq 'CODE'
Condition66%2/3
uc $node->nodeName eq 'PRE' || uc $node->nodeName eq 'CODE'
l!l&&r!l&&!r
01043
MC/DC50%1/2
uc $node->nodeName eq 'PRE' || uc $node->nodeName eq 'CODE'
uc $node->nodeName eq 'PRE' uc $node->nodeName eq 'CODE'
Truth table66%2/3
A || B
A= uc $node->nodeName eq 'PRE'B= uc $node->nodeName eq 'CODE'
ABresult
1X1
011
000
459 }
460  
461 167 ▶sub turndown( $self, $input ) {
Subroutine turndown: called CC=4 SCAR=14.0
Pod undocumented
462 167 ▶ if( ! ref $input ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $input) 166 1 167
463 166 ▶ if( $input eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 0 166 166
464 0 return ''
465 }
466 166 $input = $self->html_parser->parse_html_string( $input, { recover => 2, encoding => 'UTF-8' });
467 };
468  
469 167 for my $proc ($self->rules->_preprocess->@*) {
470 189 $input = $proc->($input);
471 }
472  
473 167 my $context = {
474 references => [],
475 };
476 167 my $output = $self->process( $input, $context );
477 167 return $self->postProcess( $output, $context );
478 }
479  
480 167 ▶sub postProcess( $self, $output, $context ) {
Subroutine postProcess: called CC=1 SCAR=0.0
Pod undocumented
481 2659 ▶ $self->rules->forEach(sub($rule) {
Subroutine __ANON__: called CC=5 SCAR=16.6
482 2659 ▶ if( ref $rule eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule eq "HASH") 2659 0 2659
483 2659 my $r = $rule->{append};
484 2659 ▶ if( $r
Branch: 1 branch
BranchTrueFalseTotal
if ($r and ref $r and ref $r eq "CODE") 167 2492 2659
Condition66%4/6
$r and ref $r and ref $r eq "CODE"
!ll&&!rl&&r
24920167
$r and ref $r
!ll&&!rl&&r
24920167
MC/DC33%1/3
$r and ref $r and ref $r eq "CODE"
$r ref $r ref $r eq "CODE"
Truth table50%2/4
A and B and C
A= $rB= ref $rC= ref $r eq "CODE"
ABCresult
0XX0
10X0
1100
1111
485 && ref $r
486 && ref $r eq 'CODE' ) {
487 167 $output = _join( $output, $r->($self->options, $context));
488 }
489 }
490 167 });
491  
492 167 $output =~ s/^[\t\r\n]+//;
493 167 $output =~ s/[\t\r\n\s]+$//;
494  
495 167 return $output;
496 }
497  
498 757 ▶sub replacementForNode( $self, $node, $context ) {
Subroutine replacementForNode: called CC=3 SCAR=11.0
Pod undocumented
499 757 my $rule = $self->rules->forNode( $node );
500 757 my $content = $self->process( $node, $context );
501 757 my $whitespace = Text::HTML::Turndown::Node::flankingWhitespace($node, $self->options);
502  
503 757 ▶ if( $whitespace->{leading} || $whitespace->{trailing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($whitespace->{"leading"} or $whitespace->{"trailing"}) 21 736 757
Condition100%3/3
$whitespace->{"leading"} or $whitespace->{"trailing"}
l!l&&r!l&&!r
1110736
MC/DC100%2/2
$whitespace->{"leading"} or $whitespace->{"trailing"}
$whitespace->{"leading"} $whitespace->{"trailing"}
Truth table100%3/3
A or B
A= $whitespace->{"leading"}B= $whitespace->{"trailing"}
ABresult
1X1
011
000
504 21 $content =~s/^\s+//;
505 21 $content =~s/\s+$//;
506 }
507 my $res = (
508 $whitespace->{leading}
509 . $rule->{replacement}->($content, $node, $self->options, $context)
510 . $whitespace->{trailing}
511 757 );
512 757 $res
513 }
514  
515 1245 ▶sub _join ($output, $replacement) {
Subroutine _join: called CC=1 SCAR=0.0
516 1245 my $s1 = trimTrailingNewlines($output);
517 1245 my $s2 = trimLeadingNewlines($replacement);
518 1245 my $nls = max(length($output) - length($s1), length($replacement)- length($s2));
519 1245 my $separator = substr( "\n\n", 0, $nls);
520  
521 1245 return "$s1$separator$s2";
522 }
523  
524 34 ▶sub cleanAttribute( $attribute ) {
Subroutine cleanAttribute: called CC=2 SCAR=6.9
Pod undocumented
525 34 ▶ (defined $attribute) ? $attribute =~ s/(\n+\s*)+/\n/gr : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute ? : 8 26 34
526 }
527  
528 1245 ▶sub trimLeadingNewlines ($string) {
Subroutine trimLeadingNewlines: called CC=1 SCAR=0.0
Pod undocumented
529 1245 $string =~ s/^\n*//r;
530 }
531  
532 1245 ▶sub trimTrailingNewlines ($string) {
Subroutine trimTrailingNewlines: called CC=3 SCAR=11.0
Pod undocumented
533 # avoid match-at-end regexp bottleneck, see #370
534 1245 my $indexEnd = length($string);
535 1245 ▶ while ($indexEnd > 0 && substr( $string, $indexEnd-1, 1 ) eq "\n") { $indexEnd-- };
Branch: 1 branch
BranchTrueFalseTotal
if ($indexEnd > 0 and substr($string, $indexEnd - 1, 1) eq "\n") 915 1245 2160
Condition100%3/3
$indexEnd > 0 and substr($string, $indexEnd - 1, 1) eq "\n"
!ll&&!rl&&r
887358915
MC/DC100%2/2
$indexEnd > 0 and substr($string, $indexEnd - 1, 1) eq "\n"
$indexEnd > 0 substr($string, $indexEnd - 1, 1) eq "\n"
Truth table100%3/3
A and B
A= $indexEnd > 0B= substr($string, $indexEnd - 1, 1) eq "\n"
ABresult
0X0
100
111
536 1245 return substr( $string, 0, $indexEnd )
537 }
538  
539 88 ▶sub use( $self, $plugin ) {
Subroutine use: called CC=4 SCAR=16.9
Pod undocumented
540 88 ▶ if( ref $plugin and ref $plugin eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $plugin and ref $plugin eq 'ARRAY') { } 0 88 88
Condition33%1/3
ref $plugin and ref $plugin eq "ARRAY"
!ll&&!rl&&r
8800
MC/DC0%0/2
ref $plugin and ref $plugin eq "ARRAY"
ref $plugin ref $plugin eq "ARRAY"
Truth table33%1/3
A and B
A= ref $pluginB= ref $plugin eq "ARRAY"
ABresult
0X0
100
111
541 0 $self->use( $_ ) for $plugin->@*
542 } else {
543 88 load $plugin;
544 88 $plugin->install( $self );
545 }
546 }
547  
548 6 1;
549  
550 =head1 FUNCTIONS
551  
552 =head2 C<< html2markdown >>
553  
554 use Text::HTML::Turndown 'html2markdown';
555 my $frontmatter = html2markdown( $html, %frontmatter_defaults );
556 say $frontmatter->document_string;
557  
558 Converts an HTML string to a L<Text::FrontMatter::YAML> object.
559 The HTML string is assumed to contain UTF-8 octets.
560  
561 =cut
562  
563 1 ▶sub html2markdown( $html, %keys ) {
Subroutine html2markdown: called CC=1 SCAR=0.0
Pod documented
564 1 my $tree = XML::LibXML->new->parse_html_string(
565 $html,
566 { recover => 2, encoding => 'UTF-8' }
567 );
568  
569 1 my $frontmatter = extract_info(
570 $tree,
571 #maybe url => $url,
572 );
573  
574 1 my $convert = Text::HTML::Turndown->new();
575 1 my $markdown = $convert->turndown($tree);
576  
577 1 my $tfm = Text::FrontMatter::YAML->new(
578 frontmatter_hashref => { %keys, $frontmatter->%* },
579 data_text => $markdown,
580 );
581  
582 1 return $tfm
583 }
584  
585 =head1 MARKDOWN FLAVOURS / FEATURES
586  
587 =head1 COMPATIBILITY
588  
589 This port aims to be compatible with the Javascript code and uses the same
590 test suite. But the original library does not pass its tests and the Joplin
591 part does not use the original tests.
592  
593 =over 4
594  
595 =item Table headers
596  
597 For Github flavoured markdown, Joplin aims to always force table headers in
598 markdown. This libary does not (yet).
599  
600 =back
601  
602 =head1 SEE ALSO
603  
604 The original library (unmaintained):
605  
606 L<https://github.com/mixmark-io/turndown/>
607  
608 The Joplin library (maintained):
609  
610 L<https://github.com/laurent22/joplin/tree/dev/packages/turndown>
611  
612 L<https://github.com/laurent22/joplin/tree/dev/packages/turndown-plugin-gfm>
613  
614 =head1 REPOSITORY
615  
616 The public repository of this module is
617 L<https://github.com/Corion/Text-HTML-Turndown>.
618  
619 =head1 SUPPORT
620  
621 The public support forum of this module is L<https://perlmonks.org/>.
622  
623 =head1 BUG TRACKER
624  
625 Please report bugs in this module via the Github bug queue at
626 L<https://github.com/Corion/Text-HTML-Turndown/issues>
627  
628 =head1 AUTHOR
629  
630 Max Maischein C<corion@cpan.org>
631  
632 =head1 COPYRIGHT (c)
633  
634 Copyright 2025- by Max Maischein C<corion@cpan.org>.
635  
636 =head1 LICENSE
637  
638 This module is released under the Artistic License 2.0.
639  
640 =cut
« ExtractInfo.pm ↑ Summary Turndown/Rules.pm »