App-PDFLibrarian

 view release on metacpan or  search on metacpan

lib/App/PDFLibrarian/Library.pm  view on Meta::CPAN

# Copyright (C) 2016--2026 Karl Wette
#
# This file is part of App::PDFLibrarian.
#
# App::PDFLibrarian is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# App::PDFLibrarian is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;

package App::PDFLibrarian::Library;
$App::PDFLibrarian::Library::VERSION = '6.2.0';
use parent 'Exporter';

use Carp;
use File::Copy;
use File::Find;
use File::Path;
use File::Spec;
use File::stat;
use FindBin qw($Script);
use Text::Unidecode;

use App::PDFLibrarian qw($pdflibrarydir);
use App::PDFLibrarian::BibTeX qw(bib_checksum format_bib_authors);
use App::PDFLibrarian::Util qw(is_in_dir remove_tex_markup remove_tex_markup_undef remove_short_words);

our @EXPORT_OK = qw(pdf_is_in_library update_pdf_lib make_pdf_links cleanup_links);

1;

sub pdf_is_in_library {
  my ($pdffile) = @_;

  # return whether PDF file is already in library
  return is_in_dir("$pdflibrarydir/Files", $pdffile);

}

sub update_pdf_lib {
  my (@bibentries) = @_;
  return unless @bibentries > 0;

  # add PDF files in BibTeX entries to PDF library
  my $newbibentries = 0;
  foreach my $bibentry (@bibentries) {

    # get name of PDF file
    my $pdffile = $bibentry->get('file');

    # record which PDF files are new to the library
    ++$newbibentries unless pdf_is_in_library($pdffile);

    # create name of PDF file in library:
    # - organise PDF files under '$pdflibrarydir/Files', then first uppercase character of key
    # - name PDF files after key with '.pdf' extension
    my $key = $bibentry->key;
    my $pdflibdir = File::Spec->catdir($pdflibrarydir, "Files", uc(substr($key, 0, 1)));
    my $pdflibfile = File::Spec->catfile($pdflibdir, "$key.pdf");

    # ensure directory for PDF file exists
    File::Path::make_path($pdflibdir);

    # move PDF file into library, including if library PDF filename has changed
    if ($pdffile ne $pdflibfile) {
      move($pdffile, $pdflibfile) or croak "$Script: could not move '$pdffile' to '$pdflibfile': $!";
      $pdffile = $pdflibfile;
    }

    # record new PDF filename
    $bibentry->set('file', $pdflibfile);

  }
  printf STDERR "$Script: added %i PDF files to '$pdflibrarydir/Files'\n", $newbibentries if $newbibentries > 0;

}

sub make_pdf_links {
  my (@bibentries) = @_;
  return unless @bibentries > 0;

  # find existing links
  my %existinglinks;
  {
    my $wanted = sub {
      if (-l $_) {
        my $pdffile = readlink($_);
        $existinglinks{$pdffile}->{$_} = 1 if defined($pdffile);
      }
    };
    find({wanted => \&$wanted, bydepth => 1, no_chdir => 1}, $pdflibrarydir);
  }

  # make symbolic links in PDF links directory to real PDF files
  my $count = 0;
  foreach my $bibentry (@bibentries) {

    # get name of PDF file
    my $pdffile = $bibentry->get('file');



( run in 2.886 seconds using v1.01-cache-2.11-cpan-9581c071862 )