blib/lib/Markdown/Pod/Embed.pm

filter: Statementstmt 92.3% 267 / 289 Branchbran 74.1% 92 / 124 Conditioncond 56.3% 40 / 71 MC/DCmcdc 32.5% 13 / 40 Subroutinesub 91.6% 33 / 36 Podpod 0.0% 0 / 23 totaltotal 76.3% 445 / 583 CC 99 SCAR 25.1
CC
99
Cov
82%
markpod_markdown_text
56.0
markpod_process
23.1
markpod_end_normalize
14.2
CRAP 12.4
↑ Summary blib/lib/Markdown/Pod/Embed/Util.pm »
1 #
2 # This file is part of Markdown::Pod::Embed.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package Markdown::Pod::Embed;
14  
15  
16 # Pragma
17 #
18 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use vars qw($VERSION $AUTHORITY);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 # Base Packages
24 #
25 8 ▶use Markdown::Pod::Embed::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use Markdown::Pod::Embed::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 # Other external modules
30 #
31 8 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use Markdown::Pod;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 # Version information
41 #
42 8 $AUTHORITY='cpan:ASPEER';
43 8 $VERSION='1.013';
44  
45  
46 # Done
47 #
48 8 1;
49  
50  
51 #===================================================================================================
52 #
53 # Object creation and import methods
54 #
55  
56  
57 20 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
58  
59 # Bless self ref and retun
60 #
61 20 my ($class, $opt_hr)=@_;
62  
63  
64 # Get default options and overrides
65 #
66 my %opt=(
67 20 ▶ %{$OPTION_HR},
Branch: 1 branch
BranchTrueFalseTotal
$opt_hr ? : 15 5 20
68 20 $opt_hr ? %{$opt_hr} : ()
69 );
70  
71  
72 # Done
73 #
74 20 return bless({opt=>\%opt}, $class);
75  
76 }
77  
78  
79 #===================================================================================================
80 #
81 # Markdown processing and source updates
82 #
83 20 ▶sub markpod_process {
Subroutine markpod_process: called CC=23 SCAR=31.4
Pod undocumented
84  
85  
86 # Find and replace POD in a file
87 #
88 20 my ($self, $fn)=@_;
89 20 delete @{$self}{qw(pod_changed markdown pod ppi_doc_or)};
90 20 debug("processing file: $fn");
91  
92  
93 # Create new PPI documents from supplied file
94 #
95 20 ▶ my $ppi_doc_or=PPI::Document->new($fn) ||
Condition50%1/2
'PPI::Document'->new($fn) || (return err("nable to create new PPI instance on file $fn"))
l!l
200
MC/DC0%0/1
'PPI::Document'->new($fn) || (return err("nable to create new PPI instance on file $fn"))
'PPI::Document'->new($fn)
Truth table50%1/2
A
A= 'PPI::Document'->new($fn)
Aresult
11
00
96 return err ("nable to create new PPI instance on file $fn");
97  
98  
99 20 my $sidecar_md=$self->markpod_markdown_file_read($fn);
100 20 my $end_or=$ppi_doc_or->find_first('PPI::Statement::End');
101  
102  
103 # Find Pod section and massage. If no POD exists, create one from sidecar markdown
104 #
105 20 my $pod_or_ar=$ppi_doc_or->find('PPI::Token::Pod');
106 20 ▶ unless ($pod_or_ar) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pod_or_ar) 7 13 20
107 7 ▶ unless (defined $sidecar_md) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sidecar_md) 1 6 7
108 1 return undef;
109 }
110 6 my $original=$ppi_doc_or->serialize();
111 6 ▶ die "cannot append documentation after a data section in $fn\n"
Branch: 1 branch
BranchTrueFalseTotal
if $original =~ /^__DATA__\b/m or $end_or and not $end_or->content =~ /\A__END__\s*\z/ 1 5 6
Condition66%4/6
$original =~ /^__DATA__\b/m or $end_or and not $end_or->content =~ /\A__END__\s*\z/
l!l&&r!l&&!r
105
$end_or and not $end_or->content =~ /\A__END__\s*\z/
!ll&&!rl&&r
410
MC/DC33%1/3
$original =~ /^__DATA__\b/m or $end_or and not $end_or->content =~ /\A__END__\s*\z/
$original =~ /^__DATA__\b/m $end_or not $end_or->content =~ /\A__END__\s*\z/
Truth table75%3/4
A or B and not(C)
A= $original =~ /^__DATA__\b/mB= $end_orC= not $end_or->content =~ /\A__END__\s*\z/
ABCresult
1XX1
0111
00X0
0100
112 if $original=~/^__DATA__\b/m || ($end_or && $end_or->content()!~/\A__END__\s*\z/);
113 5 ▶ $self->markpod_end_normalize($end_or) if $end_or;
Branch: 1 branch
BranchTrueFalseTotal
if $end_or 1 4 5
114 5 my $pod_md=$sidecar_md;
115 5 ▶ my $pod=
Condition50%1/2
$self->markpod_pod_merge($pod_md, $fn) || (return err())
l!l
50
MC/DC0%0/1
$self->markpod_pod_merge($pod_md, $fn) || (return err())
$self->markpod_pod_merge($pod_md, $fn)
Truth table50%1/2
A
A= $self->markpod_pod_merge($pod_md, $fn)
Aresult
11
00
116 $self->markpod_pod_merge($pod_md, $fn) ||
117 return err();
118 5 $pod.="\n=cut\n";
119 5 ▶ unless ($end_or) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($end_or) 4 1 5
120 4 $ppi_doc_or->add_element(PPI::Token::Separator->new("__END__\n\n"));
121 }
122 5 $ppi_doc_or->add_element(PPI::Document->new(\$pod));
123 5 @{$self}{qw(
124  
125 pod_changed
126 markdown
127 ppi_doc_or
128  
129 )}=(
130  
131 1,
132 $pod_md,
133 $ppi_doc_or
134 );
135 5 return 1;
136 }
137 13 my $end_changed=0;
138 13 ▶ if ($end_or && (defined $sidecar_md || grep { $_->content()=~/^=begin markdown(?=\s*)/im } @{$pod_or_ar})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_or and defined $sidecar_md || grep({$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;})) 12 1 13
Condition83%5/6
$end_or and defined $sidecar_md || grep({$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;})
!ll&&!rl&&r
0112
defined $sidecar_md or grep {$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;}
l!l&&r!l&&!r
661
MC/DC66%2/3
$end_or and defined $sidecar_md || grep({$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;})
$end_or defined $sidecar_md grep {$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;}
Truth table75%3/4
A and B or C
A= $end_orB= defined $sidecar_mdC= grep {$_->content =~ /^=begin markdown(?=\s*)/im;} @{$pod_or_ar;}
ABCresult
0XX0
1000
11X1
1011
139 12 $end_changed=$self->markpod_end_normalize($end_or);
140 }
141 13 my $sidecar_target_idx=0;
142 13 ▶ if (defined $sidecar_md) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sidecar_md) 6 7 13
143 6 foreach my $idx (0 .. $#{$pod_or_ar}) {
144 6 ▶ if ($pod_or_ar->[$idx]->content()=~/^=begin markdown(?=\s*)/im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod_or_ar->[$idx]->content =~ /^=begin markdown(?=\s*)/im) 5 1 6
145 5 $sidecar_target_idx=$idx;
146 5 last;
147 }
148 }
149 }
150 13 my ($md, $pod_changed, @pod, @raw_pod)=(undef, $end_changed);
151 13 foreach my $idx (0 .. $#{$pod_or_ar}) {
152 15 my $pod_or=$pod_or_ar->[$idx];
153 15 my $pod_content=$pod_or->content();
154 15 my $pod_md=$self->markpod_markdown_extract($pod_content);
155 15 ▶ if (defined $sidecar_md && $idx == $sidecar_target_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sidecar_md and $idx == $sidecar_target_idx) 6 9 15
Condition66%2/3
defined $sidecar_md and $idx == $sidecar_target_idx
!ll&&!rl&&r
906
MC/DC50%1/2
defined $sidecar_md and $idx == $sidecar_target_idx
defined $sidecar_md $idx == $sidecar_target_idx
Truth table66%2/3
A and B
A= defined $sidecar_mdB= $idx == $sidecar_target_idx
ABresult
0X0
100
111
156 6 $pod_md=$sidecar_md;
157 6 undef $sidecar_md;
158 }
159 15 ▶ unless (defined $pod_md) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pod_md) 2 13 15
160 2 debug("pod: no markdown source, preserving existing POD");
161 2 push @pod, $pod_content;
162 2 push @raw_pod, $pod_content;
163 2 next;
164 }
165 13 $md.=$pod_md;
166 13 ▶ my $pod=
Condition50%1/2
$self->markpod_pod_merge($pod_md, $fn) || (return err())
l!l
130
MC/DC0%0/1
$self->markpod_pod_merge($pod_md, $fn) || (return err())
$self->markpod_pod_merge($pod_md, $fn)
Truth table50%1/2
A
A= $self->markpod_pod_merge($pod_md, $fn)
Aresult
11
00
167 $self->markpod_pod_merge($pod_md, $fn) ||
168 return err();
169 13 push @raw_pod, $self->{'pod'};
170 13 $pod.="\n=cut\n";
171 13 ▶ if ($pod_changed += ($pod ne $pod_content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod_changed += $pod ne $pod_content) { } 10 3 13
172 10 debug("pod: updating");
173 10 $pod_or->set_content($pod);
174 }
175 else {
176 3 debug("pod: no change, not updating");
177 }
178 13 push @pod, $pod;
179 }
180  
181  
182 # Join POD
183 #
184 13 my $pod=join($/, @raw_pod);
185  
186  
187 # Store results
188 #
189 13 ▶ @{$self}{qw(
Condition100%2/2
$md || ''
l!l
121
MC/DC100%1/1
$md || ''
$md
Truth table100%2/2
A
A= $md
Aresult
11
00
190  
191 pod_changed
192 markdown
193 ppi_doc_or
194  
195 )}=(
196  
197 $pod_changed,
198 $md || '',
199 $ppi_doc_or
200 );
201  
202  
203 # Return number of pod lines that would have changed
204 #
205 13 return $pod_changed;
206  
207 }
208  
209  
210 13 ▶sub markpod_end_normalize {
Subroutine markpod_end_normalize: called CC=14 SCAR=26.5
Pod undocumented
211  
212 13 my ($self, $end_or)=@_;
213 13 my $content=$end_or->content();
214 13 my @children=$end_or->children();
215 13 ▶ return 0 unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 13 13
216  
217 13 my $pod_idx;
218 13 foreach my $idx (0 .. $#children) {
219 49 ▶ if ($children[$idx]->isa('PPI::Token::Pod')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($children[$idx]->isa("PPI::Token::Pod")) 12 37 49
220 12 $pod_idx=$idx;
221 12 last;
222 }
223 }
224  
225 13 ▶ my $last_gap_idx=defined $pod_idx ? $pod_idx - 1 : $#children;
Branch: 1 branch
BranchTrueFalseTotal
defined $pod_idx ? : 12 1 13
226 13 foreach my $idx (1 .. $last_gap_idx) {
227 24 ▶ return 0 if $children[$idx]->content()=~/\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $children[$idx]->content =~ /\S/ 0 24 24
228 }
229 13 foreach my $idx (0 .. $#children) {
230 53 ▶ if ($idx == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($idx == 0) { } 13 40 53
elsif ($idx == 1 and $idx <= $last_gap_idx) { } 12 28 40
elsif ($idx <= $last_gap_idx) { } 12 16 28
231 13 $children[$idx]->set_content("__END__");
232 }
233 ▶ elsif ($idx == 1 && $idx <= $last_gap_idx) {
Condition66%2/3
$idx == 1 and $idx <= $last_gap_idx
!ll&&!rl&&r
28012
MC/DC50%1/2
$idx == 1 and $idx <= $last_gap_idx
$idx == 1 $idx <= $last_gap_idx
Truth table66%2/3
A and B
A= $idx == 1B= $idx <= $last_gap_idx
ABresult
0X0
100
111
234 12 $children[$idx]->set_content("\n\n");
235 }
236 elsif ($idx <= $last_gap_idx) {
237 12 $children[$idx]->set_content('');
238 }
239 }
240 13 ▶ if (!defined $pod_idx && @children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pod_idx and @children == 1) 1 12 13
Condition66%2/3
not defined $pod_idx and @children == 1
!ll&&!rl&&r
1201
MC/DC50%1/2
not defined $pod_idx and @children == 1
not defined $pod_idx @children == 1
Truth table66%2/3
not(A) and B
A= not defined $pod_idxB= @children == 1
ABresult
0X0
100
111
241 1 $end_or->add_element(PPI::Token::Whitespace->new("\n\n"));
242 }
243 13 return $content ne $end_or->content();
244  
245 }
246  
247  
248 23 ▶sub markpod_markdown_file_read {
Subroutine markpod_markdown_file_read: called CC=3 SCAR=11.2
Pod undocumented
249  
250 23 my ($self, $fn)=@_;
251 23 my $md_fn="${fn}.md";
252 23 ▶ return undef unless -f $md_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $md_fn 10 13 23
253 13 my $md=slurp($md_fn);
254 13 chomp($md);
255 13 $md=$self->markpod_markdown_normalize($md);
256 13 ▶ unless (length $md) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $md) 0 13 13
257 0 debug("markdown sidecar file is empty, falling back to embedded markdown: $md_fn");
258 0 return undef;
259 }
260 13 debug("using markdown sidecar file: $md_fn");
261 13 return $md;
262  
263 }
264  
265  
266  
267 15 ▶sub markpod_inplace_update {
Subroutine markpod_inplace_update: called CC=10 SCAR=23.9
Pod undocumented
268  
269  
270 # Update file in place
271 #
272 15 my ($self, $fn)=@_;
273 15 ▶ my $ppi_doc_or=$self->ppi_doc_or()
Condition50%1/2
$self->ppi_doc_or || (return err())
l!l
150
MC/DC0%0/1
$self->ppi_doc_or || (return err())
$self->ppi_doc_or
Truth table50%1/2
A
A= $self->ppi_doc_or
Aresult
11
00
274 || return err();
275 15 ▶ return 1 if $self->{'opt'}{'dry_run'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt"}{"dry_run"} 1 14 15
276 14 ▶ die "refusing to replace symlink $fn\n" if -l $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -l $fn 0 14 14
277  
278  
279 # Make a backup copy if needed
280 #
281 14 debug("updating file ${fn}");
282 14 ▶ unless ($self->{'opt'}{'nobackup'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"opt"}{"nobackup"}) 1 13 14
283 1 ▶ File::Copy::copy($fn, "${fn}.bak") || die "unable to back up $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($fn, "$fn.bak") or die "unable to back up ${fn}: $!\n" 1 0 1
284 }
285  
286  
287 # Save
288 #
289 14 my ($output_fh, $temporary_fn)=tempfile('.markpod-XXXXXX', DIR => dirname($fn), UNLINK => 1);
290 14 binmode($output_fh);
291 14 ▶ print {$output_fh} $ppi_doc_or->serialize() or die "unable to write $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$output_fh;} $ppi_doc_or->serialize or die "unable to write ${fn}: $!\n" 14 0 14
292 14 ▶ close($output_fh) || die "unable to close $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $output_fh or die "unable to close ${fn}: $!\n" 14 0 14
293 14 ▶ chmod((stat($fn))[2] & 07777, $temporary_fn) || die "unable to preserve mode of $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod +(stat $fn)[2] & 4095, $temporary_fn or die "unable to preserve mode of ${fn}: $!\n" 14 0 14
294 14 ▶ rename($temporary_fn, $fn) || die "unable to replace $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $temporary_fn, $fn or die "unable to replace ${fn}: $!\n" 14 0 14
295 14 return 1;
296  
297  
298 }
299  
300  
301 9 ▶sub markpod_process_and_update {
Subroutine markpod_process_and_update: called CC=4 SCAR=14.0
Pod undocumented
302  
303  
304 # Process a file and save any resulting changes
305 #
306 9 my ($self, $fn)=@_;
307 9 my $pod_changed=$self->markpod_process($fn);
308 8 ▶ return undef unless defined $pod_changed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pod_changed 0 8 8
309 8 ▶ if ($pod_changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod_changed) 6 2 8
310 6 ▶ $self->markpod_inplace_update($fn) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->markpod_inplace_update($fn) or return err() 6 0 6
311 return err();
312 }
313 8 return $pod_changed;
314  
315 }
316  
317  
318 3 ▶sub markpod_markdown_source {
Subroutine markpod_markdown_source: called CC=7 SCAR=19.5
Pod undocumented
319  
320  
321 # Resolve markdown source for a file using sidecar-first precedence
322 #
323 3 my ($self, $fn)=@_;
324 3 my $sidecar_md=$self->markpod_markdown_file_read($fn);
325 3 ▶ return $sidecar_md if defined $sidecar_md;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sidecar_md 1 2 3
326  
327  
328 # Fall back to embedded markdown in POD blocks
329 #
330 2 ▶ my $ppi_doc_or=PPI::Document->new($fn) ||
Condition50%1/2
'PPI::Document'->new($fn) || (return err("unable to create new PPI instance on file $fn"))
l!l
20
MC/DC0%0/1
'PPI::Document'->new($fn) || (return err("unable to create new PPI instance on file $fn"))
'PPI::Document'->new($fn)
Truth table50%1/2
A
A= 'PPI::Document'->new($fn)
Aresult
11
00
331 return err ("unable to create new PPI instance on file $fn");
332 2 ▶ my $pod_or_ar=$ppi_doc_or->find('PPI::Token::Pod') || return undef;
Condition50%1/2
$ppi_doc_or->find('PPI::Token::Pod') || (return undef)
l!l
20
MC/DC0%0/1
$ppi_doc_or->find('PPI::Token::Pod') || (return undef)
$ppi_doc_or->find('PPI::Token::Pod')
Truth table50%1/2
A
A= $ppi_doc_or->find('PPI::Token::Pod')
Aresult
11
00
333 2 my $md='';
334 2 foreach my $pod_or (@{$pod_or_ar}) {
335 2 ▶ my $pod_md=$self->markpod_markdown_extract($pod_or->content()) || next;
Condition100%2/2
$self->markpod_markdown_extract($pod_or->content) || (next)
l!l
11
MC/DC100%1/1
$self->markpod_markdown_extract($pod_or->content) || (next)
$self->markpod_markdown_extract($pod_or->content)
Truth table100%2/2
A
A= $self->markpod_markdown_extract($pod_or->content)
Aresult
11
00
336 1 $md.=$pod_md;
337 }
338 2 ▶ return length $md ? $md : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $md ? : 1 1 2
339  
340 }
341  
342  
343 17 ▶sub markpod_markdown_extract {
Subroutine markpod_markdown_extract: called CC=5 SCAR=18.0
Pod undocumented
344  
345 17 my ($self, $pod)=@_;
346 17 my $md;
347 17 ▶ if ($pod=~/^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gims || $pod=~/^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gims or $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims) { } 13 4 17
Condition66%2/3
$pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gims or $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims
l!l&&r!l&&!r
1304
MC/DC50%1/2
$pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gims or $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims
$pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gims $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims
Truth table66%2/3
A or B
A= $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*?)\n*^=end markdown\s*$/gimsB= $pod =~ /^=begin markdown(?=\s*)(.*?)\n(.*)\n*$/gims
ABresult
1X1
011
000
348 13 ▶ if (my $fn=$1) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $1) 0 13 13
349 0 $fn=~s/^\s*//;
350 0 debug("suggested output filename: $fn");
351 0 ▶ $self->{'opt'}{'outfile'} ||= $fn;
Condition0%0/3
$self->{"opt"}{"outfile"} ||= $fn
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"opt"}{"outfile"} ||= $fn
$self->{"opt"}{"outfile"} $fn
Truth table0%0/3
A ||= B
A= $self->{"opt"}{"outfile"}B= $fn
ABresult
1X1
011
000
352 }
353 13 $md=$2;
354 }
355 else {
356 4 return undef;
357 }
358 13 chomp($md);
359 13 $md=$self->markpod_markdown_normalize($md);
360 13 debug('extracted markdown: %d bytes', length($md));
361 13 return $md;
362  
363 }
364  
365  
366 26 ▶sub markpod_markdown_normalize {
Subroutine markpod_markdown_normalize: called CC=1 SCAR=0.0
Pod undocumented
367  
368 26 my ($self, $md)=@_;
369 26 $md=~s/\A(?:[ \t]*\r?\n)+//;
370 26 $md=~s/^[ \t]+$//mg;
371 26 return $md;
372  
373 }
374  
375  
376 19 ▶sub markpod_pod_merge {
Subroutine markpod_pod_merge: called CC=4 SCAR=13.9
Pod undocumented
377  
378 19 my ($self, $md, $fn)=@_;
379 19 my ($conversion_md, $replacement_ar)=$self->markpod_markdown_prepare($md);
380 19 ▶ my $md2pod_or=Markdown::Pod->new() ||
Condition50%1/2
'Markdown::Pod'->new || (return err('unable to create new Markdown::Pod object'))
l!l
190
MC/DC0%0/1
'Markdown::Pod'->new || (return err('unable to create new Markdown::Pod object'))
'Markdown::Pod'->new
Truth table50%1/2
A
A= 'Markdown::Pod'->new
Aresult
11
00
381 return err ('unable to create new Markdown::Pod object');
382 my $pod=$md2pod_or->markdown_to_pod(
383 19 dialect => $self->{'opt'}{'dialect'},
384 markdown => $conversion_md
385 );
386 19 $pod=$self->markpod_pod_module_links($pod, $fn);
387 19 foreach my $replacement_hr (@{$replacement_ar}) {
388 2 my $placeholder=$replacement_hr->{'placeholder'};
389 2 my $format=$replacement_hr->{'format'};
390 2 my $text=$replacement_hr->{'text'};
391 2 my $formatted=$self->markpod_pod_format($format, $text);
392 2 $pod=~s/\Q${format}<${placeholder}>\E/$formatted/g;
393 }
394 19 ▶ my $encoding=$md=~/[^\x00-\x7f]/ ? "=encoding utf8\n\n" : '';
Branch: 1 branch
BranchTrueFalseTotal
$md =~ /[^\x00-\x7f]/ ? : 1 18 19
395 19 $pod=~s/^[ \t]+$//mg;
396 # Make a note of raw POD for getter function
397 19 $self->{'pod'}="${encoding}${pod}";
398 19 debug('created pod: %d bytes', length($pod));
399 19 $pod=$encoding.join(
400 "\n",
401 '=begin markdown',
402 '',
403 $md,
404 '',
405 '=end markdown',
406 '',
407 $pod
408 );
409 # This is markdown merged with created POD
410 19 return $pod;
411  
412 }
413  
414  
415 19 ▶sub markpod_pod_module_links {
Subroutine markpod_pod_module_links: called CC=4 SCAR=13.9
Pod undocumented
416  
417 19 my ($self, $pod, $source_fn)=@_;
418 19 ▶ return $pod unless defined($source_fn) && length($source_fn);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source_fn and length $source_fn 1 18 19
Condition66%2/3
defined $source_fn and length $source_fn
!ll&&!rl&&r
1018
MC/DC50%1/2
defined $source_fn and length $source_fn
defined $source_fn length $source_fn
Truth table66%2/3
A and B
A= defined $source_fnB= length $source_fn
ABresult
0X0
100
111
419  
420  
421 # Resolve links to companion module sidecars for the POD rendering. The
422 # retained Markdown continues to use its repository-relative filename.
423 #
424 18 $pod=~s{L<([^<>|\r\n]+)\|([^<>|\r\n]+\.pm\.md)>}{
425 6 my ($label, $target)=($1, $2);
426 6 my $package=$self->markpod_module_link_package($source_fn, $target);
427 6 ▶ defined($package) ? "L<${label}|${package}>" : $&;
Branch: 1 branch
BranchTrueFalseTotal
defined $package ? : 4 2 6
428 }ge;
429 18 return $pod;
430  
431 }
432  
433  
434 6 ▶sub markpod_module_link_package {
Subroutine markpod_module_link_package: called CC=9 SCAR=23.9
Pod undocumented
435  
436 6 my ($self, $source_fn, $target)=@_;
437 6 ▶ return undef if File::Spec->file_name_is_absolute($target);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($target) 0 6 6
438  
439 6 my $sidecar_fn=File::Spec->rel2abs($target, dirname($source_fn));
440 6 ▶ return undef unless -f $sidecar_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $sidecar_fn 2 4 6
441  
442 4 my $module_fn=$sidecar_fn;
443 4 ▶ $module_fn=~s/\.md\z// || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$module_fn =~ s/\.md\z// or return undef 4 0 4
444 4 ▶ return undef unless -f $module_fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $module_fn 0 4 4
445  
446 4 ▶ my $ppi_doc_or=PPI::Document->new($module_fn) || return undef;
Condition50%1/2
'PPI::Document'->new($module_fn) || (return undef)
l!l
40
MC/DC0%0/1
'PPI::Document'->new($module_fn) || (return undef)
'PPI::Document'->new($module_fn)
Truth table50%1/2
A
A= 'PPI::Document'->new($module_fn)
Aresult
11
00
447 4 ▶ my $package_or=$ppi_doc_or->find_first('PPI::Statement::Package') ||
Condition50%1/2
$ppi_doc_or->find_first('PPI::Statement::Package') || (return undef)
l!l
40
MC/DC0%0/1
$ppi_doc_or->find_first('PPI::Statement::Package') || (return undef)
$ppi_doc_or->find_first('PPI::Statement::Package')
Truth table50%1/2
A
A= $ppi_doc_or->find_first('PPI::Statement::Package')
Aresult
11
00
448 return undef;
449 4 my $package=$package_or->namespace();
450 4 ▶ return defined($package) && $package=~/\A[A-Za-z_]\w*(?:::\w+)*\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $package && $package =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/ ? : 4 0 4
Condition33%1/3
defined $package and $package =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/
!ll&&!rl&&r
004
MC/DC0%0/2
defined $package and $package =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/
defined $package $package =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/
Truth table33%1/3
A and B
A= defined $packageB= $package =~ /\A[A-Za-z_]\w*(?:::\w+)*\z/
ABresult
0X0
100
111
451 ? $package
452 : undef;
453  
454 }
455  
456  
457 19 ▶sub markpod_markdown_prepare {
Subroutine markpod_markdown_prepare: called CC=3 SCAR=11.0
Pod undocumented
458  
459 19 my ($self, $md)=@_;
460 19 my @replacement;
461 19 my $counter=0;
462  
463 # A leading page title is useful in Markdown but would create an empty
464 # POD section immediately before the conventional NAME section.
465 #
466 19 $md=~s/\A[ \t]*\#[ \t]+[^\r\n]+?[ \t]*\#?[ \t]*\r?\n(?:[ \t]*\r?\n)+(?=[ \t]*\#[ \t]+NAME(?:[ \t]*\#)?[ \t]*(?:\r?\n|\z))//i;
467  
468 # Protect constructs that Markdent's GitHub dialect emits as ambiguous
469 # POD. The placeholders contain no Markdown punctuation and are restored
470 # with delimiters appropriate for their original content.
471 #
472 19 $md=~s{(?<!`)`(https?://[^`\r\n]+)`(?!`)}{
473 1 my $placeholder;
474 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(index($md, $placeholder) != -1) and do { do { $placeholder = sprintf("MARKPODTOKEN%06dX", ++$counter) } } 0 1 1
475 1 $placeholder=sprintf('MARKPODTOKEN%06dX', ++$counter);
476 } while (index($md, $placeholder)>=0);
477 1 push(@replacement, {
478 format => 'C',
479 placeholder => $placeholder,
480 text => $1
481 });
482 1 "`${placeholder}`";
483 }ge;
484 19 $md=~s{(?<!\S)\*\*([^*\r\n]*?>[^*\r\n]*)\*\*}{
485 1 my $placeholder;
486 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
(index($md, $placeholder) != -1) and do { do { $placeholder = sprintf("MARKPODTOKEN%06dX", ++$counter) } } 0 1 1
487 1 $placeholder=sprintf('MARKPODTOKEN%06dX', ++$counter);
488 } while (index($md, $placeholder)>=0);
489 1 push(@replacement, {
490 format => 'B',
491 placeholder => $placeholder,
492 text => $1
493 });
494 1 "**${placeholder}**";
495 }ge;
496  
497 19 return ($md, \@replacement);
498  
499 }
500  
501  
502 2 ▶sub markpod_pod_format {
Subroutine markpod_pod_format: called CC=4 SCAR=13.9
Pod undocumented
503  
504 2 my ($self, $format, $text)=@_;
505 2 ▶ return "${format}<${text}>" unless $text=~/[<>]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[<>]/ 1 1 2
506  
507 1 my $longest=0;
508 1 ▶ while ($text=~/(<+|>+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(<+|>+)/g) 1 1 2
509 1 my $length=length($1);
510 1 ▶ $longest=$length if $length>$longest;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $longest 1 0 1
511 }
512 1 my $delimiter='<' x ($longest + 1);
513 1 my $end_delimiter='>' x ($longest + 1);
514 1 return "${format}${delimiter} ${text} ${end_delimiter}";
515  
516 }
517  
518  
519 0 ▶sub markpod_markdown_text {
Subroutine markpod_markdown_text: not called CC=7 SCAR=40.3
Pod undocumented
520  
521  
522 # Convert Markdown to text
523 #
524 0 my ($self, $md, $fn)=@_;
525  
526  
527 # Need IPC::Run3
528 #
529 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IPC::Run3; 1 } } or return err("unable to load IPC::Run3 module") 0 0 0
530 0 require IPC::Run3;
531 0 1;
532 } || return err('unable to load IPC::Run3 module');
533  
534  
535 # Need pandoc for this bit
536 #
537 0 ▶ $PANDOC_EXE ||
Branch: 1 branch
BranchTrueFalseTotal
$Markdown::Pod::Embed::PANDOC_EXE or return err("pandoc is required for markdown to text conversion") 0 0 0
538 return err('pandoc is required for markdown to text conversion');
539  
540 # Run the Pandoc conversion to markup
541 #
542 0 my $text;
543 0 { my $command_ar=
544 $PANDOC_CMD_MD2TEXT_CR->($PANDOC_EXE, '-');
545  
546 #die Dumper($command_ar, \$md, ($fn || \$text), \undef);
547 0 ▶ IPC::Run3::run3($command_ar, \$md, ($fn || \$text), \undef) ||
Branch: 1 branch
BranchTrueFalseTotal
IPC::Run3::run3($command_ar, \$md, $fn || \$text, \(undef)) or return err("unable to run3 %s", Dumper($command_ar)) 0 0 0
Condition0%0/2
$fn || \$text
l!l
00
MC/DC0%0/1
$fn || \$text
$fn
Truth table0%0/2
A
A= $fn
Aresult
11
00
548 return err('unable to run3 %s', Dumper($command_ar));
549 0 ▶ if ((my $err=$?) >> 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $err = $?) >> 8) 0 0 0
550 0 return err("error $err on run3 of: %s", Dumper($command_ar));
551 }
552 }
553  
554 # Done
555 #
556 0 ▶ return $text || '';
Condition0%0/2
$text || ''
l!l
00
MC/DC0%0/1
$text || ''
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
557  
558 }
559  
560  
561 0 ▶sub outfile {
Subroutine outfile: not called CC=2 SCAR=17.9
Pod undocumented
562  
563  
564 # Save output to a file or send to STDOUT
565 #
566 0 my ($self, $output, $fn)=@_;
567  
568  
569 # Send to STDOUT or selected output file
570 #
571 0 ▶ return blurp($fn, $output) if $fn;
Branch: 1 branch
BranchTrueFalseTotal
if $fn 0 0 0
572 0 print STDOUT $output;
573 0 return 1;
574  
575  
576 }
577  
578  
579  
580 # Getters
581 #
582 1 ▶sub markdown { $_[0]->{'markdown'} }
Subroutine markdown: called CC=1 SCAR=0.0
Pod undocumented
583 2 ▶sub pod { $_[0]->{'pod'} }
Subroutine pod: called CC=1 SCAR=0.0
Pod undocumented
584 15 ▶sub ppi_doc_or { $_[0]->{'ppi_doc_or'} }
Subroutine ppi_doc_or: called CC=1 SCAR=0.0
Pod undocumented
585  
586 0 ▶sub source { return shift()->markpod_markdown_source(@_) }
Subroutine source: not called CC=1 SCAR=6.9
Pod undocumented
587 2 ▶sub process { return shift()->markpod_process(@_) }
Subroutine process: called CC=1 SCAR=0.0
Pod undocumented
588 6 ▶sub update { return shift()->markpod_process_and_update(@_) }
Subroutine update: called CC=1 SCAR=0.0
Pod undocumented
589  
590 2 ▶sub discover {
Subroutine discover: called CC=4 SCAR=14.1
Pod undocumented
591 2 my ($class, @input)=@_;
592 2 my %found;
593 2 foreach my $input (@input) {
594 2 ▶ if (-f $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $input) 1 1 2
595 1 $found{$input}=1;
596 1 next;
597 }
598 1 ▶ die "input does not exist: $input\n" unless -d $input;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $input 0 1 1
599 5 ▶ File::Find::find({no_chdir => 1, wanted => sub {
Subroutine __ANON__: called CC=10 SCAR=24.1
600 5 my $fn=$File::Find::name;
601 5 ▶ if (-d $fn && $fn=~m{(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $fn and $fn =~ m[(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$]) 2 3 5
Condition100%3/3
-d $fn and $fn =~ m[(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$]
!ll&&!rl&&r
212
MC/DC100%2/2
-d $fn and $fn =~ m[(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$]
-d $fn $fn =~ m[(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$]
Truth table100%3/3
A and B
A= -d $fnB= $fn =~ m[(?:^|/)(?:\.git|\.venv|node_modules|blib|build|site|t)$]
ABresult
0X0
100
111
602 2 $File::Find::prune=1;
603 2 return;
604 }
605 3 ▶ return unless -f $fn && !-l $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fn and not -l $fn 1 2 3
Condition66%2/3
-f $fn and not -l $fn
!ll&&!rl&&r
102
MC/DC50%1/2
-f $fn and not -l $fn
-f $fn not -l $fn
Truth table66%2/3
A and not(B)
A= -f $fnB= not -l $fn
ABresult
0X0
100
111
606 2 ▶ if ($fn=~/\.(?:pm|pl)$/) { $found{$fn}=1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /\.(?:pm|pl)$/) { } 1 1 2
elsif ($fn =~ /\.md$/) { } 1 0 1
607 elsif ($fn=~/\.md$/) {
608 1 (my $target_fn=$fn)=~s/\.md$//;
609 1 ▶ $found{$target_fn}=1 if -f $target_fn && -x $target_fn && !-l $target_fn;
Branch: 1 branch
BranchTrueFalseTotal
if -f $target_fn and -x $target_fn and not -l $target_fn 1 0 1
Condition33%2/6
-f $target_fn and -x $target_fn and not -l $target_fn
!ll&&!rl&&r
001
-f $target_fn and -x $target_fn
!ll&&!rl&&r
001
MC/DC0%0/3
-f $target_fn and -x $target_fn and not -l $target_fn
-f $target_fn -x $target_fn not -l $target_fn
Truth table25%1/4
A and B and not(C)
A= -f $target_fnB= -x $target_fnC= not -l $target_fn
ABCresult
0XX0
10X0
1100
1111
610 }
611 1 }}, $input);
612 }
613 2 return [sort keys %found];
614 }
615  
616  
617 8 1;
618 __END__
↑ Summary blib/lib/Markdown/Pod/Embed/Util.pm »