blob: 2d61514bfd4bf6a32423a3f81b7dd4fea06cc287 (
plain)
- #!/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;
- }
|