blib/lib/Pandoc/Filter/ImagesFromCode.pm

filter: Statementstmt 94.4% 102 / 108 Branchbran 46.4% 13 / 28 Conditioncond 39.2% 11 / 28 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 100% 20 / 20 Podpod 50.0% 3 / 6 totaltotal 72.6% 149 / 205 SCAR 35.4
CC
26
Cov
77%
__ANON__
13.5
new
9.5
build_image
5.3
CRAP 34.4
« blib/lib/Pandoc/Metadata.pm ↑ Summary blib/lib/Pandoc/Walker.pm »
1 package Pandoc::Filter::ImagesFromCode;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.36';
9  
10 1 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Pandoc::Elements;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use parent 'Pandoc::Filter', 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our @EXPORT_OK = qw(read_file write_file);
19  
20 sub new {
21 1 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=7 SCAR=22.5
Pod documented
22  
23 1 ▶ $opts{from} //= 'code';
Condition50%1/2
$opts{"from"} //= "code"
l!l
10
MC/DC0%0/1
$opts{"from"} //= "code"
$opts{"from"}
Truth table50%1/2
A
A= $opts{"from"}
Aresult
11
00
24 1 ▶ $opts{dir} //= '.';
Condition50%1/2
$opts{"dir"} //= "."
l!l
10
MC/DC0%0/1
$opts{"dir"} //= "."
$opts{"dir"}
Truth table50%1/2
A
A= $opts{"dir"}
Aresult
11
00
25 1 $opts{dir} =~ s!/$!!;
26 $opts{name} //= sub {
27 1 ▶ $_[0]->id =~ /^[a-z0-9_]+$/i ? $_[0]->id
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->id =~ /^[a-z0-9_]+$/i ? : 1 0 1
28 : md5_hex( encode( 'utf8', $_[0]->content ) );
29 1 ▶ };
Condition33%1/3
$opts{"name"} //= sub { $_[0]->id =~ /^[a-z0-9_]+$/i ? $_[0]->id : md5_hex(&encode("utf8", scalar $_[0]->content)); }
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{"name"} //= sub { $_[0]->id =~ /^[a-z0-9_]+$/i ? $_[0]->id : md5_hex(&encode("utf8", scalar $_[0]->content)); }
$opts{"name"} sub { $_[0]->id =~ /^[a-z0-9_]+$/i ? $_[0]->id : md5_hex(&encode("utf8", scalar $_[0]->content)); }
Truth table33%1/3
A //= B
A= $opts{"name"}B= sub { $_[0]->id =~ /^[a-z0-9_]+$/i ? $_[0]->id : md5_hex(&encode("utf8", scalar $_[0]->content)); }
ABresult
1X1
011
000
30  
31 1 ▶ die "missing option: to\n" unless $opts{to};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"to"} 0 1 1
32  
33 1 ▶ if ('ARRAY' ne reftype $opts{run} or !@{$opts{run}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" ne builtin::reftype($opts{"run"}) or not @{$opts{"run"};}) 0 1 1
Condition33%1/3
"ARRAY" ne builtin::reftype($opts{"run"}) or not @{$opts{"run"};}
l!l&&r!l&&!r
001
MC/DC0%0/2
"ARRAY" ne builtin::reftype($opts{"run"}) or not @{$opts{"run"};}
"ARRAY" ne builtin::reftype($opts{"run"}) not @{$opts{"run"};}
Truth table33%1/3
A or not(B)
A= "ARRAY" ne builtin::reftype($opts{"run"})B= not @{$opts{"run"};}
ABresult
1X1
011
000
34 0 die "missing or empty option: run\n";
35 }
36  
37 1 bless \%opts, $class;
38 }
39  
40 sub to {
41 1 ▶ my $to = $_[0]->{to};
Subroutine to: called CC=3 SCAR=12.7
Pod documented
42 1 my $format = $_[1];
43 1 ▶ if (ref $to) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $to) { } 0 1 1
elsif ($to) { } 1 0 1
44 0 return $to->($format);
45 } elsif ($to) {
46 1 return $to;
47 } else {
48 0 return 'png';
49 }
50 }
51  
52 sub action {
53 1 ▶ my $self = shift;
Subroutine action: called CC=1 SCAR=0.3
Pod documented
54  
55 sub {
56 1 ▶ my ($e, $format, $m) = @_;
Subroutine __ANON__: called CC=10 SCAR=26.0
57  
58 1 ▶ return if $e->name ne 'CodeBlock';
Branch: 1 branch
BranchTrueFalseTotal
if $e->name ne "CodeBlock" 0 1 1
59  
60 1 my $code = $e->content;
61 1 my $dir = $self->{dir};
62  
63 my %args = (
64 name => $self->{name}->($e),
65 from => $self->{from},
66 1 to => $self->to($format),
67 );
68 1 $args{infile} = catfile($self->{dir}, "$args{name}.$args{from}");
69 1 $args{outfile} = catfile($self->{dir}, "$args{name}.$args{to}");
70  
71 # TODO: document or remove this experimental code. If keep, expand args
72 1 my $kv = $e->keyvals;
73 1 my @options = $kv->get_all('option');
74 1 push @options, map { split /\s+/, $_ } $kv->get_all('options');
75  
76 # TODO: print args in debug mode?
77  
78 # skip transformation if nothing has changed
79 1 my $in = stat($args{infile});
80 1 my $out = stat($args{outfile});
81 1 ▶ if (!$self->{force} and $in and $out and $in->mtime <= $out->mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"force"} and $in and $out and $in->mtime <= $out->mtime) 0 1 1
Condition33%3/9
not $self->{"force"} and $in and $out and $in->mtime <= $out->mtime
!ll&&!rl&&r
100
not $self->{"force"} and $in and $out
!ll&&!rl&&r
100
not $self->{"force"} and $in
!ll&&!rl&&r
010
MC/DC0%0/4
not $self->{"force"} and $in and $out and $in->mtime <= $out->mtime
not $self->{"force"} $in $out $in->mtime <= $out->mtime
Truth table20%1/5
not(A) and B and C and D
A= not $self->{"force"}B= $inC= $outD= $in->mtime <= $out->mtime
ABCDresult
0XXX0
10XX0
110X0
11100
11111
82 0 ▶ if ($code eq read_file($args{infile}, ':utf8')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq read_file($args{"infile"}, ":utf8")) 0 0 0
83 # no need to rebuild the same outfile
84 0 return build_image($e, $args{outfile});
85 }
86 }
87  
88 1 write_file($args{infile}, $code, ':utf8');
89  
90 1 my ($stderr, $stdout);
91 my @command = map {
92 2 my $s = $_;
93 #if ($args{substr $s, 1, -1})
94 2 ▶ $s =~ s|\$([^\$]+)\$| $args{$1} // $1 |eg;
Condition33%1/3
$args{$1} // $1
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{$1} // $1
$args{$1} $1
Truth table33%1/3
A // B
A= $args{$1}B= $1
ABresult
1X1
011
000
95 2 $s
96 1 } @{$self->{run}};
97 1 push @command, @options;
98  
99 1 run3 \@command, \undef, \$stdout, \$stderr,
100 {
101 binmode_stdin => ':utf8',
102 binmode_stdout => ':raw',
103 binmode_stderr => ':raw',
104 };
105  
106 1 ▶ if ($self->{capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"capture"}) 1 0 1
107 1 write_file($args{outfile}, $stdout, ':raw');
108 }
109  
110 # TODO: include $code or $stderr on error in debug mode
111 # TODO: skip error if requested
112 1 ▶ die $stderr if $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 0 1 1
113  
114 1 return build_image($e, $args{outfile});
115 }
116 1 }
117  
118 # build_image( $element [, $filename ] )
119 #
120 # Maps an element to an L<Image|Pandoc::Elements/Image> element with attributes
121 # from the given element. The attribute C<caption>, if available, is transformed
122 # into image caption. This utility function is useful for filters that transform
123 # content to images. See graphviz, tikz, lilypond and similar filters in the
124 # L<examples|https://metacpan.org/pod/distribution/Pandoc-Elements/examples/>.
125  
126 sub build_image {
127 1 ▶ my $e = shift;
Subroutine build_image: called CC=5 SCAR=16.6
Pod undocumented
128 1 ▶ my $filename = shift // '';
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
129  
130 1 my $keyvals = $e->keyvals;
131 1 ▶ my $title = $keyvals->get('title') // '';
Condition50%1/2
$keyvals->get("title") // ""
l!l
10
MC/DC0%0/1
$keyvals->get("title") // ""
$keyvals->get("title")
Truth table50%1/2
A
A= $keyvals->get("title")
Aresult
11
00
132 1 my $img = Image attributes { id => $e->id, class => $e->class },
133 [], [$filename, $title];
134  
135 1 ▶ my $caption = $keyvals->get('caption') // '';
Condition50%1/2
$keyvals->get("caption") // ""
l!l
10
MC/DC0%0/1
$keyvals->get("caption") // ""
$keyvals->get("caption")
Truth table50%1/2
A
A= $keyvals->get("caption")
Aresult
11
00
136 1 ▶ if (defined $caption) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $caption) 1 0 1
137 1 push @{$img->content}, Str($caption);
138 }
139  
140 1 return Plain [ $img ];
141 }
142  
143 sub write_file {
144 2 ▶ my ($file, $content, $encoding) = @_;
Subroutine write_file: called CC=2 SCAR=7.0
Pod undocumented
145  
146 2 ▶ open my $fh, ">$encoding", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$encoding", $file or die "failed to create file ${file}: $!\n" 0 2 2
147 or die "failed to create file $file: $!\n";
148 2 print $fh $content;
149 2 close $fh;
150 }
151  
152 sub read_file {
153 2 ▶ my ($file, $encoding) = @_;
Subroutine read_file: called CC=3 SCAR=11.2
Pod undocumented
154  
155 2 ▶ open my $fh, "<$encoding", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<$encoding", $file or die "failed to open file: ${file}: $!\n" 0 2 2
156 or die "failed to open file: $file: $!\n";
157  
158 2 my $content = do { local $/; <$fh> };
159 2 ▶ close $fh or die "failed to close file: $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "failed to close file: ${file}: $!\n" 0 2 2
160  
161 2 return $content;
162 }
163  
164 1 1;
165  
166 __END__
« blib/lib/Pandoc/Metadata.pm ↑ Summary blib/lib/Pandoc/Walker.pm »