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