summaryrefslogtreecommitdiff
path: root/pandoc-todo
blob: 06283a141f293d7d0dda046c0ef51caccaa10e6c (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. # TODO: clear font styling in notes
  8. use warnings;
  9. use strict;
  10. use feature qw(switch);
  11. no if $] >= 5.018, warnings => "experimental::smartmatch";
  12. use Pandoc::Filter 0.05;
  13. use Pandoc::Elements;
  14. my $breakable_chars = qr/[\&\;\/\.\?\!\=]/;
  15. my $hyphen_chars = qr/[\x{2010}-\x{2015}\x{2053}\x{2212}]/;
  16. 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}]/;
  17. my $singlequote_chars = qr/[\x{0060}\x{2018}\x{2019}\x{201A}\x{201B}\x{2039}\x{203A}\x{FF07}]/;
  18. my $odd_singlequote_chars = qr/[\x{2019}]/;
  19. my $inline_count;
  20. pandoc_filter(
  21. \&todo,
  22. );
  23. sub latex_encode {
  24. my $s = shift;
  25. $s =~ s/(?<!\s|$breakable_chars)($breakable_chars])(?!\s)/$1\\-/g;
  26. $s =~ s/$odd_singlequote_chars/'/g; #'
  27. return $s;
  28. }
  29. sub mark_inside {
  30. my ( $before, $it, $after, $note ) = @_;
  31. return [ RawInline( 'latex', sprintf( '%s\\mytodo{%s}{%s}%s',
  32. latex_encode($before),
  33. latex_encode($note),
  34. latex_encode($it),
  35. latex_encode($after),
  36. ))];
  37. };
  38. sub todo {
  39. my $self = shift;
  40. $inline_count = 0 if ( $self->is_block );
  41. return unless ( $self->name eq 'Str' );
  42. given ($self->content) {
  43. $inline_count++;
  44. when (/^(-)$/) {
  45. return mark_inside( '', $1, '', 'maybe break' ) };
  46. when (/^(---+)(.+?)$/) {
  47. return mark_inside( '', $1, $2, 'maybe break' ) };
  48. when (/^(.+?)(--+)(.+?)$/) {
  49. return mark_inside( $1, $2, $3, 'maybe range' ) };
  50. when ( /^(.*?)($hyphen_chars(?:.*$hyphen_chars)?)(.*?)$/ ) {
  51. return mark_inside( $1, $2, $3, 'fancy hyphen' ) };
  52. when ( /^(.*?)($doublequote_chars(?:.*$doublequote_chars)?)(.*?)$/ ) {
  53. return mark_inside( $1, $2, $3, 'fancy quote' ) };
  54. when ( /^(.*?)($singlequote_chars(?:.*$singlequote_chars)?)(.*?)$/ ) {
  55. return mark_inside( $1, $2, $3, 'fancy quote' ) };
  56. when ( $inline_count == 1 and /^(\(?\d+\)|\d+\.)$/ ) {
  57. return mark_inside( '', $1, '', 'maybe list' ) };
  58. when ( $inline_count == 1 and /^(\(?[ivxc]+\)|\d+\.[\d.]+)$/ ) {
  59. return mark_inside( '', $1, '', 'maybe fancy list' ) };
  60. default { return };
  61. }
  62. };