File: remove-unnumbered-sections.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 (60 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (4)
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
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
use strict;

=head1 NAME

unnumbered-sections - remove all unnumbered sections

=head1 DESCRIPTION

This Pandoc filter removes all unnumbered sections, that is everything from a
header with class C<unnumbered> until the next normal header of the same level.
For instance this document:

   # Section

   # Unnumbered Section {.unnumbered}
   ...

   ## Subsection
   ...

   # Another Section
   ... 
     
Would be reduced to

   # Section
    
   # Another Section
   ... 

=cut

use Pandoc::Filter;

my $skiplevel = 0;

# process all elements
pandoc_filter sub {

    if ($skiplevel > 0) {
        # end of currently skipped section
        if ($_->name eq 'Header' and $_->level <= $skiplevel) {
            $skiplevel = 0;
        # remove element
        } else {
            return []; 
        }
    }

    # new unnumbered section to skip
    if ($_->match('Header.unnumbered')) {
        $skiplevel = $_->level;
        return [];
    }
    
    # keep element
    return
};