Turndown/Tables.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 83.3% 15 / 18 Conditioncond 82.7% 24 / 29 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 4 totaltotal 87.2% 144 / 165 CC 26 SCAR 15.1
CC
26
Cov
92%
__ANON__
8.0
isHeadingRow
7.0
__ANON__
6.0
CRAP 4.5
« Turndown/Node.pm ↑ Summary ExtractInfo.pm »
1 package Text::HTML::Turndown::Tables 0.12;
2 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use stable 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::MoreUtils 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Text::HTML::Turndown::Tables - rules for Markdown Tables
12  
13 =head1 SYNOPSIS
14  
15 use Text::HTML::Turndown;
16 my $turndown = Text::HTML::Turndown->new(%$options);
17 $turndown->use('Text::HTML::Turndown::Tables');
18  
19 my $markdown = $convert->turndown(<<'HTML');
20 <table><tr><td>Hello</td><td>world!</td></tr></table>
21 HTML
22 # | Hello | world! |
23 # | ----- | ------ |
24  
25 =cut
26  
27 our %RULES = (
28  
29 tableCell => {
30 filter => ['th', 'td'],
31 80 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 80 return cell($content, $node, undef);
33 },
34 },
35  
36 tableRow => {
37 filter => 'tr',
38 37 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=8 SCAR=20.8
39 37 my $borderCells = '';
40 37 my $alignMap = { left => ':--', right => '--:', center => ':-:' };
41  
42 # Eliminate empty rows
43 37 ▶ if( $content =~ m!\A\|( \|)+\z! ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\A\|( \|)+\z/u) 2 35 37
44 2 return '';
45 }
46  
47 35 ▶ if (isHeadingRow($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isHeadingRow($node)) 11 24 35
48 #warn "Header content: [$content]";
49 11 my @ch = $node->childNodes;
50 11 for my $ch ($node->childNodes) {
51 21 my $border = '---';
52 21 ▶ my $align = lc(
Condition100%2/2
$ch->getAttribute('align') || ''
l!l
813
MC/DC100%1/1
$ch->getAttribute('align') || ''
$ch->getAttribute('align')
Truth table100%2/2
A
A= $ch->getAttribute('align')
Aresult
11
00
53 $ch->getAttribute('align') || ''
54 );
55  
56 21 ▶ if ($align) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align) 8 13 21
57 8 ▶ $border = $alignMap->{$align} || $border;
Condition66%2/3
$alignMap->{$align} || $border
l!l&&r!l&&!r
620
MC/DC0%0/2
$alignMap->{$align} || $border
$alignMap->{$align} $border
Truth table66%2/3
A || B
A= $alignMap->{$align}B= $border
ABresult
1X1
011
000
58 }
59  
60 21 $borderCells .= cell($border, $ch, undef)
61 }
62 }
63 35 ▶ return "\n" . $content . ($borderCells ? "\n" . $borderCells : '')
Branch: 1 branch
BranchTrueFalseTotal
$borderCells ? : 11 24 35
64 }
65 },
66  
67 table => {
68 filter => ['table'],
69  
70 13 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
71 # Ensure there are no blank lines
72 13 $content =~ s/\n\n/\n/g;
73 13 $content =~ s/^\s*//;
74 # Re-parse and re-layout the table:
75 13 my @table = split /\r?\n/, $content;
76 13 my @new_table;
77 my @column_width;
78 13 for my $row (@table) {
79 46 $row =~ s!^\|\s*!!;
80 46 $row =~ s!\s+\|\s*\z!!;
81 46 my @cols = map {s!^\s+!!; s!\s+\z!!r; } split /\|/, $row;
82 46 push @new_table, \@cols;
83 };
84 13 my $h = shift @new_table;
85 13 $h = [map { $_, \" | " } $h->@*];
86 13 pop $h->@*;
87 13 unshift $h->@*, \"| ";
88 13 push $h->@*, \" |";
89 13 my $table = Text::Table->new(
90 $h->@*,
91 );
92 #shift @new_table;
93 13 $table->load( @new_table );
94 13 $content = "\n\n" . $table . "\n\n";
95 },
96  
97 },
98  
99 tableSection => {
100 filter => ['thead', 'tbody', 'tfoot'],
101 19 ▶ replacement => sub( $content, $node, $options, $context ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 19 return $content
103 }
104 }
105 1 );
106  
107 # A tr is a heading row if:
108 # - the parent is a THEAD
109 # - or if its the first child of the TABLE or the first TBODY (possibly
110 # following a blank THEAD)
111 # - and every cell is a TH
112 35 ▶sub isHeadingRow ($tr) {
Subroutine isHeadingRow: called CC=7 SCAR=19.5
Pod undocumented
113 35 ▶ return if ! $tr;
Branch: 1 branch
BranchTrueFalseTotal
unless $tr 0 35 35
114 35 my $parentNode = $tr->parentNode;
115 35 ▶ my $n = $tr->can('_node') ? $tr->_node : $tr;
Branch: 1 branch
BranchTrueFalseTotal
$tr->can('_node') ? : 35 0 35
116 return (
117 uc ($parentNode->nodeName) eq 'THEAD' ||
118 (
119 $n->isEqual($parentNode->firstChild)
120 && (uc $parentNode->nodeName eq 'TABLE' || isFirstTbody($parentNode))
121 35 ▶ && all { uc($_->nodeName) eq 'TH' } $tr->childNodes
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%12/12
uc $parentNode->nodeName eq 'THEAD' || $n->isEqual($parentNode->firstChild) && (uc $parentNode->nodeName eq 'TABLE' || isFirstTbody($parentNode)) && &all(sub { uc $_->nodeName eq 'TH'; } , $tr->childNodes)
l!l&&r!l&&!r
7424
$n->isEqual($parentNode->firstChild) and uc $parentNode->nodeName eq "TABLE" || isFirstTbody($parentNode) and &all(sub { uc $_->nodeName eq "TH"; } , $tr->childNodes)
!ll&&!rl&&r
2224
$n->isEqual($parentNode->firstChild) and uc $parentNode->nodeName eq "TABLE" || isFirstTbody($parentNode)
!ll&&!rl&&r
1486
uc $parentNode->nodeName eq "TABLE" or isFirstTbody($parentNode)
l!l&&r!l&&!r
338
MC/DC100%5/5
uc $parentNode->nodeName eq 'THEAD' || $n->isEqual($parentNode->firstChild) && (uc $parentNode->nodeName eq 'TABLE' || isFirstTbody($parentNode)) && &all(sub { uc $_->nodeName eq 'TH'; } , $tr->childNodes)
uc $parentNode->nodeName eq 'THEAD' $n->isEqual($parentNode->firstChild) uc $parentNode->nodeName eq "TABLE" isFirstTbody($parentNode) &all(sub { uc $_->nodeName eq "TH"; } , $tr->childNodes)
Truth table85%6/7
A || B and C or D and E
A= uc $parentNode->nodeName eq 'THEAD'B= $n->isEqual($parentNode->firstChild)C= uc $parentNode->nodeName eq "TABLE"D= isFirstTbody($parentNode)E= &all(sub { uc $_->nodeName eq "TH"; } , $tr->childNodes)
ABCDEresult
1XXXX1
011X11
010111
00XXX0
0100X0
011X00
010100
122 )
123 )
124 }
125  
126 11 ▶sub isFirstTbody ($element) {
Subroutine isFirstTbody: called CC=4 SCAR=13.9
Pod undocumented
127 11 my $previousSibling = $element->previousSibling;
128 return (
129 11 ▶ uc $element->nodeName eq 'TBODY'
Condition88%8/9
uc $element->nodeName eq 'TBODY' && (!$previousSibling || uc $previousSibling->nodeName eq 'THEAD' && $previousSibling->textContent =~ /^\s*$/u)
!ll&&!rl&&r
083
not $previousSibling or uc $previousSibling->nodeName eq "THEAD" and $previousSibling->textContent =~ /^\s*$/u
l!l&&r!l&&!r
218
uc $previousSibling->nodeName eq "THEAD" and $previousSibling->textContent =~ /^\s*$/u
!ll&&!rl&&r
171
MC/DC75%3/4
uc $element->nodeName eq 'TBODY' && (!$previousSibling || uc $previousSibling->nodeName eq 'THEAD' && $previousSibling->textContent =~ /^\s*$/u)
uc $element->nodeName eq 'TBODY' not $previousSibling uc $previousSibling->nodeName eq "THEAD" $previousSibling->textContent =~ /^\s*$/u
Truth table80%4/5
A && not(B) or C and D
A= uc $element->nodeName eq 'TBODY'B= not $previousSiblingC= uc $previousSibling->nodeName eq "THEAD"D= $previousSibling->textContent =~ /^\s*$/u
ABCDresult
0XXX0
100X0
10100
11XX1
10111
130 && (
131 !$previousSibling ||
132 (
133 uc $previousSibling->nodeName eq 'THEAD'
134 && $previousSibling->textContent =~ /^\s*$/
135 )
136 )
137 )
138 }
139  
140 101 ▶sub cell ($content, $node, $escape=1) {
Subroutine cell: called CC=4 SCAR=14.0
Pod undocumented
141 101 my $first = !$node->previousSibling;
142 101 my $prefix = ' ';
143 101 ▶ if ($first) { $prefix = '| ' };
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 48 53 101
144  
145 # We assume that we have no further HTML tags contained in $content
146 # convert all elements in $content into their Markdown equivalents
147 101 ▶ if( $escape ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape) 0 101 101
148 0 $content = Text::HTML::Turndown->escape( $content );
149 }
150  
151 # Fix up newlines
152 101 $content =~ s!\r?\n!<br/>!g;
153  
154 101 return $prefix . $content . ' |'
155 }
156  
157 22 ▶sub install ($class, $target) {
Subroutine install: called CC=2 SCAR=7.4
Pod undocumented
158 22 ▶ $target->preprocess(sub($tree) {
Subroutine __ANON__: called CC=4 SCAR=13.9
159 # We will likely need other/more rules to turn arbitrary HTML
160 # into what a browser has as DOM for tables
161 22 for my $table ($tree->find('//table')->@*) {
162 # Turn <table><thead><td>...
163 # into <table><thead><tr><td>...
164 13 ▶ if( $table->find( './thead/td' )->@* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$table->find("./thead/td")}) 1 12 13
165 1 my $head = $table->find('./thead',$table)->shift;
166 1 my $tr = $head->ownerDocument->createElement('tr');
167 1 $tr->appendChild($_) for $head->childNodes;
168 1 $head->appendChild( $tr );
169 }
170 }
171 22 return $tree;
172 22 });
173 0 ▶ $target->keep(sub ($node) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 my $firstRow = $node->find('.//tr')->shift;
175 0 ▶ return uc $node->nodeName eq 'TABLE' && !isHeadingRow($firstRow)
Condition0%0/3
uc $node->nodeName eq 'TABLE' && !isHeadingRow($firstRow)
!ll&&!rl&&r
000
MC/DC0%0/2
uc $node->nodeName eq 'TABLE' && !isHeadingRow($firstRow)
uc $node->nodeName eq 'TABLE' !isHeadingRow($firstRow)
Truth table0%0/3
A && not(B)
A= uc $node->nodeName eq 'TABLE'B= !isHeadingRow($firstRow)
ABresult
0X0
100
111
176 22 });
177 22 for my $key (keys %RULES) {
178 88 $target->addRule($key, $RULES{$key})
179 }
180 }
181  
182 1 1;
183  
184 =head1 REPOSITORY
185  
186 The public repository of this module is
187 L<https://github.com/Corion/Text-HTML-Turndown>.
188  
189 =head1 SUPPORT
190  
191 The public support forum of this module is L<https://perlmonks.org/>.
192  
193 =head1 BUG TRACKER
194  
195 Please report bugs in this module via the Github bug queue at
196 L<https://github.com/Corion/Text-HTML-Turndown/issues>
197  
198 =head1 AUTHOR
199  
200 Max Maischein C<corion@cpan.org>
201  
202 =head1 COPYRIGHT (c)
203  
204 Copyright 2025- by Max Maischein C<corion@cpan.org>.
205  
206 =head1 LICENSE
207  
208 This module is released under the Artistic License 2.0.
209  
210 =cut
« Turndown/Node.pm ↑ Summary ExtractInfo.pm »