#!/usr/bin/perl # emphasis + doublequote means technical or foreign term. # doublequote + emphasis means title. use Moops -strict; use feature qw(switch); no if $] >= 5.018, warnings => "experimental::smartmatch"; use Pandoc::Filter 0.06; use Pandoc::Elements; use Log::Any qw($log); use Log::Any::Adapter ( 'ScreenColoredLevel', use_color => ! -t *STDERR ); class Window { use Pandoc::Filter; has view => { is => 'rw', isa => Object, }; has ledge => { is => 'rw', isa => Maybe[Object], }; method set_view ( Object $object ) { $self->view($object); } method peek_at_ledge () { return unless ( $self->ledge ); my $value = stringify( $self->ledge ); $value = $self->ledge->content; return $value; } method clear_ledge () { $self->ledge(undef); } method my $recall_from_ledge () { my $oldview = $self->ledge; $self->clear_ledge; return $oldview; } method describe_view () { return [ $self->$recall_from_ledge, $self->view ] if ( $self->ledge ); return; } method interpret_view ( Object $interpretation ) { return [ $self->$recall_from_ledge, $interpretation ] if ( $self->$recall_from_ledge ); return [ $interpretation ]; } method suppress_view () { return [ $self->$recall_from_ledge ] if ( $self->ledge ); return []; } method postpone_view () { $self->ledge( $self->view ); return []; } } my $window = Window->new; my ($title, $foreign); pandoc_filter( \&emphasis, ); sub emphasis { my $self = shift; return unless ( $self->name =~ /Str|Emph/ ); if ( $self->name eq 'Emph' ) { $window->set_view($self); given ( stringify($self) ) { when ( $self->content->[0]->name eq 'Str' ) { if ( $window->peek_at_ledge() eq '"' ) { $window->clear_ledge(); $title++; $log->infof( 'emphasis: %s+Emph→title: %s', '"', $_ ); return $window->interpret_view( Quoted( DoubleQuote, $self->content )); } } when ( $self->content->[0]->name eq 'Quoted' ) { $log->infof( 'emphasis: Emph+Quoted→foreign: %s', $_ ); # FIXME: LaTeX-encode text return $window->interpret_view( RawInline( 'latex', '\\foreign{' . $_ . '}' )); } } return $window->describe_view(); } if ( $self->name eq 'Str' ) { $window->set_view($self); given ( $self->content ) { when ('"') { if ($title) { $title--; return $window->suppress_view(); }; return $window->postpone_view(); } } $window->clear_ledge(); if ($title) { $log->warnf( "emphasis: odd title end markup." ); $title--; } return $window->describe_view(); } return; }