summaryrefslogtreecommitdiff
path: root/pandoc-todo
blob: 784b8fac42e3136bc7afefac6aa667b07cf65220 (plain)
  1. #!/usr/bin/perl
  2. # TODO: support multi-match within word
  3. # TODO: support multi-word match
  4. # TODO: always count and allow expressing which (default: all)
  5. # TODO: parse regex+commeent as definition in external markdown TODO file.
  6. # TODO: normal hyphenation within note (keep quote with word in "word")
  7. use warnings;
  8. use strict;
  9. use feature qw(switch);
  10. no if $] >= 5.018, warnings => "experimental::smartmatch";
  11. use Pandoc::Filter 0.05;
  12. use Pandoc::Elements;
  13. my $breakable_chars = qr/[\&\;\/\.\?\!\=]/;
  14. my $hyphen_chars = qr/[\x{2010}-\x{2015}\x{2053}\x{2212}]/;
  15. my $doublequote_chars = qr/[\x{00AB}\x{00BB}\x{201C}-\x{201F}\x{300C}-\x{300F}\x{301D}-\x{301F}\x{FE41}-\x{FE44}\x{FF02}\x{FF62}\x{FF63}]/;
  16. my $singlequote_chars = qr/[\x{0060}\x{2018}\x{2019}\x{201A}\x{201B}\x{2039}\x{203A}\x{FF07}]/;
  17. my $odd_singlequote_chars = qr/[\x{2019}]/;
  18. pandoc_filter(
  19. \&todo,
  20. );
  21. sub latex_encode {
  22. my $s = shift;
  23. $s =~ s/(?<!\s|$breakable_chars)($breakable_chars])(?!\s)/$1\\-/g;
  24. $s =~ s/$odd_singlequote_chars/'/g; #'
  25. return $s;
  26. }
  27. sub mark_inside {
  28. my ( $before, $it, $after, $note ) = @_;
  29. return [ RawInline( 'latex', sprintf( '%s\\todo{%s}\\hl{%s}%s',
  30. latex_encode($before),
  31. latex_encode($note),
  32. latex_encode($it),
  33. latex_encode($after),
  34. ))];
  35. };
  36. sub todo {
  37. my $self = shift;
  38. return unless ( $self->name eq 'Str' );
  39. given ($self->content) {
  40. when (/^(-)$/) {
  41. return mark_inside( '', $1, '', 'double dash?' ) };
  42. when ( /^(.*?)($hyphen_chars(?:.*$hyphen_chars)?)(.*?)$/ ) {
  43. return mark_inside( $1, $2, $3, 'bad dash/hyphen' ) };
  44. when ( /^(.*?)($doublequote_chars(?:.*$doublequote_chars)?)(.*?)$/ ) {
  45. return mark_inside( $1, $2, $3, 'bad quote' ) };
  46. when ( /^(.*?)($singlequote_chars(?:.*$singlequote_chars)?)(.*?)$/ ) {
  47. return mark_inside( $1, $2, $3, 'Bad quote/apostrophe' ) };
  48. default { return };
  49. }
  50. };