#!/usr/bin/perl # bibtex_to_paper opens the paper corresponding to a bibtex key # and is released under the terms of the GNU GPL version 3, or any # later version, at your option. See the file README and COPYING for # more information. # Copyright 2014 by Don Armstrong . use warnings; use strict; use Getopt::Long; use Pod::Usage; use File::Find; use Text::BibTex; use DB_File; use MLDBM qw(DB_FILE Storable); use Fcntl qw/O_RDWR O_CREAT O_TRUNC/; =head1 NAME bibtex_to_paper - opens the paper corresponding to a bibtex key =head1 SYNOPSIS bibtex_to_paper [options] bibtexkey Options: --bibtex, -b bibtex file to look up key in --bibtex-cache, -c bibtex cache file --build-cache, -B build cache using bibtex files --pdf-dir pdf directory --pdfviewer, -p pdf viewer to use --debug, -d debugging level (Default 0) --help, -h display this help --man, -m display manual =head1 OPTIONS =over =item B<--bibtex, -b> Bibtex file to look key up in =item B<--bibtex-cache, -c> Bibtex cache file; rebuilt if bibtex file changes =item B<--pdfviewer, -p> PDF viewer to use; defaults to evince unless a .xoj exists, in which case xournal is used. =item B<--debug, -d> Debug verbosity. (Default 0) =item B<--help, -h> Display brief usage information. =item B<--man, -m> Display this manual. =back =head1 EXAMPLES bibtex_to_paper =cut use vars qw($DEBUG); my %options = (debug => 0, help => 0, man => 0, ); GetOptions(\%options, 'build_cache|build-cache|B!', 'bibtex|b=s', 'bibtex_cache|bibtex-cache|c=s', 'pdfviewer|p=s', 'debug|d+','help|h|?','man|m'); pod2usage() if $options{help}; pod2usage({verbose=>2}) if $options{man}; $DEBUG = $options{debug}; my @USAGE_ERRORS; if (not exists $options{bibtex} and not exists $options{bibtex_cache}) { push @USAGE_ERRORS, "You must give at least one of --bibtex". "or --bibtex-cache"; } pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS; sub parse_bibtex_file { my ($file,$entries) = @_; my $bibfile = Text::BibTex::File->new($file) or die "Unable to open $bibfile for reading: $!"; my @entry_comments; my $entry; while ($entry = Text::BibTex::Entry->new($bibfile)) { if ($entry->metatype() eq 'BTE_COMMENT') { push @entry_comments,$entry->value(); } elsif ($entry->metatype() eq 'BTE_REGULAR') { my $entry_key = $entry->key(); my $link_name; if (defined $entry_key) { # if there is a file comment, use it as the file name for my $comment (@entry_comments) { next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i; $link_name = $1; last; } # if there is a file key, use that as the file name # if there is a doi, then use that # if there is a html, then use that if (not defined $link_name) { my @possible_fields = $entry->get(qw(file doi html)); for my $possible_field (@possible_fields) { if (defined $possible_field and length $possible_field) { $link_name = $possible_field; last; } } } } if (not exists $entries->{$entry_key} or defined $link_name; ) { $entries->{$entry_key} = $link_name } # reset the entry comments @entry_comments = (); } else { # do nothing } } } sub initialize_database { my ($cache) = @_; return open_cache($cache,1); } sub open_cache { my ($cache,$initialize) @_; my $open_flags = O_RDWR|O_CREAT; if ($initialize) { $open_flags = $open_flags | O_TRUNC; } my %entries; tie %entries, MLDBM => $cache $open_flags, 0666 or die "Unable to create create/truncate $cache: $!"; return \%entries; } __END__