summaryrefslogtreecommitdiff
path: root/pandoc-emphasis
blob: 34770f564529d166a21d6549f157476ff86e34c8 (plain)
  1. #!/usr/bin/perl
  2. # emphasis + doublequote means technical or foreign term.
  3. # doublequote + emphasis means title.
  4. use Moops -strict;
  5. use Pandoc::Filter 0.06;
  6. use Pandoc::Elements;
  7. use Log::Any qw($log);
  8. use Log::Any::Adapter ( 'ScreenColoredLevel',
  9. use_color => ! -t *STDERR );
  10. class Window {
  11. use Pandoc::Filter;
  12. has view => {
  13. is => 'rw',
  14. isa => Object,
  15. };
  16. has ledge => {
  17. is => 'rw',
  18. isa => Maybe[Object],
  19. };
  20. method set_view ( Object $object ) {
  21. $self->view($object);
  22. }
  23. method peek_at_ledge () {
  24. return
  25. unless ( $self->ledge );
  26. my $value = stringify( $self->ledge );
  27. $value = $self->ledge->content;
  28. return $value;
  29. }
  30. method clear_ledge () {
  31. $self->ledge(undef);
  32. }
  33. method my $recall_from_ledge () {
  34. my $oldview = $self->ledge;
  35. $self->clear_ledge;
  36. return $oldview;
  37. }
  38. method describe_view () {
  39. return [ $self->$recall_from_ledge, $self->view ]
  40. if ( $self->ledge );
  41. return;
  42. }
  43. method interpret_view ( Object $interpretation ) {
  44. return [ $self->$recall_from_ledge, $interpretation ]
  45. if ( $self->$recall_from_ledge );
  46. return [ $interpretation ];
  47. }
  48. method suppress_view () {
  49. return [ $self->$recall_from_ledge ]
  50. if ( $self->ledge );
  51. return [];
  52. }
  53. method postpone_view () {
  54. $self->ledge( $self->view );
  55. return [];
  56. }
  57. }
  58. my $window = Window->new;
  59. my ($title, $foreign);
  60. pandoc_filter(
  61. \&emphasis,
  62. );
  63. sub emphasis {
  64. my $self = shift;
  65. return unless ( $self->name =~ /Str|Emph/ );
  66. if ( $self->name eq 'Emph' ) {
  67. $window->set_view($self);
  68. if ( $self->content->[0]->name eq 'Str'
  69. and $window->peek_at_ledge() eq '"'
  70. ) {
  71. $window->clear_ledge();
  72. $title++;
  73. $log->infof( 'emphasis: %s+Emph→title: %s',
  74. '"',
  75. stringify(
  76. $self->content ));
  77. return $window->interpret_view(
  78. Quoted( DoubleQuote,
  79. $self->content ));
  80. }
  81. if ( $self->content->[0]->name eq 'Quoted'
  82. ) {
  83. my $new_content = stringify(
  84. $self->content->[0] );
  85. $log->infof( 'emphasis: Emph+Quoted→foreign: %s',
  86. $new_content );
  87. # FIXME: LaTeX-encode content
  88. return $window->interpret_view(
  89. RawInline( 'latex',
  90. sprintf( '\foreign{%s}',
  91. $new_content )));
  92. }
  93. return $window->describe_view();
  94. }
  95. if ( $self->name eq 'Str' ) {
  96. $window->set_view($self);
  97. if ( $self->content eq '"' ) {
  98. if ($title) {
  99. $title--;
  100. return $window->suppress_view();
  101. };
  102. return $window->postpone_view();
  103. }
  104. $window->clear_ledge();
  105. if ($title) {
  106. $log->warnf( "emphasis: odd title end markup." );
  107. $title--;
  108. }
  109. return $window->describe_view();
  110. }
  111. return;
  112. }