File: section.pl

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 (51 lines) | stat: -rw-r--r-- 1,010 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
44
45
46
47
48
49
50
51
#!/usr/bin/env perl
use strict;

=head1 NAME

section - select document sections

=head1 SYNOPSIS

  section --select-id foo --select-class bar --select-name "the title"
  section --select '#foo' --select .bar --select '"the title"'

=cut

use Pandoc::Filter;

=head1 DESCRIPTION

    ---
    multifilter:
      - filter: section
      - options:
        - select: .keep
    ...

=cut

# TODO: get options

my @select = ('.keep'); # TODO: use Pandoc::Element::Selector;
my $level = 0;

# process all elements
pandoc_filter sub {
    my $e = shift;

    if ($level > 0) {
        if ($_->name eq 'Header' and $e->level <= $level) {
            $level = 0;     # end of currently selected section
        } else {
            return;         # keep element in selected section
        }
    }

    if ($e->name eq 'Header' and grep { $e->match($_) } @select ) {
        $level = $e->level; # new selected section
        return;             # keep Header
    } else {
        return [];          # skip
    }
};