#!/usr/bin/perl use warnings; use strict; use feature qw(switch); no if $] >= 5.018, warnings => "experimental::smartmatch"; use Pandoc::Filter 0.06; use Pandoc::Elements; my ( $frontmatter_seen, $mainmatter_seen, $backmatter_seen, $toc_seen); pandoc_filter( \&matter, \&toc, ); sub matter { my $self = shift; return unless ( $self->name eq 'Header' ); return unless ( $self->level == 1 ); my (@replace, @prepend); # push @prepend, RawBlock( 'latex', '\\frontmatter' ) # unless ($toc_seen++); given (stringify($self)) { when ( /^Table/ and not $frontmatter_seen++ ) { push @replace, RawBlock( 'latex', '{\\hypersetup{linkcolor=black}\\setcounter{tocdepth}{3}\\cleartorecto\\tableofcontents}' ); }; when ( /^Scope/ and not $mainmatter_seen++ ) { push @prepend, RawBlock( 'latex', '\\mainmatter' ); }; when ( /^Notes/ and not $backmatter_seen++ ) { push @prepend, RawBlock( 'latex', '\\backmatter' ); push @replace, Header( 1, attributes {}, [ Str 'References' ] ); }; default { return }; } return unless ( @replace or @prepend ); return [ @prepend, $self ] unless (@replace); return [ @prepend, @replace ]; } sub toc { my $self = shift; return [] if ( $self->name eq 'Para' and stringify($self) =~ /__TOC__/ ); return; }