From 5cbb6b9dca11fe98b2feefcd31684ee2c2f1455a Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 25 Dec 2014 18:19:55 +0100 Subject: Rename and shorten filters. --- Makefile | 2 +- pandoc-cs1 | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ pandoc-filter-bib | 68 -------------------------------------------------- pandoc-filter-iri | 41 ------------------------------ pandoc-filter-sections | 55 ---------------------------------------- pandoc-iri | 41 ++++++++++++++++++++++++++++++ pandoc-memoir | 55 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 165 insertions(+), 165 deletions(-) create mode 100755 pandoc-cs1 delete mode 100755 pandoc-filter-bib delete mode 100755 pandoc-filter-iri delete mode 100755 pandoc-filter-sections create mode 100755 pandoc-iri create mode 100755 pandoc-memoir diff --git a/Makefile b/Makefile index 2cbd7c8..43e8607 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ re_urlencode = s|\bhttp[^\s\"]+?\K(--+)|uri_escape($$1, "-")|eg templates = template.tex header.tex before.tex after.tex -filters = ./pandoc-filter-sections ./pandoc-filter-bib ./pandoc-filter-iri +filters = ./pandoc-memoir ./pandoc-cs1 ./pandoc-iri export PANDOC_CITEPROC_FILE = $(stem).bib diff --git a/pandoc-cs1 b/pandoc-cs1 new file mode 100755 index 0000000..27ba8d4 --- /dev/null +++ b/pandoc-cs1 @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use v5.10.1; # use switch keyword "when" (but avoid too complex "given") +no if $] >= 5.018, warnings => "experimental::smartmatch"; + +use Pandoc::Filter; +use Pandoc::Elements; + +use Path::Tiny; +use List::Util qw(pairmap); + +my $bibfilename = $ENV{'PANDOC_CITEPROC_FILE'} || 'bibliography.bib'; +my $bibfile = path($bibfilename); + +my (@a, $i); + +pandoc_filter sub { + my $self = shift; + return unless ($self->name eq 'RawInline' and $self->format eq 'mediawiki'); + + for ($self->content) { + when (/^{{cite\s+(\w+)\s*\|([^}]*)}}$/) { + my $id = 'ref'.++$i; + my @data = pairmap { $b =~ s/"/\\"/g; "$a=\"$b\"" } + map { /^\s*(\w+)\s*=\s*"?(.*?)"?\s*$/ } + split( /\|/, $2 ); + push @a, join ",\n", + '@'.$1.'{'.$id, + @data, + '}'; + return Cite( + [{ + 'citationId' => $id, + 'citationPrefix' => [], + 'citationSuffix' => [], + 'citationMode' => { + t => 'NormalCitation', + c => [], + }, + 'citationNoteNum' => 0, + 'citationHash' => 0, + }], + [ Str $id ], + ); + } + when (/^{{citation needed}}$/) { + say STDERR "WARNING: Mediawiki citation needed."; + return [ + Str "citation", + Space, + Str "needed", + ]; + } + default { + say STDERR "WARNING: Mediawiki unknown data skipped: " + . '"' . $_[0]->content . '"'; + return Str ""; + } + } +}; + +END { + $bibfile->spew_utf8( join "\n\n", @a ) + if (@a); +} diff --git a/pandoc-filter-bib b/pandoc-filter-bib deleted file mode 100755 index 27ba8d4..0000000 --- a/pandoc-filter-bib +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/perl - -use warnings; -use strict; - -use v5.10.1; # use switch keyword "when" (but avoid too complex "given") -no if $] >= 5.018, warnings => "experimental::smartmatch"; - -use Pandoc::Filter; -use Pandoc::Elements; - -use Path::Tiny; -use List::Util qw(pairmap); - -my $bibfilename = $ENV{'PANDOC_CITEPROC_FILE'} || 'bibliography.bib'; -my $bibfile = path($bibfilename); - -my (@a, $i); - -pandoc_filter sub { - my $self = shift; - return unless ($self->name eq 'RawInline' and $self->format eq 'mediawiki'); - - for ($self->content) { - when (/^{{cite\s+(\w+)\s*\|([^}]*)}}$/) { - my $id = 'ref'.++$i; - my @data = pairmap { $b =~ s/"/\\"/g; "$a=\"$b\"" } - map { /^\s*(\w+)\s*=\s*"?(.*?)"?\s*$/ } - split( /\|/, $2 ); - push @a, join ",\n", - '@'.$1.'{'.$id, - @data, - '}'; - return Cite( - [{ - 'citationId' => $id, - 'citationPrefix' => [], - 'citationSuffix' => [], - 'citationMode' => { - t => 'NormalCitation', - c => [], - }, - 'citationNoteNum' => 0, - 'citationHash' => 0, - }], - [ Str $id ], - ); - } - when (/^{{citation needed}}$/) { - say STDERR "WARNING: Mediawiki citation needed."; - return [ - Str "citation", - Space, - Str "needed", - ]; - } - default { - say STDERR "WARNING: Mediawiki unknown data skipped: " - . '"' . $_[0]->content . '"'; - return Str ""; - } - } -}; - -END { - $bibfile->spew_utf8( join "\n\n", @a ) - if (@a); -} diff --git a/pandoc-filter-iri b/pandoc-filter-iri deleted file mode 100755 index 94be2e4..0000000 --- a/pandoc-filter-iri +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/perl - -use warnings; -use strict; - -use Pandoc::Filter; -use Pandoc::Elements; - -use URI; -use URI::Find; - -pandoc_filter( - \&link_normalize, -); - -sub link_normalize { - my $self = shift; - return [ Link( - $self->content, - [ pp_uri($self->target->[0]) => '' ] - )] - if ( $self->name eq 'Link' ); - return [ Str pp_string($self->content) ] - if ( $self->name eq 'Str' ); - return; -} - -sub pp_uri { - return URI->new(shift)->canonical->as_string; -} - -sub pp_iri { - return URI->new(shift)->canonical->as_iri; -} - -sub pp_string { - my $string = shift; - my $finder = URI::Find->new( \&pp_iri ); - $finder->find(\$string); - return $string; -} diff --git a/pandoc-filter-sections b/pandoc-filter-sections deleted file mode 100755 index f0bb5a5..0000000 --- a/pandoc-filter-sections +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/perl - -use warnings; -use strict; - -use Pandoc::Filter; -use Pandoc::Elements; - -# FIXME: avoid eating content past tweaked headers -pandoc_filter( - \&frontmatter, - \&mainmatter, - \&backmatter, - \&toc, -); - -# FIXME: use Header (not latex RawBlock) - why does it hang?!? -sub header { - my $label = shift; -# Header( 1, attributes {}, [ Str $label ], ); - RawBlock( 'latex', '\\chapter{'.$label.'}' ); -}; - -sub frontmatter { - my $self = shift; - return [ RawBlock( 'latex', '\\frontmatter' ), header('About'), ] - if ( $self->name eq 'Header' and $self->level >= 1 - and stringify($self) eq 'About' ); - return; -} - -sub mainmatter { - my $self = shift; - return [ RawBlock( 'latex', '\\mainmatter' ), header(stringify($self)), ] - if ( $self->name eq 'Header' and $self->level == 1 - and stringify($self) =~ /^Scope/ ); - return; -} - -sub backmatter { - my $self = shift; - return [ RawBlock( 'latex', '\\backmatter' ), header('References'), ] - if ( $self->name eq 'Header' and $self->level == 1 - and stringify($self) =~ /^Notes/ ); - return; -} - -sub toc { - my $self = shift; - return unless ( $self->name eq 'Header' and $self->level == 1 - and stringify($self) =~ /^Table/ ); - return RawBlock( 'latex', - '{\\hypersetup{linkcolor=black}\\setcounter{tocdepth}{3}\\tableofcontents}' - ); -} diff --git a/pandoc-iri b/pandoc-iri new file mode 100755 index 0000000..94be2e4 --- /dev/null +++ b/pandoc-iri @@ -0,0 +1,41 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use Pandoc::Filter; +use Pandoc::Elements; + +use URI; +use URI::Find; + +pandoc_filter( + \&link_normalize, +); + +sub link_normalize { + my $self = shift; + return [ Link( + $self->content, + [ pp_uri($self->target->[0]) => '' ] + )] + if ( $self->name eq 'Link' ); + return [ Str pp_string($self->content) ] + if ( $self->name eq 'Str' ); + return; +} + +sub pp_uri { + return URI->new(shift)->canonical->as_string; +} + +sub pp_iri { + return URI->new(shift)->canonical->as_iri; +} + +sub pp_string { + my $string = shift; + my $finder = URI::Find->new( \&pp_iri ); + $finder->find(\$string); + return $string; +} diff --git a/pandoc-memoir b/pandoc-memoir new file mode 100755 index 0000000..f0bb5a5 --- /dev/null +++ b/pandoc-memoir @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use Pandoc::Filter; +use Pandoc::Elements; + +# FIXME: avoid eating content past tweaked headers +pandoc_filter( + \&frontmatter, + \&mainmatter, + \&backmatter, + \&toc, +); + +# FIXME: use Header (not latex RawBlock) - why does it hang?!? +sub header { + my $label = shift; +# Header( 1, attributes {}, [ Str $label ], ); + RawBlock( 'latex', '\\chapter{'.$label.'}' ); +}; + +sub frontmatter { + my $self = shift; + return [ RawBlock( 'latex', '\\frontmatter' ), header('About'), ] + if ( $self->name eq 'Header' and $self->level >= 1 + and stringify($self) eq 'About' ); + return; +} + +sub mainmatter { + my $self = shift; + return [ RawBlock( 'latex', '\\mainmatter' ), header(stringify($self)), ] + if ( $self->name eq 'Header' and $self->level == 1 + and stringify($self) =~ /^Scope/ ); + return; +} + +sub backmatter { + my $self = shift; + return [ RawBlock( 'latex', '\\backmatter' ), header('References'), ] + if ( $self->name eq 'Header' and $self->level == 1 + and stringify($self) =~ /^Notes/ ); + return; +} + +sub toc { + my $self = shift; + return unless ( $self->name eq 'Header' and $self->level == 1 + and stringify($self) =~ /^Table/ ); + return RawBlock( 'latex', + '{\\hypersetup{linkcolor=black}\\setcounter{tocdepth}{3}\\tableofcontents}' + ); +} -- cgit v1.2.3