| 1 | package Pandoc::Filter::Lazy; | ||||||||||||||||||||||||||||
| 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.17'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use parent 'Pandoc::Filter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Pandoc::Elements; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | sub new { | ||||||||||||||||||||||||||||
| 13 | 6 | ▶ | my ( $class, $script ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 6 | $script =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | ▶ | if ( $script =~ /^(.*?)\s*=>\s*(.+)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 5 | my ( $selector, $action ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 18 | 5 | ▶ | if ( $selector =~ /[^a-z]/i && $selector !~ /^["']/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$selector =~ /[^a-z]/i and not $selector =~ /^["']/
MC/DC50%1/2
$selector =~ /[^a-z]/i and not $selector =~ /^["']/
$selector =~ /[^a-z]/i
not $selector =~ /^["']/
Truth table66%2/3
A and not(B)
A= $selector =~ /[^a-z]/iB= not $selector =~ /^["']/
| |||||||||||||||||||||||||||||
| 19 | 3 | $selector = "'$selector'"; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | 5 | ▶ | if ( $action !~ /^sub\s*{/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 2 | $action = "sub { $action }"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 5 | $script = "$selector => $action"; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 6 | my $filter = | |||||||||||||||||||||||||||
| 28 | "use Pandoc::Elements;use Pandoc::Walker;Pandoc::Walker::action($script)"; | ||||||||||||||||||||||||||||
| 29 | 1 | ▶ | $filter = eval $filter; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | my $self = bless { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | script => $script, | ||||||||||||||||||||||||||||
| 32 | action => $filter, | ||||||||||||||||||||||||||||
| 33 | error => $filter ? '' : $@, | ||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub script { | ||||||||||||||||||||||||||||
| 38 | 8 | ▶ | shift->{script}; | ||||||||||||||||||||||||||
|
Subroutine
script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub code { | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $script = shift->script; | ||||||||||||||||||||||||||
|
Subroutine
code: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 2 | my %opt = @_; | |||||||||||||||||||||||||||
| 44 | 2 | ▶ | my $function = $opt{function} || 'Pandoc::Filter->new'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'function'} || 'Pandoc::Filter->new'
MC/DC0%0/1
$opt{'function'} || 'Pandoc::Filter->new'
$opt{'function'}
Truth table50%1/2
A
A= $opt{'function'}
| |||||||||||||||||||||||||||||
| 45 | 2 | my $code = <<CODE; | |||||||||||||||||||||||||||
| 46 | use 5.010; | ||||||||||||||||||||||||||||
| 47 | use strict; | ||||||||||||||||||||||||||||
| 48 | use warnings; | ||||||||||||||||||||||||||||
| 49 | use Pandoc::Filter; | ||||||||||||||||||||||||||||
| 50 | use Pandoc::Elements; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | $function( $script ); | ||||||||||||||||||||||||||||
| 53 | CODE | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | $code = join "\n", map { $opt{indent} . $_ } split "\n", $code | |||||||||||||||||||||||||||
| 56 | 2 | ▶ | if $opt{indent}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | return $code; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 NAME | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Pandoc::Filter::Lazy - facilitate creation of filters | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $filter = Pandoc::Filter::Lazy->new( | ||||||||||||||||||||||||||||
| 68 | 'Header => sub { Header $_->level, [ Str $_->string ] }' | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | if ( $filter->error ) { | ||||||||||||||||||||||||||||
| 71 | say STDERR $lazy->error; | ||||||||||||||||||||||||||||
| 72 | say STDERR $lazy->code; | ||||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | $filter->apply(...) | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | This module helps creation of L<Pandoc::Filter> with arguments given as string. | ||||||||||||||||||||||||||||
| 80 | The following should result in equivalent filters: | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Pandoc::Walker::action( ... ); # ... as code | ||||||||||||||||||||||||||||
| 83 | Pandoc::Filter::Lazy->new( '...' ) # '...' as string | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The script passed as only argument is tried to convert to valid Perl by escaping | ||||||||||||||||||||||||||||
| 86 | selectors and adding a missing C<sub { ... }">, for instance | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Code|CodeBlock => say $_->class | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Is converted to | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 'Code|CodeBlock' => sub { say $_->class } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head1 METHODS | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | In addition to the methods inherited from L<Pandoc::Filter>: | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 error | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Return an error message if compilation of the filter failed. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 script | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Return the (possibly cleaned) script arguments to create the filter. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 code( [ indent => $indent, ] [ function => $function ] ) | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | Return a string of Perl code that can be used to create the same filter. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This module is used in command line scripts L<pandocwalk> and L<pod2pandoc>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||