summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2015-03-17 17:54:35 +0100
committerJonas Smedegaard <dr@jones.dk>2015-03-17 17:54:35 +0100
commit988043c74eec24dfc2c82eb80f8964f01b3784b2 (patch)
tree4f7724dadd6c14c30d85267484afa198c9eb16f6
parent70c5f9018263f103a0732eda909d47a22c5435a3 (diff)
Add and enable emphasis filter, and add foreign package.
-rw-r--r--Makefile2
-rw-r--r--header.tex1
-rwxr-xr-xpandoc-emphasis128
3 files changed, 130 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 46e1570..990ba59 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/header.tex b/header.tex
index a546fc1..461d5a4 100644
--- a/header.tex
+++ b/header.tex
@@ -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;
+}