1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
use strict;
use 5.010;
use Test::More;
use Pandoc::Filter;
use Pandoc::Elements;
# action function
my $action = sub {
return unless $_[0]->name eq 'Header' and $_[0]->level >= 2;
Para [ Emph $_[0]->content ];
};
my $h1 = Header(1, attributes {}, [ Str 'hello']);
my $h2 = Header(2, attributes {}, [ Str 'hello']);
is $action->($h1), undef, 'action';
is_deeply $action->($h2), Para [ Emph [ Str 'hello' ] ], 'action';
{
my $doc = Document {}, [ $h1, $h2 ];
Pandoc::Filter->new($action)->apply($doc);
is_deeply $doc->content->[1], Para [ Emph [ Str 'hello' ] ], 'apply';
}
{
my $doc = Document { title => MetaInlines [ Str 'test' ] }, [ $h1, $h2 ];
Pandoc::Filter->new(
Header => sub {
Para [ Str $_[1] . ':' . $_[2]->{title}->string ]
}
)->apply($doc, 'html');
is_deeply $doc->content->[1], Para [ Str 'html:test' ], 'format and metadata';
}
# TODO: should croak because 1 is no selector ( 1 => sub { } )
# eval { Pandoc::Filter->new( 1 ) }; ok $@, 'invalid filter';
my $doc = Document {}, [ Str "hello" ];
my $filter = Pandoc::Filter->new(sub {
return if $_->name ne 'Str';
$_->{c} = uc $_->{c};
return [ $_, Str " world!" ];
});
$filter->apply($doc);
is_deeply $doc->content, [ Str('HELLO'), Str(' world!') ], "don't filter injected elements";
done_testing;
|