summaryrefslogtreecommitdiff
path: root/pandoc-cs1
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2014-12-25 18:19:55 +0100
committerJonas Smedegaard <dr@jones.dk>2014-12-25 18:19:55 +0100
commit5cbb6b9dca11fe98b2feefcd31684ee2c2f1455a (patch)
treeeef9ca15ab02bfc1873d3875350faa671188fea9 /pandoc-cs1
parent7912a20b53c06e5eb8ef9682d06bd57429a87fca (diff)
Rename and shorten filters.
Diffstat (limited to 'pandoc-cs1')
-rwxr-xr-xpandoc-cs168
1 files changed, 68 insertions, 0 deletions
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);
+}