summaryrefslogtreecommitdiff
path: root/pandoc-emphasis
blob: 97ddac079b524a517f8799ff97d59e666cb68509 (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 ( $self->content->[0]->name eq 'Str' ) {
  78. if ( $window->peek_at_ledge() eq '"' ) {
  79. $window->clear_ledge();
  80. $title++;
  81. log_debug { 'emphasis →title: ' . $_ };
  82. return $window->interpret_view(
  83. Quoted( DoubleQuote,
  84. $self->content ));
  85. }
  86. }
  87. when ( $self->content->[0]->name eq 'Quoted' ) {
  88. log_debug { 'emphasis →foreign: ' . $_ };
  89. # FIXME: LaTeX-encode text
  90. return $window->interpret_view(
  91. RawInline( 'latex',
  92. '\\foreign{' . $_ . '}' ));
  93. }
  94. }
  95. return $window->describe_view();
  96. }
  97. if ( $self->name eq 'Str' ) {
  98. $window->set_view($self);
  99. given ( $self->content ) {
  100. when ('"') {
  101. if ($title) {
  102. $title--;
  103. return $window->suppress_view();
  104. };
  105. return $window->postpone_view();
  106. }
  107. }
  108. $window->clear_ledge();
  109. if ($title) {
  110. log_info { "emphasis odd after title markup." };
  111. $title--;
  112. }
  113. return $window->describe_view();
  114. }
  115. return;
  116. }