summaryrefslogtreecommitdiff
path: root/pandoc-memoir
blob: 6954877d0c6cdf4fd2cc9966ccd8c31463bc8686 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use feature qw(switch);
  5. no if $] >= 5.018, warnings => "experimental::smartmatch";
  6. use Pandoc::Filter 0.06;
  7. use Pandoc::Elements;
  8. my ( $frontmatter_seen, $mainmatter_seen, $backmatter_seen, $toc_seen);
  9. pandoc_filter(
  10. \&matter,
  11. \&toc,
  12. );
  13. sub matter {
  14. my $self = shift;
  15. return unless ( $self->name eq 'Header' );
  16. return unless ( $self->level == 1 );
  17. my (@replace, @prepend);
  18. # push @prepend, RawBlock( 'latex', '\\frontmatter' )
  19. # unless ($toc_seen++);
  20. given (stringify($self)) {
  21. when ( /^Table/ and not $frontmatter_seen++ ) {
  22. push @replace, RawBlock( 'latex',
  23. '{\\hypersetup{linkcolor=black}\\setcounter{tocdepth}{3}\\cleartorecto\\tableofcontents}'
  24. );
  25. };
  26. when ( /^Scope/ and not $mainmatter_seen++ ) {
  27. push @prepend, RawBlock( 'latex', '\\mainmatter' );
  28. };
  29. when ( /^Notes/ and not $backmatter_seen++ ) {
  30. push @prepend, RawBlock( 'latex', '\\backmatter' );
  31. push @replace, Header( 1, attributes {},
  32. [ Str 'References' ] );
  33. };
  34. default { return };
  35. }
  36. return unless ( @replace or @prepend );
  37. return [ @prepend, $self ] unless (@replace);
  38. return [ @prepend, @replace ];
  39. }
  40. sub toc {
  41. my $self = shift;
  42. return []
  43. if ( $self->name eq 'Para' and stringify($self) =~ /__TOC__/ );
  44. return;
  45. }