-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbdsk-to-zotero-attachments.pl
50 lines (46 loc) · 1.56 KB
/
bdsk-to-zotero-attachments.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
#!/usr/bin/perl
#
#bdsk-zotero-attachments.pl
#converts 1 BibTeX file to another BibTeX file that can be imported into Zotero with its attachments intact.
#020130205 Peter M. Carlton ([email protected])
#Licensed under CC attribution-sharealike http://creativecommons.org/licenses/by-sa/3.0/
use strict;
use warnings;
my $fname = shift;
my $newname=shift;
my $basedir=`dirname $fname`;
chomp($basedir);
my $tmpname="/tmp/b64.encoded";
open IN,$fname;
open OUT,">$newname";
foreach my $line (<IN>) {
open TMP,">$tmpname";
chomp($line);
if ($line =~ /Bdsk-File/) {
my $extrabracket=($line =~ /}}/)?1:0;
my $comma=($line =~ /},/)?1:0;
my $num=$line;$num =~ s/.*Bdsk-File-//;$num =~ s/\s.*//;
$line =~ s/.*{//;
$line =~ s/}.*//;
# $line =~ s/==/=/;
print TMP $line;
system("base64 -D -i $tmpname -o $tmpname.dec");
system("plutil -convert xml1 $tmpname.dec");
my $decoded=`grep -A 3 relative $tmpname.dec | tail -n 3 | head -n 1`;
my $testtype=$decoded;$testtype=~s/<string>//;$testtype=~s/<\/string>//;$testtype=~s/\t//g;$testtype=$basedir.'/'.$testtype;
chomp $testtype;
$decoded =~ s/<string>/Local-Zo-Url-$num = {$basedir\//;
$decoded =~ s/<\/string>/}/;
chomp($decoded);
$decoded .= "," if $comma;
if (-d $testtype) {print "Ignoring attached directory $testtype\n";}
print OUT $decoded,"\n" unless -d $testtype;
print OUT "}" if $extrabracket;
}
else {
print OUT $line,"\n";
}
close TMP;
}
close IN;
close OUT;