blib/lib/Pandoc/Walker.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 95.8% 23 / 24 Conditioncond 68.4% 13 / 19 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 91.0% 142 / 156 CC 24 SCAR 18.8
CC
24
Cov
94%
transform
12.0
_simple_action
9.2
action
4.0
CRAP 6.5
↑ Summary blib/lib/Pandoc/Elements.pm »
1 package Pandoc::Walker;
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.17';
7  
8 15 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 our @EXPORT = qw(walk query transform);
13 15 our @EXPORT_OK = ( @EXPORT, 'action' );
14  
15 sub _simple_action {
16 4783 ▶ my $action = shift // return sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine _simple_action: called CC=8 SCAR=22.2
Condition100%2/2
shift() // (return sub { } )
l!l
47821
MC/DC100%1/1
shift() // (return sub { } )
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
17  
18 4782 ▶ if ( blessed $action and $action->isa('Pandoc::Filter') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($action) and $action->isa('Pandoc::Filter')) { } 1 4781 4782
elsif (not ref $action or ref $action ne 'CODE') { } 0 4781 4781
Condition66%2/3
builtin::blessed($action) and $action->isa("Pandoc::Filter")
!ll&&!rl&&r
478101
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 1 $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
004781
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 4782 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2384 2398 4782
26 2384 my @args = @_;
27 2384 ▶ return sub { $_ = $_[0]; $action->( $_[0], @args ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 }
29 else {
30 2398 return $action;
31 }
32 }
33  
34 sub action {
35 4767 ▶ my @actions;
Subroutine action: called CC=4 SCAR=13.9
Pod documented
36 my @args;
37  
38 # $selector => $action [, @arguments ]
39 4767 ▶ if ( !ref $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $_[0]) { } 10 4757 4767
elsif (ref $_[0] eq 'HASH') { } 16 4741 4757
40 10 @actions = ( shift, shift );
41 10 @args = @_;
42 }
43  
44 # { $selector => $code, ... } [, @arguments ]
45 elsif ( ref $_[0] eq 'HASH' ) {
46 16 @actions = %{ shift @_ };
47 16 @args = @_;
48  
49 # code [, @arguments ]
50 }
51 else {
52 4741 return _simple_action(@_);
53 }
54  
55 26 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 26 for 0 .. $n;
60  
61 # TODO: compile selectors for performance
62  
63 sub {
64 877 ▶ my $element = $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.0
65  
66 # get all matching actions
67 my @matching =
68 53 map { $actions[ $_ * 2 + 1 ] }
69 877 grep { $element->match( $actions[ $_ * 2 ] ) } 0 .. $n;
70  
71 877 my @return = ();
72  
73 877 foreach my $action (@matching) {
74 53 $_ = $_[0];
75 53 @return = ( $action->(@_) );
76 }
77  
78 877 ▶ wantarray ? @return : $return[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 869 8 877
79 }
80 26 }
81  
82 sub transform {
83 4715 ▶ my $ast = shift;
Subroutine transform: called CC=12 SCAR=24.9
Pod documented
84 4715 my $action = action(@_);
85  
86 4715 ▶ my $reftype = reftype($ast) || '';
Condition100%2/2
builtin::reftype($ast) || ''
l!l
26162099
MC/DC100%1/1
builtin::reftype($ast) || ''
builtin::reftype($ast)
Truth table100%2/2
A
A= builtin::reftype($ast)
Aresult
11
00
87  
88 4715 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 1380 3335 4715
elsif ($reftype eq 'HASH') { } 1236 2099 3335
89 1380 ▶ for ( my $i = 0 ; $i < @$ast ; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ast) 2305 1380 3685
90 2305 my $item = $ast->[$i];
91 2305 ▶ if ( ( reftype $item || '' ) eq 'HASH' and $item->{t} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($item) || "") eq "HASH" and $item->{"t"}) 1147 1158 2305
Condition100%5/5
(builtin::reftype($item) || "") eq "HASH" and $item->{"t"}
!ll&&!rl&&r
1123351147
builtin::reftype($item) || ''
l!l
1909396
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
92 1147 my $res = $action->($item);
93  
94 # replace current item with result element(s)
95 1147 ▶ if ( defined $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) 17 1130 1147
96 17 ▶ my @elements = #map { transform($_, $action, @_) }
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($res) || '') eq 'ARRAY' ? : 10 7 17
Condition50%1/2
builtin::reftype($res) || ''
l!l
170
MC/DC0%0/1
builtin::reftype($res) || ''
builtin::reftype($res)
Truth table50%1/2
A
A= builtin::reftype($res)
Aresult
11
00
97 ( reftype $res || '' ) eq 'ARRAY' ? @$res : $res;
98 17 splice @$ast, $i, 1, @elements;
99 17 $i += scalar @elements;
100 17 next;
101 }
102 }
103 2288 transform( $item, $action );
104 2288 $i++;
105 }
106 }
107 elsif ( $reftype eq 'HASH' ) {
108  
109 # TODO: directly transform an element.
110 # if (blessed $ast and $ast->isa('Pandoc::Elements::Element')) {
111 # } else {
112 1236 foreach ( keys %$ast ) {
113 2375 transform( $ast->{$_}, $action, @_ );
114 }
115  
116 # }
117 }
118  
119 4715 $ast;
120 }
121  
122 sub walk(@) { ## no critic
123 14 ▶ my $ast = shift;
Subroutine walk: called CC=1 SCAR=0.0
Pod documented
124 14 my $action = action(@_);
125  
126 14 ▶ transform( $ast, sub { $_ = $_[0]; $action->(@_); return } );
Subroutine __ANON__: called CC=1 SCAR=0.0
127 }
128  
129 sub query(@) { ## no critic
130 24 ▶ my $ast = shift;
Subroutine query: called CC=1 SCAR=0.0
Pod documented
131 24 my $action = action(@_);
132  
133 24 my $list = [];
134 24 ▶ transform( $ast, sub { $_ = $_[0]; push @$list, $action->(@_); return } );
Subroutine __ANON__: called CC=1 SCAR=0.0
135 24 return $list;
136 }
137  
138 15 1;
139 __END__
↑ Summary blib/lib/Pandoc/Elements.pm »