-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmdbibexport.pl
52 lines (42 loc) · 1.37 KB
/
mdbibexport.pl
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl
#Simple script to extract references from a bibtex database which are cited in a markdown file
#The installation of BibTool, Pandoc and Perl is required
#Version: 0.2
#Release: 2016/10/31
#Copyright: Dr. Robert Winkler
#Contributor: Albert Krewinkel
#Contact: [email protected], [email protected]
#License: General Public License (GPL), 3.0
use strict;
use warnings;
print "Extract from markdown (.md) file: ";
my $namemd = <STDIN>;
chomp($namemd);
print "Extract from bibtex database (.bib) file: ";
my $namebib = <STDIN>;
chomp($namebib);
#mdbibexport files
my $nameaux = "mdbibexport.aux";
my $nameout = "mdbibexport.bib";
open my $fm, '<', $namemd or die "Could not open '$namemd' $!\n";
my $mdcitations = "";
my $pdcmd = 'pandoc -t json '. $namemd . ' > mdbibexport.tmp';
system($pdcmd);
my $pandoctmp = "mdbibexport.tmp";
open my $fm2, '<', $pandoctmp or die "Could not open '$pandoctmp' $!\n";
while (my $line = <$fm2>) {
chomp $line;
while ($line =~ /"citationId":"([^"]*)"/g){
print "Extracted citation: $1\n";
$mdcitations .= "$1,";
}
}
open(my $fa, '>', $nameaux) or die;
print $fa "\\bibstyle{alpha}\n";
print $fa "\\bibdata{$namebib}\n";
print $fa "\\citation{$mdcitations}\n";
close $fa;
print "$nameaux created.\n";
my $btcmd = 'bibtool -v -x '. $nameaux . ' -o ' . $nameout;
system($btcmd);
print "$nameout created.\n";