summaryrefslogtreecommitdiff
path: root/pandoc-filter-bib
blob: 27ba8d48e7ac1ed4bf694ca7c09b9a955bb45917 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use v5.10.1; # use switch keyword "when" (but avoid too complex "given")
  5. no if $] >= 5.018, warnings => "experimental::smartmatch";
  6. use Pandoc::Filter;
  7. use Pandoc::Elements;
  8. use Path::Tiny;
  9. use List::Util qw(pairmap);
  10. my $bibfilename = $ENV{'PANDOC_CITEPROC_FILE'} || 'bibliography.bib';
  11. my $bibfile = path($bibfilename);
  12. my (@a, $i);
  13. pandoc_filter sub {
  14. my $self = shift;
  15. return unless ($self->name eq 'RawInline' and $self->format eq 'mediawiki');
  16. for ($self->content) {
  17. when (/^{{cite\s+(\w+)\s*\|([^}]*)}}$/) {
  18. my $id = 'ref'.++$i;
  19. my @data = pairmap { $b =~ s/"/\\"/g; "$a=\"$b\"" }
  20. map { /^\s*(\w+)\s*=\s*"?(.*?)"?\s*$/ }
  21. split( /\|/, $2 );
  22. push @a, join ",\n",
  23. '@'.$1.'{'.$id,
  24. @data,
  25. '}';
  26. return Cite(
  27. [{
  28. 'citationId' => $id,
  29. 'citationPrefix' => [],
  30. 'citationSuffix' => [],
  31. 'citationMode' => {
  32. t => 'NormalCitation',
  33. c => [],
  34. },
  35. 'citationNoteNum' => 0,
  36. 'citationHash' => 0,
  37. }],
  38. [ Str $id ],
  39. );
  40. }
  41. when (/^{{citation needed}}$/) {
  42. say STDERR "WARNING: Mediawiki citation needed.";
  43. return [
  44. Str "citation",
  45. Space,
  46. Str "needed",
  47. ];
  48. }
  49. default {
  50. say STDERR "WARNING: Mediawiki unknown data skipped: "
  51. . '"' . $_[0]->content . '"';
  52. return Str "";
  53. }
  54. }
  55. };
  56. END {
  57. $bibfile->spew_utf8( join "\n\n", @a )
  58. if (@a);
  59. }