File: pandoc_filter.t

package info (click to toggle)
libpandoc-elements-perl 0.38-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: perl: 1,630; makefile: 15; sh: 1
file content (43 lines) | stat: -rw-r--r-- 1,160 bytes parent folder | download | duplicates (3)
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
use Test::More;
use Pandoc::Filter;
use Pandoc::Elements;
use JSON;
use Encode;
use Test::Output;

# FIXME: don't require decode_utf8 (?)

my $ast = Pandoc::Elements::pandoc_json(
    '{"blocks":[{"t":"Para","c":[{"t":"Str","c":"☃"}]}]}'
)->blocks->[0]->content->[0];
is_deeply $ast, { t => 'Str', c => decode_utf8("☃") }, 'JSON with Unicode';
Pandoc::Filter->new()->apply($ast);
is_deeply $ast, { t => 'Str', c => decode_utf8("☃") }, 'identity filter';

sub shout {
    return unless $_[0]->name eq 'Str';
    return Str($_[0]->content.'!');
}

# FIXME: cannot directly filter root element
$ast = [$ast];
Pandoc::Filter->new(\&shout)->apply($ast);
is_deeply $ast, [{ t => 'Str', c => "\x{2603}!" }], 'applied filter';

{
    local *STDIN = *DATA;
    my $data_start = tell DATA;
    stdout_like(sub { 
            pandoc_filter( \&shout ) 
        }, qr/"c":"☃!"/, 'pandoc_filter (sub)');

    seek DATA, $data_start, 0;
    stdout_like(sub { 
            pandoc_filter( Str => sub { Str($_[0]->content.'!') } ) 
        }, qr/"c":"☃!"/, 'pandoc_filter (hash)');
}

done_testing;

__DATA__
[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"☃"}]}]]