summaryrefslogtreecommitdiff
path: root/pandoc-emphasis
blob: 2d61514bfd4bf6a32423a3f81b7dd4fea06cc287 (plain)
  1. #!/usr/bin/perl
  2. # emphasis + doublequote means technical or foreign term.
  3. # doublequote + emphasis means title.
  4. use Moops -strict;
  5. use feature qw(switch);
  6. no if $] >= 5.018, warnings => "experimental::smartmatch";
  7. use Log::Contextual::LogDispatchouli qw( :log set_logger );
  8. use Log::Dispatchouli;
  9. use Pandoc::Filter 0.06;
  10. use Pandoc::Elements;
  11. my $ld = Log::Dispatchouli->new({
  12. ident => 'pandoc-emphasis',
  13. to_stderr => 1,
  14. debug => ($ENV{PANDOC_DEBUG}),
  15. quiet_fatal => 'stdout',
  16. });
  17. set_logger $ld;
  18. class Window {
  19. use Pandoc::Filter;
  20. has view => {
  21. is => 'rw',
  22. isa => Object,
  23. };
  24. has ledge => {
  25. is => 'rw',
  26. isa => Maybe[Object],
  27. };
  28. method set_view ( Object $object ) {
  29. $self->view($object);
  30. }
  31. method peek_at_ledge () {
  32. return
  33. unless ( $self->ledge );
  34. my $value = stringify( $self->ledge );
  35. $value = $self->ledge->content;
  36. return $value;
  37. }
  38. method clear_ledge () {
  39. $self->ledge(undef);
  40. }
  41. method my $recall_from_ledge () {
  42. my $oldview = $self->ledge;
  43. $self->clear_ledge;
  44. return $oldview;
  45. }
  46. method describe_view () {
  47. return [ $self->$recall_from_ledge, $self->view ]
  48. if ( $self->ledge );
  49. return;
  50. }
  51. method interpret_view ( Object $interpretation ) {
  52. return [ $self->$recall_from_ledge, $interpretation ]
  53. if ( $self->$recall_from_ledge );
  54. return [ $interpretation ];
  55. }
  56. method suppress_view () {
  57. return [ $self->$recall_from_ledge ]
  58. if ( $self->ledge );
  59. return [];
  60. }
  61. method postpone_view () {
  62. $self->ledge( $self->view );
  63. return [];
  64. }
  65. }
  66. my $window = Window->new;
  67. my ($title, $foreign);
  68. pandoc_filter(
  69. \&emphasis,
  70. );
  71. sub emphasis {
  72. my $self = shift;
  73. return unless ( $self->name =~ /Str|Emph/ );
  74. if ( $self->name eq 'Emph' ) {
  75. $window->set_view($self);
  76. given ( stringify($self) ) {
  77. when (/^[^\"]+$/) {
  78. if ( $window->peek_at_ledge() eq '"' ) {
  79. $window->clear_ledge();
  80. $title++;
  81. # FIXME: LaTeX-encode text
  82. log_debug { 'emphasis →title: ' . $_ };
  83. return $window->interpret_view(
  84. RawInline( 'latex',
  85. '\\title{' . $_ . '}' ));
  86. }
  87. }
  88. when (/^\"([^\"]+)\"$/) {
  89. # FIXME: LaTeX-encode text
  90. log_debug { 'emphasis →foreign: ' . $_ };
  91. return $window->interpret_view(
  92. RawInline( 'latex',
  93. '\\foreign{' . $1 . '}' ));
  94. }
  95. }
  96. return $window->describe_view();
  97. }
  98. if ( $self->name eq 'Str' ) {
  99. $window->set_view($self);
  100. given ( $self->content ) {
  101. when ('"') {
  102. if ($title) {
  103. $title--;
  104. return $window->suppress_view();
  105. };
  106. return $window->postpone_view();
  107. }
  108. }
  109. $window->clear_ledge();
  110. if ($title) {
  111. log_info { "emphasis odd after title markup." };
  112. $title--;
  113. }
  114. return $window->describe_view();
  115. }
  116. return;
  117. }