]> git.donarmstrong.com Git - bin.git/blobdiff - bibtex_to_paper
add reset usb bus command
[bin.git] / bibtex_to_paper
index 77bfe0f0a63deccc9ef90f577a8864266f8bb63b..a6350eb98caedfecffcac5d9b248c5ac41760660 100755 (executable)
@@ -91,6 +91,8 @@ my %options = (debug           => 0,
                man             => 0,
                only_print      => 0,
                search_by_pmid  => 0,
+               search_by_file  => 0,
+               use_git         => 1,
                'bibtex_cache'  => File::Spec->catfile(User->Home,'.bibtex_to_paper_cache'),
               );
 
@@ -99,8 +101,10 @@ GetOptions(\%options,
            'bibtex|b=s@',
            'bibtex_cache|bibtex-cache|c=s',
            'pdfviewer|p=s',
+           'use_git|use-git!',
            'only_print|only-print!',
            'search_by_pmid|search-by-pmid!',
+           'search_by_file|search-by-file!',
            'clear_cache|clear-cache!',
            'papers_directory|papers-directory=s@',
            'debug|d+','help|h|?','man|m');
@@ -156,13 +160,17 @@ sub main{
     if (exists $options{papers_directory} and
         defined $dbh
        ) {
+        $dbh->begin_work;
         load_papers_into_database($dbh,$sth,$options{papers_directory});
+        $dbh->commit;
     }
 
     p %entries if $DEBUG;
     if (keys %entries and
         defined $dbh) {
+        $dbh->begin_work;
         load_bibtex_entries_into_database($dbh,$sth,\%entries);
+        $dbh->commit;
     }
 
     p @ARGV if $DEBUG;
@@ -183,21 +191,27 @@ sub load_papers_into_database {
 
     my @dirs = ref($dir)?@{$dir}:$dir;
 
-    my $actually_load_it = sub {
-        if (/\.git/) {
-            $File::Find::prune = 1;
-            return;
-        }
-        return unless /\.pdf$/;
-        my $xoj = 0;
-        if (-e "${_}.xoj") {
-            $xoj = 1;
+    if ($options{use_git}) {
+        my @files = grep /\.pdf\"?$/, split /\n/, qx(git ls-tree HEAD -r --full-name --name-only);
+        for my $file  (@files) {
+            $file =~ s/^\"(.+)\"$/"qq($1)"/gee;
+            insert_or_replace_papers($dbh,$sth,basename($file),File::Spec->rel2abs($file), -e "${file}.xoj");
         }
-        insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
-    };
-
-    my @pdfs;
-    find($actually_load_it,@dirs);
+    } else {
+        my $actually_load_it = sub {
+            if (/\.git/) {
+                $File::Find::prune = 1;
+                return;
+            }
+            return unless /\.pdf$/;
+            my $xoj = 0;
+            if (-e "${_}.xoj") {
+                $xoj = 1;
+            }
+            insert_or_replace_papers($dbh,$sth,basename($File::Find::name),File::Spec->rel2abs($_),$xoj);
+        };
+        find($actually_load_it,@dirs);
+    }
 }
 
 sub insert_or_replace_papers {
@@ -224,8 +238,10 @@ sub open_bibtex_key {
         my $entry;
         if ($options->{search_by_pmid}) {
             $entry = select_entry_from_pmid($dbh,$sth,$bibtex_key);
+        } elsif ($options->{search_by_file}) {
+            $entry = select_entry_from_file($dbh,$sth,$bibtex_key);
         } else {
-            select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
+            $entry = select_entry_from_bibtex_key($dbh,$sth,$bibtex_key);
         }
         p $entry if $DEBUG;
         open_entry($dbh,$sth,$entry,$options);
@@ -254,11 +270,11 @@ sub fork_exec {
 sub open_pdf {
     my ($file_name,$options,$has_xoj) = @_;
     print STDERR "opening $file_name\n" if $DEBUG;
-    if ($has_xoj) {
-        fork_exec('xournal',$file_name);
-    } else {
-        fork_exec('xournal',$file_name)
+    my $pdf_viewer = 'xournal';
+    if (exists $options->{pdfviewer} and defined $options->{pdfviewer}) {
+        $pdf_viewer = $options->{pdfviewer};
     }
+    fork_exec($pdf_viewer,$file_name);
 }
 
 sub open_browser{
@@ -270,6 +286,10 @@ sub open_entry{
     my ($dbh,$sth,$entry,$options) = @_;
 
     return unless defined $entry and ref $entry and keys %{$entry};
+    if ($DEBUG) {
+        print STDERR "Entry: \n";
+        p $entry;
+    }
     if (defined $entry->{file_name} and length $entry->{file_name}) {
         my $paper = select_one($dbh,$sth->{select_papers_by_name},$entry->{file_name});
         if (not defined $paper) {
@@ -287,6 +307,8 @@ sub open_entry{
             }
             open_pdf($paper->{path},$options,$paper->{has_xoj});
             return;
+        } else {
+            print STDERR "Unable to find paper\n" if $DEBUG;
         }
     }
     if (defined $entry->{doi}) {
@@ -315,15 +337,23 @@ sub select_entry_from_pmid{
     return select_one($dbh,$sth->{select_bibtex_by_pmid},$pmid);
 }
 
+sub select_entry_from_file{
+    my ($dbh,$sth,$filename) = @_;
+
+    return select_one($dbh,$sth->{select_bibtex_by_file_name_like},'%'.$filename.'%');
+}
+
 
 sub select_entry_from_bibtex_key{
     my ($dbh,$sth,$bibtex_key) = @_;
 
     my $entry = select_one($dbh,$sth->{select_bibtex_by_key},$bibtex_key);
     if (not defined $entry) {
+        print STDERR "Unable to find entry by exact search\n" if $DEBUG;
         $bibtex_key =~ s/:.*$//;
         $entry = select_one($dbh,$sth->{select_bibtex_by_approximate_key},$bibtex_key.'%');
     }
+    print STDERR "Found entry\n" if $DEBUG and defined $entry;
     return $entry;
 }
 
@@ -449,6 +479,9 @@ EOF
 SELECT * FROM papers WHERE file_name = ?;
 EOF
          select_papers_by_pmid => <<'EOF',
+SELECT * FROM papers JOIN bibtex ON papers.file_name = bibtex.file_name WHERE bibtex.pmid = ?;
+EOF
+         select_papers_by_name_like => <<'EOF',
 SELECT * FROM papers WHERE file_name LIKE ?;
 EOF
          select_papers_by_path => <<'EOF',
@@ -462,6 +495,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_file_name_like => <<'EOF',
+SELECT * FROM bibtex WHERE file_name LIKE ?;
 EOF
          select_bibtex_by_pmid => <<'EOF',
 SELECT * FROM bibtex WHERE pmid = ?;