summaryrefslogtreecommitdiff
path: root/pandoc-cs1
blob: e5620c9ca717a6e542c0fe365e9ac9c54ffd3af7 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use feature qw(switch);
  5. no if $] >= 5.018, warnings => "experimental::smartmatch";
  6. use Pandoc::Filter 0.05;
  7. use Pandoc::Elements;
  8. use Text::BibTeX::Entry;
  9. use Path::Tiny;
  10. use List::Util qw(pairmap);
  11. use DateTime::Format::Flexible;
  12. my $bibfilename = $ENV{'PANDOC_CITEPROC_FILE'} || 'bibliography.bib';
  13. my $bibfile = path($bibfilename);
  14. my $bibdata = new Text::BibTeX::Entry;
  15. my (@a, $i);
  16. pandoc_filter sub {
  17. my $self = shift;
  18. return unless ($self->name eq 'RawInline' and $self->format eq 'mediawiki');
  19. given ($self->content) {
  20. when (/^\{\{cite\s+(\w+)\s*\|([^}]*)\}}$/) {
  21. my $id = 'ref'.++$i;
  22. my @data = pairmap { $b =~ s/"/\\"/g; "$a=\"$b\"" }
  23. map { /^\s*(\w+)\s*=\s*"?(.*?)"?\s*$/ }
  24. split( /\|/, $2 );
  25. $bibdata->parse_s (join ",\n",
  26. '@'.$1.'{'.$id,
  27. @data,
  28. '}');
  29. if ( ! $bibdata->get('author') ) {
  30. my @authors;
  31. for ( my $i = 1;; $i++ ) {
  32. my ( $first, $last );
  33. if ( $i == 1 and $bibdata->get('first') ) {
  34. $first = $bibdata->get("first$i");
  35. $bibdata->delete("first$i");
  36. } elsif ( $bibdata->get("first$i") ) {
  37. $first = $bibdata->get('first');
  38. $bibdata->delete('first');
  39. }
  40. if ( $i == 1 and $bibdata->get('last') ) {
  41. $last = $bibdata->get('last');
  42. $bibdata->delete('last');
  43. } elsif ( $bibdata->get("last$i") ) {
  44. $last = $bibdata->get("last$i");
  45. $bibdata->delete("last$i");
  46. }
  47. if ( $first and $last ) {
  48. push @authors, "$first {$last}";
  49. } elsif ($first) {
  50. push @authors, $first;
  51. } elsif ($last) {
  52. push @authors, $last;
  53. } else {
  54. last;
  55. }
  56. }
  57. if ( @authors > 0 ) {
  58. $bibdata->set( 'author', join ( ' and ', @authors ) );
  59. };
  60. };
  61. if ( $bibdata->type =~ /web/i ) {
  62. $bibdata->set_type('online');
  63. if ( my $urldate = $bibdata->get('accessdate') ) {
  64. my $dt = DateTime::Format::Flexible->parse_datetime($urldate);
  65. $bibdata->set( 'urldate', $dt->format_cldr('yyyy-MM-dd') );
  66. $bibdata->delete('accessdate');
  67. };
  68. };
  69. $bibfile->spew( '' )
  70. if ( $i <= 1 );
  71. $bibfile->append( $bibdata->print_s );
  72. return Cite(
  73. [{
  74. 'citationId' => $id,
  75. 'citationPrefix' => [],
  76. 'citationSuffix' => [],
  77. 'citationMode' => {
  78. t => 'NormalCitation',
  79. c => [],
  80. },
  81. 'citationNoteNum' => 0,
  82. 'citationHash' => 0,
  83. }],
  84. [ Str $id ],
  85. );
  86. }
  87. when (/^\{\{citation needed\}\}$/) {
  88. say STDERR "WARNING: Mediawiki citation needed.";
  89. return [
  90. Str "citation",
  91. Space,
  92. Str "needed",
  93. ];
  94. }
  95. default {
  96. say STDERR "WARNING: Mediawiki unknown data skipped: "
  97. . '"' . $_[0]->content . '"';
  98. return Str "";
  99. }
  100. }
  101. };