lib/review/book/image_finder.rb in review-5.7.0 vs lib/review/book/image_finder.rb in review-5.8.0

- old
+ new

@@ -1,7 +1,7 @@ # -# Copyright (c) 2014-2018 Minero Aoki, Kenshi Muto, Masayoshi Takahashi +# Copyright (c) 2014-2023 Minero Aoki, Kenshi Muto, Masayoshi Takahashi # # This program is free software. # You can distribute or modify this program under the terms of # the GNU LGPL, Lesser General Public License version 2.1. # For details of LGPL, see the file "COPYING". @@ -11,38 +11,53 @@ require 'review/exception' module ReVIEW module Book class ImageFinder - def initialize(basedir, chapid, builder, exts) - @basedir = basedir - @chapid = chapid - @builder = builder - @exts = exts - @entries = dir_entries + def initialize(chapter) + @book = chapter.book + @basedir = @book.imagedir + @chapid = chapter.id + @builder = @book.config['builder'] + @entries = dir_entries.map { |path| entry_object(path) } end + def entry_object(path) + { path: path, basename: path.sub(/\.[^.]+$/, ''), downcase: path.sub(/\.[^.]+$/, $&.downcase) } + end + def dir_entries - Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort + Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort.map { |entry| entry.sub(%r{^\./}, '') } end def add_entry(path) - @entries << path unless @entries.include?(path) + path.sub!(%r{^\./}, '') + unless @entries.find { |entry| entry[:path] == path } + @entries << entry_object(path) + end @entries end def find_path(id) targets = target_list(id) targets.each do |target| - @exts.each do |ext| - @entries.find do |entry| - downname = entry.sub(/\.[^.]+$/, File.extname(entry).downcase) - if downname == "#{target}#{ext}" - return entry + @book.image_types.each do |ext| + entries = @entries.select do |entry| + entry[:basename] == target + end + + unless entries + break + end + + entries.find do |entry| + if entry[:downcase] == "#{target}#{ext}" + return entry[:path] end end end end + nil end def target_list(id) [