summaryrefslogtreecommitdiff
path: root/pandoc-emphasis
blob: 60c42d85d4635078d01e8e4c0080e1026f1a4b7f (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 text
  87. return $window->interpret_view(
  88. RawInline( 'latex',
  89. '\\foreign{' . $_ . '}' ));
  90. }
  91. }
  92. return $window->describe_view();
  93. }
  94. if ( $self->name eq 'Str' ) {
  95. $window->set_view($self);
  96. given ( $self->content ) {
  97. when ('"') {
  98. if ($title) {
  99. $title--;
  100. return $window->suppress_view();
  101. };
  102. return $window->postpone_view();
  103. }
  104. }
  105. $window->clear_ledge();
  106. if ($title) {
  107. $log->warnf( "emphasis: odd title end markup." );
  108. $title--;
  109. }
  110. return $window->describe_view();
  111. }
  112. return;
  113. }