blib/lib/Pandoc/Filter/Multifilter.pm

filter: Statementstmt 56.2% 36 / 64 Branchbran 27.2% 6 / 22 Conditioncond 22.7% 5 / 22 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 75.0% 9 / 12 Podpod 100% 4 / 4 totaltotal 44.2% 61 / 138 SCAR 46.9
CC
22
Cov
44%
apply
90.0
apply_filter
42.0
find_filter
9.6
CRAP 109.2
« blib/lib/Pandoc/Document/Element.pm ↑ Summary blib/lib/Pandoc/Metadata.pm »
1 package Pandoc::Filter::Multifilter;
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 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.34';
7  
8 1 ▶use parent 'Pandoc::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 our @EXPORT_OK = (qw(find_filter apply_filter));
10  
11 1 ▶use Pandoc::Elements 'pandoc_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Pandoc;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use IPC::Cmd 'can_run';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 0 ▶ bless { }, shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
18 }
19  
20 sub apply {
21 0 ▶ my ( $self, $doc, $format, $meta ) = @_;
Subroutine apply: not called CC=9 SCAR=45.0
Pod documented
22 0 ▶ return $doc if $doc->name ne 'Document';
Branch: 1 branch
BranchTrueFalseTotal
if $doc->name ne "Document" 0 0 0
23  
24 0 my $multi = $doc->meta->{multifilter};
25 0 ▶ return $doc if !$multi or $multi->name ne 'MetaList';
Branch: 1 branch
BranchTrueFalseTotal
if not $multi or $multi->name ne "MetaList" 0 0 0
Condition0%0/3
not $multi or $multi->name ne "MetaList"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $multi or $multi->name ne "MetaList"
not $multi $multi->name ne "MetaList"
Truth table0%0/3
not(A) or B
A= not $multiB= $multi->name ne "MetaList"
ABresult
1X1
011
000
26  
27 my @filters = map {
28 0 ▶ if ($_->name eq 'MetaMap' and $_->{filter}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_->name eq 'MetaMap' and $_->{'filter'}) { } 0 0 0
elsif ($_->name eq 'MetaString' or $_->name eq 'MetaInlines') { } 0 0 0
Condition0%0/3
$_->name eq "MetaMap" and $_->{"filter"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->name eq "MetaMap" and $_->{"filter"}
$_->name eq "MetaMap" $_->{"filter"}
Truth table0%0/3
A and B
A= $_->name eq "MetaMap"B= $_->{"filter"}
ABresult
0X0
100
111
29 0 $_->value
30 ▶ } elsif ($_->name eq 'MetaString' or $_->name eq 'MetaInlines') {
Condition0%0/3
$_->name eq "MetaString" or $_->name eq "MetaInlines"
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->name eq "MetaString" or $_->name eq "MetaInlines"
$_->name eq "MetaString" $_->name eq "MetaInlines"
Truth table0%0/3
A or B
A= $_->name eq "MetaString"B= $_->name eq "MetaInlines"
ABresult
1X1
011
000
31 0 { filter => $_->value }
32 }
33 0 } @{$multi->content};
34  
35 0 foreach (@filters) {
36 0 my @filter = find_filter($_->{filter});
37 0 apply_filter($doc, $format, @filter);
38 }
39  
40 0 $doc;
41 }
42  
43 1 our %SCRIPTS = (
44 hs => 'runhaskell',
45 js => 'node',
46 php => 'php',
47 pl => 'perl',
48 py => 'python',
49 rb => 'ruby',
50 );
51  
52 sub find_filter {
53 4 ▶ my $name = shift;
Subroutine find_filter: called CC=9 SCAR=22.6
Pod documented
54 4 ▶ my $data_dir = shift // pandoc_data_dir;
Condition33%1/3
shift() // pandoc_data_dir()
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() // pandoc_data_dir()
shift() pandoc_data_dir()
Truth table33%1/3
A // B
A= shift()B= pandoc_data_dir()
ABresult
1X1
011
000
55 4 $data_dir =~ s|/$||;
56  
57 4 foreach my $filter ("$data_dir/filters/$name", $name) {
58 8 ▶ return $filter if -x $filter;
Branch: 1 branch
BranchTrueFalseTotal
if -x $filter 1 7 8
59 7 ▶ if (-e $filter and $filter =~ /\.([a-z]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filter and $filter =~ /\.([a-z]+)$/i) 1 6 7
Condition66%2/3
-e $filter and $filter =~ /\.([a-z]+)$/i
!ll&&!rl&&r
601
MC/DC50%1/2
-e $filter and $filter =~ /\.([a-z]+)$/i
-e $filter $filter =~ /\.([a-z]+)$/i
Truth table66%2/3
A and B
A= -e $filterB= $filter =~ /\.([a-z]+)$/i
ABresult
0X0
100
111
60 1 ▶ if ( my $cmd = $SCRIPTS{lc($1)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = $Pandoc::Filter::Multifilter::SCRIPTS{lc $1}) 1 0 1
61 1 ▶ die "cannot execute filter with $cmd\n" unless can_run($cmd);
Branch: 1 branch
BranchTrueFalseTotal
unless can_run($cmd) 0 1 1
62 1 return ($cmd, $filter);
63 }
64 }
65 }
66  
67 2 ▶ return (can_run($name) or die "filter not found: $name\n");
Condition100%2/2
can_run($name) || die("filter not found: $name\n")
l!l
11
MC/DC0%0/1
can_run($name) || die("filter not found: $name\n")
can_run($name)
Truth table50%1/2
A
A= can_run($name)
Aresult
11
00
68 }
69  
70 sub apply_filter {
71 0 ▶ my ($doc, $format, @filter) = @_;
Subroutine apply_filter: not called CC=6 SCAR=37.4
Pod documented
72  
73 0 my $stdin = $doc->to_json;
74 0 my $stdout = "";
75 0 my $stderr = "";
76  
77 0 ▶ run3 [@filter, $format // ''], \$stdin, \$stdout, \$stderr;
Condition0%0/2
$format // ""
l!l
00
MC/DC0%0/1
$format // ""
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
78 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
79 0 ▶ $stderr .= "\n" if $stderr ne '' and $stderr !~ /\n\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr ne "" and not $stderr =~ /\n\z/s 0 0 0
Condition0%0/3
$stderr ne "" and not $stderr =~ /\n\z/s
!ll&&!rl&&r
000
MC/DC0%0/2
$stderr ne "" and not $stderr =~ /\n\z/s
$stderr ne "" not $stderr =~ /\n\z/s
Truth table0%0/3
A and not(B)
A= $stderr ne ""B= not $stderr =~ /\n\z/s
ABresult
0X0
100
111
80 0 die join(' ','filter failed:',@filter)."\n$stderr";
81 }
82  
83 0 my $transformed = eval { pandoc_json($stdout) };
84 0 ▶ die join(' ','filter emitted no valid JSON:',@filter)."\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
85  
86 # modify original document
87 0 $doc->meta($transformed->meta);
88 0 $doc->content($transformed->content);
89  
90 0 return $doc;
91 }
92  
93 __END__
« blib/lib/Pandoc/Document/Element.pm ↑ Summary blib/lib/Pandoc/Metadata.pm »