| 1 | # Copyright (C) 2016--2026 Karl Wette | ||
| 2 | # | ||
| 3 | # This file is part of App::PDFLibrarian. | ||
| 4 | # | ||
| 5 | # App::PDFLibrarian is free software: you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation, either version 3 of the License, or (at | ||
| 8 | # your option) any later version. | ||
| 9 | # | ||
| 10 | # App::PDFLibrarian is distributed in the hope that it will be useful, but | ||
| 11 | # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | # General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | use v5.18; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use utf8; | ||
| 22 | use open qw(:std :encoding(UTF-8)); | ||
| 23 | |||
| 24 | package App::PDFLibrarian; | ||
| 25 | # ABSTRACT: Manage a library of academic papers in PDF format with embedded BibTeX metadata | ||
| 26 | $App::PDFLibrarian::VERSION = '6.3.4'; | ||
| 27 | use parent 'Exporter'; | ||
| 28 | |||
| 29 | use Carp; | ||
| 30 | use Config::IniFiles; | ||
| 31 | use File::BaseDir; | ||
| 32 | use File::Path; | ||
| 33 | use File::Spec; | ||
| 34 | use FindBin qw($Script); | ||
| 35 | use Text::BibTeX; | ||
| 36 | |||
| 37 | =pod | ||
| 38 | |||
| 39 | =head1 NAME | ||
| 40 | |||
| 41 | B<App::PDFLibrarian> - Manage a library of academic papers in PDF format with embedded BibTeX metadata. | ||
| 42 | |||
| 43 | =head1 INSTALLATION | ||
| 44 | |||
| 45 | Requires the following packages: | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item * Debian, Ubuntu: | ||
| 50 | |||
| 51 | apt install cpanminus ghostscript libwx-perl libxml2-dev libxslt1-dev perl-base poppler-utils xdg-utils zlib1g-dev | ||
| 52 | |||
| 53 | =back | ||
| 54 | |||
| 55 | Then install from CPAN: | ||
| 56 | |||
| 57 | cpanm App::PDFLibrarian | ||
| 58 | |||
| 59 | =head1 APPLICATIONS | ||
| 60 | |||
| 61 | =over 4 | ||
| 62 | |||
| 63 | =item * B<pdf-lbr-import-pdf> | ||
| 64 | |||
| 65 | =item * B<pdf-lbr-edit-bib> | ||
| 66 | |||
| 67 | =item * B<pdf-lbr-output-bib> | ||
| 68 | |||
| 69 | =item * B<pdf-lbr-output-key> | ||
| 70 | |||
| 71 | =item * B<pdf-lbr-replace-pdf> | ||
| 72 | |||
| 73 | =item * B<pdf-lbr-remove-pdf> | ||
| 74 | |||
| 75 | =item * B<pdf-lbr-rebuild-links> | ||
| 76 | |||
| 77 | =item * B<pdf-lbr-iso4-abbr> | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | =head1 COPYRIGHT AND LICENSE | ||
| 82 | |||
| 83 | Copyright (C) 2016--2026 Karl Wette. Licensed under the GNU General Public License, version 3 or later. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | our $cfgdir; | ||
| 88 | our $pdflibrarydir; | ||
| 89 | our $pref_query_database; | ||
| 90 | our %bibtex_macros; | ||
| 91 | our %default_filter; | ||
| 92 | our %default_output_text_format; | ||
| 93 | our %query_databases; | ||
| 94 | |||
| 95 | our @EXPORT_OK = qw($cfgdir $pdflibrarydir $pref_query_database %bibtex_macros %default_filter %default_output_text_format %query_databases); | ||
| 96 | |||
| 97 | 1; | ||
| 98 | |||
| 99 | INIT { | ||
| 100 | |||
| 101 | # check for user home directory | ||
| 102 | croak "$Script: could not determine user home directory" unless defined($ENV{HOME}) && -d $ENV{HOME}; | ||
| 103 | |||
| 104 | # create configuration directory | ||
| 105 | $cfgdir = File::BaseDir->config_home("pdflibrarian"); | ||
| 106 | File::Path::make_path($cfgdir); | ||
| 107 | |||
| 108 | # read configuration file | ||
| 109 | my $cfgfile = File::Spec->catfile($cfgdir, "pdflibrarian.ini"); | ||
| 110 | my $cfg = Config::IniFiles->new(); | ||
| 111 | if (-f $cfgfile) { | ||
| 112 | $cfg->SetFileName($cfgfile); | ||
| 113 | $cfg->ReadConfig(); | ||
| 114 | } | ||
| 115 | |||
| 116 | # ensure default configuration values are set | ||
| 117 | { | ||
| 118 | my %default_config = | ||
| 119 | ( | ||
| 120 | 'general.pdflibrarydir' => File::Spec->catdir($ENV{HOME}, 'PDFLibrary'), | ||
| 121 | 'general.prefquery' => 'Astrophysics Data System using Digital Object Identifier', | ||
| 122 | 'general.default_filter' => 'keyword=d abstract=d', | ||
| 123 | 'query-ads doi.name' => 'Astrophysics Data System using Digital Object Identifier', | ||
| 124 | 'query-ads doi.cmd' => 'pdf-lbr-query-ads --query doi:%s', | ||
| 125 | 'query-ads arxiv.name' => 'Astrophysics Data System using arXiv Article Identifier', | ||
| 126 | 'query-ads arxiv.cmd' => 'pdf-lbr-query-ads --query arxiv:%s', | ||
| 127 | 'output-text-format.article' => '%author:fvlj, %title, %journal %volume, %pages (%year).', | ||
| 128 | ); | ||
| 129 | while (my ($section_key, $value) = each %default_config) { | ||
| 130 | my ($section, $key) = split /[.]/, $section_key; | ||
| 131 | if ($cfg->exists($section, $key)) { | ||
| 132 | if (length($cfg->val($section, $key)) == 0) { | ||
| 133 | $cfg->setval($section, $key, $value); | ||
| 134 | } | ||
| 135 | } else { | ||
| 136 | $cfg->newval($section, $key, $value); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | # ensure configuration file exists | ||
| 142 | $cfg->WriteConfig($cfgfile); | ||
| 143 | |||
| 144 | # set PDF library directory | ||
| 145 | $pdflibrarydir = $cfg->val('general', 'pdflibrarydir'); | ||
| 146 | File::Path::make_path($pdflibrarydir); | ||
| 147 | |||
| 148 | # set query database | ||
| 149 | $pref_query_database = $cfg->val('general', 'prefquery'); | ||
| 150 | foreach my $section ($cfg->GroupMembers('query-ads')) { | ||
| 151 | my $name = $cfg->val($section, 'name'); | ||
| 152 | my $cmd = $cfg->val($section, 'cmd'); | ||
| 153 | if ($cmd =~ /^[^%]+[%]s[^%]*$/) { | ||
| 154 | $query_databases{$name} = $cmd; | ||
| 155 | } else { | ||
| 156 | croak "$Script: invalid query command '$cmd' for database '$name'"; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # create default field filter for printed BibTeX output | ||
| 161 | foreach my $arg (split /\s+/, $cfg->val('general', 'default_filter')) { | ||
| 162 | my ($bibfield, $spec) = split(/\s*=\s*/, $arg, 2); | ||
| 163 | $default_filter{$bibfield} = $spec; | ||
| 164 | } | ||
| 165 | |||
| 166 | # read in BibTeX macros to define by default | ||
| 167 | foreach my $key ($cfg->Parameters('macros')) { | ||
| 168 | my $macro = lc($key); | ||
| 169 | $bibtex_macros{$macro} = $cfg->val('macros', $key); | ||
| 170 | } | ||
| 171 | |||
| 172 | # create default formats for plain text output | ||
| 173 | foreach my $key ($cfg->Parameters('output-text-format')) { | ||
| 174 | my $bibtype = lc($key); | ||
| 175 | $default_output_text_format{$bibtype} = $cfg->val('output-text-format', $key); | ||
| 176 | } | ||
| 177 | |||
| 178 | } |