summaryrefslogtreecommitdiff
path: root/pandoc-todo
blob: 831704819c15a29f1ffe51b565e7327a3cd7d75c (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. my $inline_count;
  19. pandoc_filter(
  20. \&todo,
  21. );
  22. sub latex_encode {
  23. my $s = shift;
  24. $s =~ s/(?<!\s|$breakable_chars)($breakable_chars])(?!\s)/$1\\-/g;
  25. $s =~ s/$odd_singlequote_chars/'/g; #'
  26. return $s;
  27. }
  28. sub mark_inside {
  29. my ( $before, $it, $after, $note ) = @_;
  30. return [ RawInline( 'latex', sprintf( '%s\\mytodo{%s}{%s}%s',
  31. latex_encode($before),
  32. latex_encode($note),
  33. latex_encode($it),
  34. latex_encode($after),
  35. ))];
  36. };
  37. sub todo {
  38. my $self = shift;
  39. $inline_count = 0 if ( $self->is_block );
  40. return unless ( $self->name eq 'Str' );
  41. given ($self->content) {
  42. $inline_count++;
  43. when (/^(-)$/) {
  44. return mark_inside( '', $1, '', 'maybe break' ) };
  45. when (/^(---+)(.+?)$/) {
  46. return mark_inside( '', $1, $2, 'maybe break' ) };
  47. when (/^(.+?)(--+)(.+?)$/) {
  48. return mark_inside( $1, $2, $3, 'maybe range' ) };
  49. when ( /^(.*?)($hyphen_chars(?:.*$hyphen_chars)?)(.*?)$/ ) {
  50. return mark_inside( $1, $2, $3, 'fancy hyphen' ) };
  51. when ( /^(.*?)($doublequote_chars(?:.*$doublequote_chars)?)(.*?)$/ ) {
  52. return mark_inside( $1, $2, $3, 'fancy quote' ) };
  53. when ( /^(.*?)($singlequote_chars(?:.*$singlequote_chars)?)(.*?)$/ ) {
  54. return mark_inside( $1, $2, $3, 'fancy quote' ) };
  55. when ( $inline_count == 1 and /^(\(?\d+\)|\d+\.)$/ ) {
  56. return mark_inside( '', $1, '', 'maybe list' ) };
  57. when ( $inline_count == 1 and /^(\(?[ivxc]+\)|\d+\.[\d.]+)$/ ) {
  58. return mark_inside( '', $1, '', 'maybe fancy list' ) };
  59. default { return };
  60. }
  61. };