File: qids.pl

package info (click to toggle)
libpandoc-elements-perl 0.38-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 732 kB
  • sloc: perl: 1,630; makefile: 15; sh: 1
file content (28 lines) | stat: -rw-r--r-- 586 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env perl
use strict;

=head1 DESCRIPTION

Pandoc filter to link all Wikidata ids such as C<[Q123]>

=cut

use Pandoc::Filter;
use Pandoc::Elements;

pandoc_filter Str => sub {
    my @split = split /\[([QP][0-9]+)\]/, $_->content;
    return if @split < 2;

    my @inlines;
    while (@split) {
        my $str = shift @split;
        push @inlines, Str($str) if $str ne '';

        my $id  = shift @split or last;
        my $url = "http://www.wikidata.org/entity/$id";
        push @inlines, Link attributes {}, [ Str $id ], [ $url, '' ];
    }
    return \@inlines;
};