Sha256: 3ee74d793e2b4f14d1f0cc722e61e07d36aaadfe83427ac95d0db7e2e118cac4

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 KB

Contents

#
# Copyright (c) 2014-2018 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".
#

require 'review/extentions'
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
      end

      def dir_entries
        Dir.glob(File.join(@basedir, '**{,/*/**}/*.*')).uniq.sort
      end

      def add_entry(path)
        @entries << path unless @entries.include?(path)
        @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
              end
            end
          end
        end
        nil
      end

      def target_list(id)
        [
          # 1. <basedir>/<builder>/<chapid>/<id>.<ext>
          "#{@basedir}/#{@builder}/#{@chapid}/#{id}",

          # 2. <basedir>/<builder>/<chapid>-<id>.<ext>
          "#{@basedir}/#{@builder}/#{@chapid}-#{id}",

          # 3. <basedir>/<builder>/<id>.<ext>
          "#{@basedir}/#{@builder}/#{id}",

          # 4. <basedir>/<chapid>/<id>.<ext>
          "#{@basedir}/#{@chapid}/#{id}",

          # 5. <basedir>/<chapid>-<id>.<ext>
          "#{@basedir}/#{@chapid}-#{id}",

          # 6. <basedir>/<id>.<ext>
          "#{@basedir}/#{id}"
        ]
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
review-5.7.0 lib/review/book/image_finder.rb
review-5.6.0 lib/review/book/image_finder.rb
review-5.5.0 lib/review/book/image_finder.rb
review-5.4.0 lib/review/book/image_finder.rb
review-5.3.0 lib/review/book/image_finder.rb
review-5.2.0 lib/review/book/image_finder.rb
review-5.1.1 lib/review/book/image_finder.rb
review-5.1.0 lib/review/book/image_finder.rb
review-5.0.0 lib/review/book/image_finder.rb
review-4.2.0 lib/review/book/image_finder.rb
review-4.1.0 lib/review/book/image_finder.rb
review-4.0.0 lib/review/book/image_finder.rb
review-3.2.0 lib/review/book/image_finder.rb
review-3.1.0 lib/review/book/image_finder.rb
review-3.0.0 lib/review/book/image_finder.rb
review-3.0.0.rc1 lib/review/book/image_finder.rb
review-3.0.0.preview4 lib/review/book/image_finder.rb
review-3.0.0.preview3 lib/review/book/image_finder.rb
review-3.0.0.preview2 lib/review/book/image_finder.rb
review-3.0.0.preview1 lib/review/book/image_finder.rb