From: Don Armstrong Date: Fri, 24 Jan 2014 02:51:26 +0000 (-0800) Subject: add start of bibtex to paper X-Git-Url: https://git.donarmstrong.com/?p=bin.git;a=commitdiff_plain;h=5a31ea9ccd52baa23e09fc7b3f7495658805fefe add start of bibtex to paper --- diff --git a/bibtex_to_paper b/bibtex_to_paper index 7474ccb..f6ac661 100755 --- a/bibtex_to_paper +++ b/bibtex_to_paper @@ -13,7 +13,11 @@ use Getopt::Long; use Pod::Usage; use File::Find; -use Text::Bibtex; +use Text::BibTex; + +use DB_File; +use MLDBM qw(DB_FILE Storable); +use Fcntl qw/O_RDWR O_CREAT O_TRUNC/; =head1 NAME @@ -101,12 +105,70 @@ if (not exists $options{bibtex} and 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 + 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__