]> git.donarmstrong.com Git - bin.git/blob - bibtex_to_paper
add start of bibtex to paper
[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 Text::BibTex;
17
18 use DB_File;
19 use MLDBM qw(DB_FILE Storable);
20 use Fcntl qw/O_RDWR O_CREAT O_TRUNC/;
21
22 =head1 NAME
23
24 bibtex_to_paper - opens the paper corresponding to a bibtex key
25
26 =head1 SYNOPSIS
27
28 bibtex_to_paper [options] bibtexkey
29
30  Options:
31   --bibtex, -b bibtex file to look up key in
32   --bibtex-cache, -c bibtex cache file
33   --build-cache, -B build cache using bibtex files
34   --pdf-dir pdf directory
35   --pdfviewer, -p pdf viewer to use
36   --debug, -d debugging level (Default 0)
37   --help, -h display this help
38   --man, -m display manual
39
40 =head1 OPTIONS
41
42 =over
43
44 =item B<--bibtex, -b>
45
46 Bibtex file to look key up in
47
48 =item B<--bibtex-cache, -c>
49
50 Bibtex cache file; rebuilt if bibtex file changes
51
52 =item B<--pdfviewer, -p>
53
54 PDF viewer to use; defaults to evince unless a .xoj exists, in which
55 case xournal is used.
56
57 =item B<--debug, -d>
58
59 Debug verbosity. (Default 0)
60
61 =item B<--help, -h>
62
63 Display brief usage information.
64
65 =item B<--man, -m>
66
67 Display this manual.
68
69 =back
70
71 =head1 EXAMPLES
72
73 bibtex_to_paper
74
75 =cut
76
77
78 use vars qw($DEBUG);
79
80 my %options = (debug           => 0,
81                help            => 0,
82                man             => 0,
83               );
84
85 GetOptions(\%options,
86            'build_cache|build-cache|B!',
87            'bibtex|b=s',
88            'bibtex_cache|bibtex-cache|c=s',
89            'pdfviewer|p=s',
90            'debug|d+','help|h|?','man|m');
91
92 pod2usage() if $options{help};
93 pod2usage({verbose=>2}) if $options{man};
94
95 $DEBUG = $options{debug};
96
97 my @USAGE_ERRORS;
98 if (not exists $options{bibtex} and
99     not exists $options{bibtex_cache}) {
100     push @USAGE_ERRORS,
101         "You must give at least one of --bibtex".
102         "or --bibtex-cache";
103 }
104
105 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
106
107 sub parse_bibtex_file {
108     my ($file,$entries) = @_;
109
110     my $bibfile = Text::BibTex::File->new($file)
111         or die "Unable to open $bibfile for reading: $!";
112     my @entry_comments;
113     my $entry;
114     while ($entry = Text::BibTex::Entry->new($bibfile)) {
115         if ($entry->metatype() eq 'BTE_COMMENT') {
116             push @entry_comments,$entry->value();
117         } elsif ($entry->metatype() eq 'BTE_REGULAR') {
118             my $entry_key = $entry->key();
119             my $link_name;
120             if (defined $entry_key) {
121                 # if there is a file comment, use it as the file name
122                 for my $comment (@entry_comments) {
123                     next unless $comment =~ /^\s*file(?:name)?:?\s*(.+?)\s*$/i;
124                     $link_name = $1;
125                     last;
126                 }
127                 # if there is a file key, use that as the file name
128                 # if there is a doi, then use that
129                 # if there is a html, then use that
130                 if (not defined $link_name) {
131                     my @possible_fields = $entry->get(qw(file doi html));
132                     for my $possible_field (@possible_fields) {
133                         if (defined $possible_field and length $possible_field) {
134                             $link_name = $possible_field;
135                             last;
136                         }
137                     }
138                 }
139             }
140             if (not exists $entries->{$entry_key} or
141                 defined $link_name;
142                ) {
143                 $entries->{$entry_key} = $link_name
144             }
145             # reset the entry comments
146             @entry_comments = ();
147         } else {
148             # do nothing
149         }
150     }
151 }
152
153
154 sub initialize_database {
155     my ($cache) = @_;
156     return open_cache($cache,1);
157 }
158
159 sub open_cache {
160     my ($cache,$initialize) @_;
161     my $open_flags = O_RDWR|O_CREAT;
162     if ($initialize) {
163         $open_flags = $open_flags | O_TRUNC;
164     }
165
166     my %entries;
167     tie %entries, MLDBM => $cache
168         $open_flags, 0666
169         or die "Unable to create create/truncate $cache: $!";
170     return \%entries;
171 }
172
173
174 __END__