| 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
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 0 | my $multi = $doc->meta->{multifilter}; | |||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | return $doc if !$multi or $multi->name ne 'MetaList'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $multi or $multi->name ne "MetaList"
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"
| |||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | my @filters = map { | ||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | if ($_->name eq 'MetaMap' and $_->{filter}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$_->name eq "MetaMap" and $_->{"filter"}
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"}
| |||||||||||||||||||||||||||||||||
| 29 | 0 | $_->value | |||||||||||||||||||||||||||||||
| 30 | ▶ | } elsif ($_->name eq 'MetaString' or $_->name eq 'MetaInlines') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
$_->name eq "MetaString" or $_->name eq "MetaInlines"
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"
| |||||||||||||||||||||||||||||||||
| 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()
MC/DC0%0/2
shift() // pandoc_data_dir()
shift()
pandoc_data_dir()
Truth table33%1/3
A // B
A= shift()B= pandoc_data_dir()
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 59 | 7 | ▶ | if (-e $filter and $filter =~ /\.([a-z]+)$/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-e $filter and $filter =~ /\.([a-z]+)$/i
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
| |||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | if ( my $cmd = $SCRIPTS{lc($1)} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | die "cannot execute filter with $cmd\n" unless can_run($cmd); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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")
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)
| |||||||||||||||||||||||||||||||||
| 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 // ""
MC/DC0%0/1
$format // ""
$format
Truth table0%0/2
A
A= $format
| |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($?) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $stderr .= "\n" if $stderr ne '' and $stderr !~ /\n\z/s; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$stderr ne "" and not $stderr =~ /\n\z/s
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||