blib/lib/Pandoc/Walker.pm

filter: Statementstmt 98.8% 87 / 88 Branchbran 90.0% 27 / 30 Conditioncond 67.8% 19 / 28 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 94.1% 16 / 17 Podpod 100% 4 / 4 totaltotal 86.8% 159 / 183 SCAR 34.2
CC
30
Cov
91%
transform
13.0
_simple_action
9.9
action
5.0
CRAP 30.6
« blib/lib/Pandoc/Filter/ImagesFromCode.pm ↑ Summary blib/lib/Pandoc/Selector.pm »
1 package Pandoc::Walker;
2 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 31 our $VERSION = '0.34';
7  
8 31 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 31 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 31 our @EXPORT = qw(walk query transform);
13 31 our @EXPORT_OK = ( @EXPORT, 'action' );
14  
15 sub _simple_action {
16 4052 ▶ my $action = shift // return sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine _simple_action: called CC=8 SCAR=22.9
Condition50%1/2
shift() // (return sub { } )
l!l
40520
MC/DC0%0/1
shift() // (return sub { } )
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
17  
18 4052 ▶ if ( blessed $action and $action->isa('Pandoc::Filter') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($action) and $action->isa('Pandoc::Filter')) { } 2 4050 4052
elsif (not ref $action or ref $action ne 'CODE') { } 0 4050 4050
Condition66%2/3
builtin::blessed($action) and $action->isa("Pandoc::Filter")
!ll&&!rl&&r
405002
MC/DC50%1/2
builtin::blessed($action) and $action->isa("Pandoc::Filter")
builtin::blessed($action) $action->isa("Pandoc::Filter")
Truth table66%2/3
A and B
A= builtin::blessed($action)B= $action->isa("Pandoc::Filter")
ABresult
0X0
100
111
19 2 $action = $action->action;
20 }
21 ▶ elsif ( !ref $action or ref $action ne 'CODE' ) {
Condition33%1/3
not ref $action or ref $action ne "CODE"
l!l&&r!l&&!r
004050
MC/DC0%0/2
not ref $action or ref $action ne "CODE"
not ref $action ref $action ne "CODE"
Truth table33%1/3
not(A) or B
A= not ref $actionB= ref $action ne "CODE"
ABresult
1X1
011
000
22 0 ▶ croak "expected code reference, got: " . ( $action // 'undef' );
Condition0%0/2
$action // "undef"
l!l
00
MC/DC0%0/1
$action // "undef"
$action
Truth table0%0/2
A
A= $action
Aresult
11
00
23 }
24  
25 4052 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2098 1954 4052
26 2098 my @args = @_;
27 2098 ▶ return sub { local $_ = $_[0]; $action->( $_[0], @args ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 }
29 else {
30 1954 return $action;
31 }
32 }
33  
34 sub action {
35 4034 ▶ my @actions;
Subroutine action: called CC=5 SCAR=16.1
Pod documented
36 my @args;
37  
38 # $selector => $action [, @arguments ]
39 4034 ▶ if ( !ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[0]) { } 6 4028 4034
elsif (ref $_[0] eq 'HASH') { } 22 4006 4028
40 6 ▶ @actions = ( shift, shift // sub { $_ } );
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() // sub { $_; }
l!l&&r!l&&!r
420
MC/DC0%0/2
shift() // sub { $_; }
shift() sub { $_; }
Truth table66%2/3
A // B
A= shift()B= sub { $_; }
ABresult
1X1
011
000
41 6 @args = @_;
42 }
43  
44 # { $selector => $code, ... } [, @arguments ]
45 elsif ( ref $_[0] eq 'HASH' ) {
46 22 @actions = %{ shift @_ };
47 22 @args = @_;
48  
49 # code [, @arguments ]
50 }
51 else {
52 4006 return _simple_action(@_);
53 }
54  
55 28 my $n = ( scalar @actions ) / 2 - 1;
56  
57 # check action functions and add arguments
58 $actions[ $_ * 2 + 1 ] = _simple_action( $actions[ $_ * 2 + 1 ], @args )
59 28 for 0 .. $n;
60  
61 # TODO: compile selectors for performance
62  
63 sub {
64 106 ▶ my $element = $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.0
65  
66 # get all matching actions
67 my @matching =
68 71 map { $actions[ $_ * 2 + 1 ] }
69 106 grep { $element->match( $actions[ $_ * 2 ] ) } 0 .. $n;
70  
71 106 my @return = ();
72  
73 106 foreach my $action (@matching) {
74 71 local $_ = $_[0]; # FIXME: $doc->walk( Section => sub { $_->id } )
75 71 @return = ( $action->(@_) );
76 }
77  
78 106 ▶ wantarray ? @return : $return[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 106 106
79 }
80 28 }
81  
82 sub transform {
83 3938 ▶ my $ast = shift;
Subroutine transform: called CC=13 SCAR=25.7
Pod documented
84 3938 my $action = action(@_);
85  
86 3938 ▶ my $reftype = reftype($ast) || '';
Condition100%2/2
builtin::reftype($ast) || ''
l!l
21481790
MC/DC100%1/1
builtin::reftype($ast) || ''
builtin::reftype($ast)
Truth table100%2/2
A
A= builtin::reftype($ast)
Aresult
11
00
87  
88 3938 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 1090 2848 3938
elsif ($reftype eq 'HASH') { } 1052 1796 2848
89 1090 ▶ for ( my $i = 0 ; $i < @$ast ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ast) 1769 1090 2859
90 1769 my $item = $ast->[$i];
91  
92 1769 ▶ if ( ( reftype $item || '' ) eq 'HASH' and $item->{t} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($item) || "") eq "HASH" and $item->{"t"}) 839 930 1769
Condition100%5/5
(builtin::reftype($item) || "") eq "HASH" and $item->{"t"}
!ll&&!rl&&r
9291839
builtin::reftype($item) || ''
l!l
1349420
MC/DC100%2/2
(builtin::reftype($item) || "") eq "HASH" and $item->{"t"}
builtin::reftype($item) $item->{"t"}
Truth table100%3/3
A and B
A= builtin::reftype($item)B= $item->{"t"}
ABresult
0X0
100
111
93 839 my $res = $action->($item);
94  
95 839 ▶ if ( defined $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) 21 818 839
96 # stop traversal
97 21 ▶ if ( $res eq \undef ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res eq \(undef)) { } 1 20 21
98 1 $i++;
99 # replace current item with result element(s)
100 } else {
101 20 ▶ my @elements = #map { transform($_, $action, @_) }
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($res) || '') eq 'ARRAY' ? : 8 12 20
Condition50%1/2
builtin::reftype($res) || ''
l!l
200
MC/DC0%0/1
builtin::reftype($res) || ''
builtin::reftype($res)
Truth table50%1/2
A
A= builtin::reftype($res)
Aresult
11
00
102 ( reftype $res || '' ) eq 'ARRAY' ? @$res : $res;
103 20 splice @$ast, $i, 1, @elements;
104 20 $i += scalar @elements;
105 }
106 21 next;
107 }
108 }
109 1748 transform( $item, $action );
110 1748 $i++;
111 }
112 }
113 elsif ( $reftype eq 'HASH' ) {
114  
115 # TODO: directly transform an element.
116 # if (blessed $ast and $ast->isa('Pandoc::Elements::Element')) {
117 # } else {
118 1052 foreach ( keys %$ast ) {
119 2088 transform( $ast->{$_}, $action, @_ );
120 }
121  
122 # }
123 }
124  
125 3938 $ast;
126 }
127  
128 sub walk(@) { ## no critic
129 54 ▶ my $ast = shift;
Subroutine walk: called CC=1 SCAR=0.1
Pod documented
130 54 my $action = action(@_);
131 transform( $ast, sub {
132 604 ▶ local $_ = $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.4
133 604 my $q = $action->(@_);
134 604 ▶ return (defined $q and $q eq \undef) ? \undef : undef
Branch: 1 branch
BranchTrueFalseTotal
defined $q && $q eq \(undef) ? : 0 604 604
Condition66%2/3
defined $q and $q eq \(undef)
!ll&&!rl&&r
475570
MC/DC0%0/2
defined $q and $q eq \(undef)
defined $q $q eq \(undef)
Truth table66%2/3
A and B
A= defined $qB= $q eq \(undef)
ABresult
0X0
100
111
135 54 } );
136 }
137  
138 sub query(@) { ## no critic
139 32 ▶ my $ast = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod documented
140 32 my $action = action(@_);
141  
142 32 my $list = [];
143 transform( $ast, sub {
144 166 ▶ local $_ = $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.0
145 166 my $q = $action->(@_);
146 166 ▶ return $q if !defined $q or $q eq \undef;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $q or $q eq \(undef) 95 71 166
Condition100%3/3
not defined $q or $q eq \(undef)
l!l&&r!l&&!r
94171
MC/DC100%2/2
not defined $q or $q eq \(undef)
not defined $q $q eq \(undef)
Truth table100%3/3
not(A) or B
A= not defined $qB= $q eq \(undef)
ABresult
1X1
011
000
147 71 push @$list, $q;
148 return
149 32 } );
150 32 return $list;
151 }
152  
153 31 1;
154 __END__
« blib/lib/Pandoc/Filter/ImagesFromCode.pm ↑ Summary blib/lib/Pandoc/Selector.pm »