]> git.donarmstrong.com Git - bin.git/commitdiff
add search-by-pmid and --only-print options to bibtex_to_paper
authorDon Armstrong <don@donarmstrong.com>
Wed, 16 Apr 2014 23:26:47 +0000 (16:26 -0700)
committerDon Armstrong <don@donarmstrong.com>
Wed, 16 Apr 2014 23:26:47 +0000 (16:26 -0700)
bibtex_to_paper

index 23e0085b9478d527f8da39beec3ceffd08b6404d..77bfe0f0a63deccc9ef90f577a8864266f8bb63b 100755 (executable)
@@ -34,8 +34,10 @@ bibtex_to_paper [options] bibtexkey
   --bibtex, -b bibtex file to look up key in
   --bibtex-cache, -c bibtex cache file
   --build-cache, -B build cache using bibtex files
+  --search-by-pmid Search term is a pmid instead of a bibtex key
   --pdf-dir pdf directory
   --pdfviewer, -p pdf viewer to use
+  --only-print Only print PDF file name
   --debug, -d debugging level (Default 0)
   --help, -h display this help
   --man, -m display manual
@@ -57,6 +59,10 @@ Bibtex cache file; rebuilt if bibtex file changes
 PDF viewer to use; defaults to evince unless a .xoj exists, in which
 case xournal is used.
 
+=item B<--only-print>
+
+Only print the PDF file name, don't open it.
+
 =item B<--debug, -d>
 
 Debug verbosity. (Default 0)
@@ -83,6 +89,8 @@ use vars qw($DEBUG);
 my %options = (debug           => 0,
                help            => 0,
                man             => 0,
+               only_print      => 0,
+               search_by_pmid  => 0,
                'bibtex_cache'  => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
               );
 
@@ -91,6 +99,8 @@ GetOptions(\%options,
            'bibtex|b=s@',
            'bibtex_cache|bibtex-cache|c=s',
            'pdfviewer|p=s',
+           'only_print|only-print!',
+           'search_by_pmid|search-by-pmid!',
            'clear_cache|clear-cache!',
            'papers_directory|papers-directory=s@',
            'debug|d+','help|h|?','man|m');
@@ -200,7 +210,7 @@ sub load_bibtex_entries_into_database {
     my ($dbh,$sth,$entries) = @_;
     for my $entry (keys %{$entries}) {
         next unless defined $entries->{$entry};
-        $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name doi html)});
+        $sth->{insert_bibtex}->execute($entry,@{$entries->{$entry}}{qw(file_name pmid doi html)});
         $sth->{insert_bibtex}->finish();
         print STDERR "inserted $entry {".join(',',map {defined $_?"'$_'":"'undef'"} %{$entries->{$entry}})."}\n" if $DEBUG;
     }
@@ -211,7 +221,12 @@ sub open_bibtex_key {
     if (not defined $dbh) {
         open_entry($dbh,$sth,$entries->{$bibtex_key},$options);
     } else {
-        my $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
+        my $entry;
+        if ($options->{search_by_pmid}) {
+            $entry = select_entry_from_pmid($dbh,$sth,$bibtex_key);
+        } else {
+            select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
+        }
         p $entry if $DEBUG;
         open_entry($dbh,$sth,$entry,$options);
     }
@@ -242,7 +257,7 @@ sub open_pdf {
     if ($has_xoj) {
         fork_exec('xournal',$file_name);
     } else {
-        fork_exec('evince',$file_name)
+        fork_exec('xournal',$file_name)
     }
 }
 
@@ -266,22 +281,41 @@ sub open_entry{
         p $paper if $DEBUG;
         print STDERR $entry->{file_name} if $DEBUG;
         if (defined $paper) {
+            if ($options->{only_print}) {
+                print $paper->{path};
+                return;
+            }
             open_pdf($paper->{path},$options,$paper->{has_xoj});
             return;
         }
     }
     if (defined $entry->{doi}) {
+        if ($options->{only_print}) {
+            print $entry->{doi};
+            return;
+        }
         my $url = $entry->{doi};
         $url =~ s{^doi://}{http://dx.doi.org/};
         open_browser($url,$options);
         return;
     }
     if (defined $entry->{html}) {
+        if ($options->{only_print}) {
+            print $entry->{html};
+            return;
+        }
         open_browser($entry->{html},$options);
         return;
     }
 }
 
+sub select_entry_from_pmid{
+    my ($dbh,$sth,$pmid) = @_;
+
+    return select_one($dbh,$sth->{select_bibtex_by_pmid},$pmid);
+}
+
+
 sub select_entry_from_bibtex_key{
     my ($dbh,$sth,$bibtex_key) = @_;
 
@@ -330,11 +364,13 @@ sub parse_bibtex_file {
             my %field_prefix = (doi => 'doi://',
                                 html => 'http://',
                                 file => '',
+                                pmid => '',
                                );
             my %field_name = (doi => 'doi',
                               html => 'html',
+                              pmid => 'pmid',
                               file => 'file_name',);
-            for my $field (qw(file doi html)) {
+            for my $field (qw(file doi html pmid)) {
                 my $field_value = $entry->get($field);
                 if (defined $field_value and $field_value =~ /\S+/) {
                     $entry_data{$field_name{$field}} =
@@ -374,6 +410,7 @@ sub open_cache {
 CREATE TABLE bibtex (
 bibtex_key TEXT PRIMARY KEY,
 file_name TEXT,
+pmid TEXT,
 doi TEXT,
 html TEXT
 );
@@ -383,6 +420,9 @@ CREATE UNIQUE INDEX bibtex_file_name ON bibtex(file_name);
 EOF
         $dbh->do(<<EOF);
 CREATE UNIQUE INDEX bibtex_bibtex_key ON bibtex(bibtex_key);
+EOF
+        $dbh->do(<<EOF);
+CREATE INDEX bibtex_pmid ON bibtex(pmid);
 EOF
         $dbh->do(<<EOF);
 CREATE TABLE papers (
@@ -403,7 +443,7 @@ EOF
 INSERT OR REPLACE INTO papers(file_name,path,has_xoj) VALUES (?,?,?);
 EOF
          insert_bibtex => <<'EOF',
-INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,doi,html) VALUES (?,?,?,?);
+INSERT OR REPLACE INTO bibtex (bibtex_key,file_name,pmid,doi,html) VALUES (?,?,?,?,?);
 EOF
          select_papers_by_name => <<'EOF',
 SELECT * FROM papers WHERE file_name = ?;
@@ -422,6 +462,9 @@ SELECT * FROM bibtex WHERE bibtex_key LIKE ?;
 EOF
          select_bibtex_by_file_name => <<'EOF',
 SELECT * FROM bibtex WHERE file_name = ?;
+EOF
+         select_bibtex_by_pmid => <<'EOF',
+SELECT * FROM bibtex WHERE pmid = ?;
 EOF
          clear_papers_cache => <<'EOF',
 DELETE FROM papers;