diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | header.tex | 1 | ||||
-rwxr-xr-x | pandoc-emphasis | 128 |
3 files changed, 130 insertions, 1 deletions
@@ -9,7 +9,7 @@ citeproc_file = $(stem).bib templates = template.tex header.tex before.tex after.tex -filters = ./pandoc-memoir ./pandoc-cs1 ./pandoc-todo +filters = ./pandoc-memoir ./pandoc-emphasis ./pandoc-cs1 ./pandoc-todo filters += $(if $(citeproc_file),pandoc-citeproc) filters += ./pandoc-iri @@ -22,3 +22,4 @@ \do0\do1\do2\do3\do4\do5\do6\do7\do8\do9\do=\do:\do\/\do\.} \semiisopage[7] \checkandfixthelayout +\usepackage{foreign} diff --git a/pandoc-emphasis b/pandoc-emphasis new file mode 100755 index 0000000..2d61514 --- /dev/null +++ b/pandoc-emphasis @@ -0,0 +1,128 @@ +#!/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 Log::Contextual::LogDispatchouli qw( :log set_logger ); +use Log::Dispatchouli; +use Pandoc::Filter 0.06; +use Pandoc::Elements; + +my $ld = Log::Dispatchouli->new({ + ident => 'pandoc-emphasis', + to_stderr => 1, + debug => ($ENV{PANDOC_DEBUG}), + quiet_fatal => 'stdout', +}); +set_logger $ld; + +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 (/^[^\"]+$/) { + if ( $window->peek_at_ledge() eq '"' ) { + $window->clear_ledge(); + $title++; +# FIXME: LaTeX-encode text + log_debug { 'emphasis →title: ' . $_ }; + return $window->interpret_view( + RawInline( 'latex', + '\\title{' . $_ . '}' )); + } + } + when (/^\"([^\"]+)\"$/) { +# FIXME: LaTeX-encode text + log_debug { 'emphasis →foreign: ' . $_ }; + return $window->interpret_view( + RawInline( 'latex', + '\\foreign{' . $1 . '}' )); + } + } + 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_info { "emphasis odd after title markup." }; + $title--; + } + return $window->describe_view(); + } + return; +} |