| 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 {
}
)
MC/DC100%1/1
shift() // (return sub {
}
)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 4782 | ▶ | 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 | 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"
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 | 4782 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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) || ''
MC/DC100%1/1
builtin::reftype($ast) || ''
builtin::reftype($ast)
Truth table100%2/2
A
A= builtin::reftype($ast)
| |||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 4715 | ▶ | if ( $reftype eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 89 | 1380 | ▶ | for ( my $i = 0 ; $i < @$ast ; ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | 2305 | my $item = $ast->[$i]; | |||||||||||||||||||||||||||||||
| 91 | 2305 | ▶ | 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"}
| |||||||||||||||||||||||||||||||||
| 92 | 1147 | my $res = $action->($item); | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | # replace current item with result element(s) | ||||||||||||||||||||||||||||||||
| 95 | 1147 | ▶ | if ( defined $res ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 96 | 17 | ▶ | 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)
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||