| 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 {
}
)
MC/DC0%0/1
shift() // (return sub {
}
)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 4052 | ▶ | if ( blessed $action and $action->isa('Pandoc::Filter') ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($action) and $action->isa("Pandoc::Filter")
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")
| |||||||||||||||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | croak "expected code reference, got: " . ( $action // 'undef' ); | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$action // "undef"
MC/DC0%0/1
$action // "undef"
$action
Truth table0%0/2
A
A= $action
| |||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 4052 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | @actions = ( shift, shift // sub { $_ } ); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() // sub {
$_;
}
MC/DC0%0/2
shift() // sub {
$_;
}
shift()
sub {
$_;
}
Truth table66%2/3
A // B
A= shift()B= sub {
$_;
}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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) || ''
MC/DC100%1/1
builtin::reftype($ast) || ''
builtin::reftype($ast)
Truth table100%2/2
A
A= builtin::reftype($ast)
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 3938 | ▶ | if ( $reftype eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 89 | 1090 | ▶ | for ( my $i = 0 ; $i < @$ast ; ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 1769 | my $item = $ast->[$i]; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 1769 | ▶ | if ( ( reftype $item || '' ) eq 'HASH' and $item->{t} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
(builtin::reftype($item) || "") eq "HASH" and $item->{"t"}
builtin::reftype($item) || ''
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"}
| |||||||||||||||||||||||||||||||||
| 93 | 839 | my $res = $action->($item); | |||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | 839 | ▶ | if ( defined $res ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | # stop traversal | ||||||||||||||||||||||||||||||||
| 97 | 21 | ▶ | if ( $res eq \undef ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 98 | 1 | $i++; | |||||||||||||||||||||||||||||||
| 99 | # replace current item with result element(s) | ||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||
| 101 | 20 | ▶ | my @elements = #map { transform($_, $action, @_) } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
builtin::reftype($res) || ''
MC/DC0%0/1
builtin::reftype($res) || ''
builtin::reftype($res)
Truth table50%1/2
A
A= builtin::reftype($res)
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $q and $q eq \(undef)
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)
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $q or $q eq \(undef)
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)
| |||||||||||||||||||||||||||||||||
| 147 | 71 | push @$list, $q; | |||||||||||||||||||||||||||||||
| 148 | return | ||||||||||||||||||||||||||||||||
| 149 | 32 | } ); | |||||||||||||||||||||||||||||||
| 150 | 32 | return $list; | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 31 | 1; | |||||||||||||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||||||||||||||