2 # bibtex_to_paper opens the paper corresponding to a bibtex key
3 # and is released under the terms of the GNU GPL version 3, or any
4 # later version, at your option. See the file README and COPYING for
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
16 use File::Basename qw(basename);
17 use File::Spec qw(rel2abs);
26 bibtex_to_paper - opens the paper corresponding to a bibtex key
30 bibtex_to_paper [options] bibtexkey
33 --bibtex, -b bibtex file to look up key in
34 --bibtex-cache, -c bibtex cache file
35 --build-cache, -B build cache using bibtex files
36 --pdf-dir pdf directory
37 --pdfviewer, -p pdf viewer to use
38 --debug, -d debugging level (Default 0)
39 --help, -h display this help
40 --man, -m display manual
48 Bibtex file to look key up in
50 =item B<--bibtex-cache, -c>
52 Bibtex cache file; rebuilt if bibtex file changes
54 =item B<--pdfviewer, -p>
56 PDF viewer to use; defaults to evince unless a .xoj exists, in which
61 Debug verbosity. (Default 0)
65 Display brief usage information.
82 my %options = (debug => 0,
85 'bibtex_cache' => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
89 'build_cache|build-cache!',
91 'bibtex_cache|bibtex-cache|c=s',
93 'clear_cache|clear-cache!',
94 'papers_directory|papers-directory=s@',
95 'debug|d+','help|h|?','man|m');
97 pod2usage() if $options{help};
98 pod2usage({verbose=>2}) if $options{man};
100 $DEBUG = $options{debug};
103 if (not exists $options{bibtex} and
104 not exists $options{bibtex_cache}) {
106 "You must give at least one of --bibtex".
110 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
118 if (exists $options{bibtex_cache}) {
120 if (-e $options{bibtex_cache}) {
121 ($dbh,$sth) = open_cache($options{bibtex_cache});
123 ($dbh,$sth) = initialize_database($options{bibtex_cache});
127 if (exists $options{clear_cache}) {
128 clear_cache($dbh,$sth);
131 if (exists $options{build_cache}) {
132 $options{bibtex} //= [];
135 @{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]},
139 if (exists $options{bibtex}) {
140 for my $bibtex_file (@{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]}) {
141 parse_bibtex_file($bibtex_file,\%entries);
145 if (exists $options{papers_directory} and
148 load_papers_into_database($dbh,$sth,$options{papers_directory});
151 p %entries if $DEBUG;
152 if (keys %entries and
154 load_bibtex_entries_into_database($dbh,$sth,\%entries);
158 for my $bibtex_key (@ARGV) {
159 open_bibtex_key(\%options,$dbh,$sth,\%entries,$bibtex_key);
166 $sth->{clear_papers_cache}->execute();
167 $sth->{clear_bibtex_cache}->execute();
170 sub load_papers_into_database {
171 my ($dbh,$sth,$dir) = @_;
173 my @dirs = ref($dir)?@{$dir}:$dir;
175 my $actually_load_it = sub {
177 $File::Find::prune = 1;
180 return unless /\.pdf$/;
185 insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
189 find($actually_load_it,@dirs);
192 sub insert_or_replace_papers {
193 my ($dbh,$sth,$file_name,$file_loc,$has_xoj) = @_;
194 $sth->{insert_papers}->execute($file_name,$file_loc,$has_xoj);
195 $sth->{insert_papers}->finish();
198 sub load_bibtex_entries_into_database {
199 my ($dbh,$sth,$entries) = @_;
200 for my $entry (keys %{$entries}) {
201 next unless defined $entries->{$entry};
202 $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name doi html)});
203 $sth->{insert_bibtex}->finish();
204 print STDERR "inserted $entry {".join(',',map {defined $_?"'$_'":"'undef'"} %{$entries->{$entry}})."}\n" if $DEBUG;
208 sub open_bibtex_key {
209 my ($options,$dbh,$sth,$entries,$bibtex_key) = @_;
210 if (not defined $dbh) {
211 open_entry($dbh,$sth,$entries->{$bibtex_key},$options);
213 my $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
215 open_entry($dbh,$sth,$entry,$options);
222 if (not defined $child) {
223 die "Unable to fork for some reason: $!";
234 my ($file_name,$options,$has_xoj) = @_;
235 print STDERR "opening $file_name\n" if $DEBUG;
237 fork_exec('xournal',$file_name);
239 fork_exec('evince',$file_name)
245 fork_exec('sensible-browser',$file);
249 my ($dbh,$sth,$entry,$options) = @_;
251 return unless defined $entry and ref $entry and keys %{$entry};
252 if (defined $entry->{file_name} and length $entry->{file_name}) {
253 my $paper = select_one($dbh,$sth->{select_papers_by_name},$entry->{file_name});
254 if (not defined $paper) {
255 my ($pmid) = $entry->{file_name} =~ /pmid_(\d+)/;
256 if (defined $pmid and length $pmid) {
257 $paper = select_one($dbh,$sth->{select_papers_by_pmid},'%pmid_'.$pmid.'.%');
261 print STDERR $entry->{file_name} if $DEBUG;
262 if (defined $paper) {
263 open_pdf($paper->{path},$options,$paper->{has_xoj});
267 if (defined $entry->{doi}) {
268 my $url = $entry->{doi};
269 $url =~ s{^doi://}{http://dx.doi.org/};
270 open_browser($url,$options);
273 if (defined $entry->{html}) {
274 open_browser($entry->{html},$options);
279 sub select_entry_from_bibtex_key{
280 my ($dbh,$sth,$bibtex_key) = @_;
282 my $entry = select_one($dbh,$sth->{select_bibtex_by_key},$bibtex_key);
283 if (not defined $entry) {
284 $bibtex_key =~ s/:.*$//;
285 $entry = select_one($dbh,$sth->{select_bibtex_by_approximate_key},$bibtex_key.'%');
291 my ($dbh,$sth,@bind_vals) = @_;
292 $sth->execute(@bind_vals) or
293 die "Unable to select one: ".$dbh->errstr();
294 my $results = $sth->fetchall_arrayref({});
296 return ref($results)?$results->[0]:undef;
299 sub parse_bibtex_file {
300 my ($file,$entries) = @_;
302 my $bibfile = Text::BibTeX::File->new($file)
303 or die "Unable to open $file for reading: $!";
306 while ($entry = Text::BibTeX::Entry->new($bibfile)) {
307 print STDERR "In Entry ".$entry->metatype() if $DEBUG;
308 if ($entry->metatype() == BTE_COMMENT) {
309 push @entry_comments,$entry->value();
310 } elsif ($entry->metatype() == BTE_REGULAR) {
311 my $entry_key = $entry->key();
312 if (not defined $entry_key) {
313 @entry_comments = ();
317 # if there is a file comment, use it as the file name
318 for my $comment (@entry_comments) {
319 next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i;
320 next unless length $1;
321 $entry_data{file_name} = $1.'.pdf';
324 my %field_prefix = (doi => 'doi://',
328 my %field_name = (doi => 'doi',
330 file => 'file_name',);
331 for my $field (qw(file doi html)) {
332 my $field_value = $entry->get($field);
333 if (defined $field_value and $field_value =~ /\S+/) {
334 $entry_data{$field_name{$field}} =
335 $field_prefix{$field}.$field_value if
336 not defined $entry_data{$field_name{$field}};
339 $entries->{$entry_key} = {} if not defined $entries->{$entry_key};
340 for my $field (keys %entry_data) {
341 $entries->{$entry_key}{$field} = $entry_data{$field} if
342 defined $entry_data{$field};
344 # reset the entry comments
345 @entry_comments = ();
349 print STDERR "\n" if $DEBUG;
355 sub initialize_database {
357 return open_cache($cache,1);
361 my ($cache,$initialize) = @_;
362 my $dbh = DBI->connect("dbi:SQLite:dbname=$cache","","") or
363 die "Unable to open/create database $cache";
365 $dbh->do("DROP TABLE IF EXISTS bibtex;");
366 $dbh->do("DROP TABLE IF EXISTS papers;");
368 CREATE TABLE bibtex (
369 bibtex_key TEXT PRIMARY KEY,
376 CREATE UNIQUE INDEX bibtex_file_name ON bibtex(file_name);
379 CREATE UNIQUE INDEX bibtex_bibtex_key ON bibtex(bibtex_key);
382 CREATE TABLE papers (
383 file_name TEXT PRIMARY KEY,
389 CREATE UNIQUE INDEX papers_path ON papers(path);
392 CREATE UNIQUE INDEX papers_file_name ON papers(file_name);
396 (insert_papers => <<'EOF',
397 INSERT OR REPLACE INTO papers(file_name,path,has_xoj) VALUES (?,?,?);
399 insert_bibtex => <<'EOF',
400 INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,doi,html) VALUES (?,?,?,?);
402 select_papers_by_name => <<'EOF',
403 SELECT * FROM papers WHERE file_name = ?;
405 select_papers_by_pmid => <<'EOF',
406 SELECT * FROM papers WHERE file_name LIKE ?;
408 select_papers_by_path => <<'EOF',
409 SELECT * FROM papers WHERE path = ?;
411 select_bibtex_by_key => <<'EOF',
412 SELECT * FROM bibtex WHERE bibtex_key = ?;
414 select_bibtex_by_approximate_key => <<'EOF',
415 SELECT * FROM bibtex WHERE bibtex_key LIKE ?;
417 select_bibtex_by_file_name => <<'EOF',
418 SELECT * FROM bibtex WHERE file_name = ?;
420 clear_papers_cache => <<'EOF',
423 clear_bibtex_cache => <<'EOF',
428 for my $key (keys %s) {
429 $st->{$key}=$dbh->prepare($s{$key}) //
430 die "Unable to prepare sql statement: ".$dbh->errstr;