]> git.donarmstrong.com Git - bin.git/blob - bibtex_to_paper
add clear cache command
[bin.git] / bibtex_to_paper
1 #!/usr/bin/perl
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
5 # more information.
6 # Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 use File::Find;
16 use File::Basename qw(basename);
17 use File::Spec qw(rel2abs);
18 use Text::BibTeX;
19 use User;
20 use Data::Printer;
21
22 use DBI;
23
24 =head1 NAME
25
26 bibtex_to_paper - opens the paper corresponding to a bibtex key
27
28 =head1 SYNOPSIS
29
30 bibtex_to_paper [options] bibtexkey
31
32  Options:
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
41
42 =head1 OPTIONS
43
44 =over
45
46 =item B<--bibtex, -b>
47
48 Bibtex file to look key up in
49
50 =item B<--bibtex-cache, -c>
51
52 Bibtex cache file; rebuilt if bibtex file changes
53
54 =item B<--pdfviewer, -p>
55
56 PDF viewer to use; defaults to evince unless a .xoj exists, in which
57 case xournal is used.
58
59 =item B<--debug, -d>
60
61 Debug verbosity. (Default 0)
62
63 =item B<--help, -h>
64
65 Display brief usage information.
66
67 =item B<--man, -m>
68
69 Display this manual.
70
71 =back
72
73 =head1 EXAMPLES
74
75 bibtex_to_paper
76
77 =cut
78
79
80 use vars qw($DEBUG);
81
82 my %options = (debug           => 0,
83                help            => 0,
84                man             => 0,
85                'bibtex_cache'  => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
86               );
87
88 GetOptions(\%options,
89            'build_cache|build-cache!',
90            'bibtex|b=s@',
91            'bibtex_cache|bibtex-cache|c=s',
92            'pdfviewer|p=s',
93            'clear_cache|clear-cache!',
94            'papers_directory|papers-directory=s@',
95            'debug|d+','help|h|?','man|m');
96
97 pod2usage() if $options{help};
98 pod2usage({verbose=>2}) if $options{man};
99
100 $DEBUG = $options{debug};
101
102 my @USAGE_ERRORS;
103 if (not exists $options{bibtex} and
104     not exists $options{bibtex_cache}) {
105     push @USAGE_ERRORS,
106         "You must give at least one of --bibtex".
107         "or --bibtex-cache";
108 }
109
110 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
111
112 main();
113
114 sub main{
115
116     my $dbh;
117     my $sth;
118     if (exists $options{bibtex_cache}) {
119         my $initialize = 0;
120         if (-e $options{bibtex_cache}) {
121             ($dbh,$sth) = open_cache($options{bibtex_cache});
122         } else {
123             ($dbh,$sth) = initialize_database($options{bibtex_cache});
124         }
125     }
126
127     if (exists $options{clear_cache}) {
128         clear_cache($dbh,$sth);
129     }
130     my %entries;
131     if (exists $options{build_cache}) {
132         $options{bibtex} //= [];
133         $options{bibtex} =
134             [@ARGV,
135              @{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]},
136             ];
137         @ARGV = ();
138     }
139     if (exists $options{bibtex}) {
140         for my $bibtex_file (@{ref $options{bibtex}?$options{bibtex}:[$options{bibtex}]}) {
141             parse_bibtex_file($bibtex_file,\%entries);
142         }
143     }
144
145     if (exists $options{papers_directory} and
146         defined $dbh
147        ) {
148         load_papers_into_database($dbh,$sth,$options{papers_directory});
149     }
150
151     p %entries if $DEBUG;
152     if (keys %entries and
153         defined $dbh) {
154         load_bibtex_entries_into_database($dbh,$sth,\%entries);
155     }
156
157     p @ARGV if $DEBUG;
158     for my $bibtex_key (@ARGV) {
159         open_bibtex_key(\%options,$dbh,$sth,\%entries,$bibtex_key);
160     }
161
162 }
163
164 sub clear_cache {
165     my ($dbh,$sth) = @_;
166     $sth->{clear_papers_cache}->execute();
167     $sth->{clear_bibtex_cache}->execute();
168 }
169
170 sub load_papers_into_database {
171     my ($dbh,$sth,$dir) = @_;
172
173     my @dirs = ref($dir)?@{$dir}:$dir;
174
175     my $actually_load_it = sub {
176         return unless /\.pdf$/;
177         my $xoj = 0;
178         if (-e "${_}.xoj") {
179             $xoj = 1;
180         }
181         insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
182     };
183
184     my @pdfs;
185     find($actually_load_it,@dirs);
186 }
187
188 sub insert_or_replace_papers {
189     my ($dbh,$sth,$file_name,$file_loc,$has_xoj) = @_;
190     $sth->{insert_papers}->execute($file_name,$file_loc,$has_xoj);
191     $sth->{insert_papers}->finish();
192 }
193
194 sub load_bibtex_entries_into_database {
195     my ($dbh,$sth,$entries) = @_;
196     for my $entry (keys %{$entries}) {
197         next unless defined $entries->{$entry};
198         $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name doi html)});
199         $sth->{insert_bibtex}->finish();
200         print STDERR "inserted $entry {".join(',',map {defined $_?"'$_'":"'undef'"} %{$entries->{$entry}})."}\n" if $DEBUG;
201     }
202 }
203
204 sub open_bibtex_key {
205     my ($options,$dbh,$sth,$entries,$bibtex_key) = @_;
206     if (not defined $dbh) {
207         open_entry($dbh,$sth,$entries->{$bibtex_key},$options);
208     } else {
209         my $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
210         p $entry if $DEBUG;
211         open_entry($dbh,$sth,$entry,$options);
212     }
213 }
214
215 sub fork_exec {
216     my (@cmd) = @_;
217     my $child = fork();
218     if (not defined $child) {
219         die "Unable to fork for some reason: $!";
220     }
221     if ($child == 0) {
222         exec(@cmd);
223     } else {
224         return $child;
225     }
226
227 }
228
229 sub open_pdf {
230     my ($file_name,$options,$has_xoj) = @_;
231     print STDERR "opening $file_name\n" if $DEBUG;
232     if ($has_xoj) {
233         fork_exec('xournal',$file_name);
234     } else {
235         fork_exec('evince',$file_name)
236     }
237 }
238
239 sub open_browser{
240     my ($file) = @_;
241     fork_exec('sensible-browser',$file);
242 }
243
244 sub open_entry{
245     my ($dbh,$sth,$entry,$options) = @_;
246
247     return unless defined $entry and ref $entry and keys %{$entry};
248     if (defined $entry->{file_name} and length $entry->{file_name}) {
249         my $paper = select_one($dbh,$sth->{select_papers_by_name},$entry->{file_name});
250         p $paper if $DEBUG;
251         print STDERR $entry->{file_name} if $DEBUG;
252         if (defined $paper) {
253             open_pdf($paper->{path},$options,$paper->{has_xoj});
254             return;
255         }
256     }
257     if (defined $entry->{doi}) {
258         my $url = $entry->{doi};
259         $url =~ s{^doi://}{http://dx.doi.org/};
260         open_browser($url,$options);
261         return;
262     }
263     if (defined $entry->{html}) {
264         open_browser($entry->{html},$options);
265         return;
266     }
267 }
268
269 sub select_entry_from_bibtex_key{
270     my ($dbh,$sth,$bibtex_key) = @_;
271
272     my $entry = select_one($dbh,$sth->{select_bibtex_by_key},$bibtex_key);
273     if (not defined $entry) {
274         $bibtex_key =~ s/:.*$//;
275         $entry = select_one($dbh,$sth->{select_bibtex_by_approximate_key},$bibtex_key.'%');
276     }
277     return $entry;
278 }
279
280 sub select_one{
281     my ($dbh,$sth,@bind_vals) = @_;
282     $sth->execute(@bind_vals) or
283         die "Unable to select one: ".$dbh->errstr();
284     my $results = $sth->fetchall_arrayref({});
285     $sth->finish();
286     return ref($results)?$results->[0]:undef;
287 }
288
289 sub parse_bibtex_file {
290     my ($file,$entries) = @_;
291
292     my $bibfile = Text::BibTeX::File->new($file)
293         or die "Unable to open $file for reading: $!";
294     my @entry_comments;
295     my $entry;
296     while ($entry = Text::BibTeX::Entry->new($bibfile)) {
297         print STDERR "In Entry ".$entry->metatype() if $DEBUG;
298         if ($entry->metatype() == BTE_COMMENT) {
299             push @entry_comments,$entry->value();
300         } elsif ($entry->metatype() == BTE_REGULAR) {
301             my $entry_key = $entry->key();
302             if (not defined $entry_key) {
303                 @entry_comments = ();
304                 next;
305             }
306             my %entry_data;
307             # if there is a file comment, use it as the file name
308             for my $comment (@entry_comments) {
309                 next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i;
310                 next unless length $1;
311                 $entry_data{file_name} = $1.'.pdf';
312                 last;
313             }
314             my %field_prefix = (doi => 'doi://',
315                                 html => 'http://',
316                                 file => '',
317                                );
318             my %field_name = (doi => 'doi',
319                               html => 'html',
320                               file => 'file_name',);
321             for my $field (qw(file doi html)) {
322                 my $field_value = $entry->get($field);
323                 if (defined $field_value and $field_value =~ /\S+/) {
324                     $entry_data{$field_name{$field}} =
325                         $field_prefix{$field}.$field_value if
326                         not defined $entry_data{$field_name{$field}};
327                 }
328             }
329             $entries->{$entry_key} = {} if not defined $entries->{$entry_key};
330             for my $field (keys %entry_data) {
331                 $entries->{$entry_key}{$field} = $entry_data{$field} if
332                     defined $entry_data{$field};
333             }
334             # reset the entry comments
335             @entry_comments = ();
336         } else {
337             # do nothing
338         }
339         print STDERR "\n" if $DEBUG;
340     }
341     return $entries;
342 }
343
344
345 sub initialize_database {
346     my ($cache) = @_;
347     return open_cache($cache,1);
348 }
349
350 sub open_cache {
351     my ($cache,$initialize) = @_;
352     my $dbh = DBI->connect("dbi:SQLite:dbname=$cache","","") or
353         die "Unable to open/create database $cache";
354     if ($initialize) {
355         $dbh->do("DROP TABLE IF EXISTS bibtex;");
356         $dbh->do("DROP TABLE IF EXISTS papers;");
357         $dbh->do(<<EOF);
358 CREATE TABLE bibtex (
359 bibtex_key TEXT PRIMARY KEY,
360 file_name TEXT,
361 doi TEXT,
362 html TEXT
363 );
364 EOF
365         $dbh->do(<<EOF);
366 CREATE UNIQUE INDEX bibtex_file_name ON bibtex(file_name);
367 EOF
368         $dbh->do(<<EOF);
369 CREATE UNIQUE INDEX bibtex_bibtex_key ON bibtex(bibtex_key);
370 EOF
371         $dbh->do(<<EOF);
372 CREATE TABLE papers (
373 file_name TEXT PRIMARY KEY,
374 path TEXT,
375 has_xoj BOOLEAN
376 );
377 EOF
378         $dbh->do(<<EOF);
379 CREATE UNIQUE INDEX papers_path ON papers(path);
380 EOF
381         $dbh->do(<<EOF);
382 CREATE UNIQUE INDEX papers_file_name ON papers(file_name);
383 EOF
384     }
385     my %s =
386         (insert_papers => <<'EOF',
387 INSERT OR REPLACE INTO papers(file_name,path,has_xoj) VALUES (?,?,?);
388 EOF
389          insert_bibtex => <<'EOF',
390 INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,doi,html) VALUES (?,?,?,?);
391 EOF
392          select_papers_by_name => <<'EOF',
393 SELECT * FROM papers WHERE file_name = ?;
394 EOF
395          select_papers_by_path => <<'EOF',
396 SELECT * FROM papers WHERE path = ?;
397 EOF
398          select_bibtex_by_key => <<'EOF',
399 SELECT * FROM bibtex WHERE bibtex_key = ?;
400 EOF
401          select_bibtex_by_approximate_key => <<'EOF',
402 SELECT * FROM bibtex WHERE bibtex_key LIKE ?;
403 EOF
404          select_bibtex_by_file_name => <<'EOF',
405 SELECT * FROM bibtex WHERE file_name = ?;
406 EOF
407          clear_papers_cache => <<'EOF',
408 DELETE FROM papers;
409 EOF
410          clear_bibtex_cache => <<'EOF',
411 DELETE FROM bibtex;
412 EOF
413         );
414     my $st;
415     for my $key (keys %s) {
416         $st->{$key}=$dbh->prepare($s{$key}) //
417             die "Unable to prepare sql statement: ".$dbh->errstr;
418     }
419     return ($dbh,$st);
420 }
421
422
423 __END__