#!/usr/bin/perl use warnings; use strict; use feature qw(switch); 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'); given ($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); }