Sha256: a959961fae2c24aedf751d13b4bea662d0129b476225c518e5a6df7f05d3a846

Contents?: true

Size: 1.77 KB

Versions: 31

Compression:

Stored size: 1.77 KB

Contents

require "jsduck/logger"
require "fileutils"

module JsDuck

  # Looks up images from directories specified through --images option.
  class Images
    def initialize(paths)
      @paths = scan_for_images(paths)
      @images = {}
    end

    # Scans each path for image files, building a hash of paths where
    # each path points to a hash of image files found in that path.
    def scan_for_images(paths)
      map = {}
      paths.each do |path|
        # Scans directory for image files
        map[path] = {}
        Dir[path+"/**/*.{png,jpg,jpeg,gif}"].each do |img|
          map[path][img] = false
        end
      end
      map
    end

    # Adds relative image path of an image
    def add(filename)
      unless @images[filename]
        @images[filename] = true
      end
    end

    # Copys over images to given output dir
    def copy(output_dir)
      @images.each_key do |img|
        unless copy_img(img, output_dir)
          Logger.instance.warn(:image, "Image not found.", img)
        end
      end
      report_unused
    end

    # Attempts to copy one image, returns true on success
    def copy_img(img, output_dir)
      @paths.each_pair do |path, map|
        filename = path + "/" + img
        if map.has_key?(filename)
          dest = output_dir + "/" + img
          Logger.instance.log("Copying image", dest)
          FileUtils.makedirs(File.dirname(dest))
          FileUtils.cp(filename, dest)
          # mark file as used.
          map[filename] = true
          return true
        end
      end
      return false
    end

    # Report unused images
    def report_unused
      @paths.each_pair do |path, map|
        map.each_pair do |img, used|
          Logger.instance.warn(:image_unused, "Image not used.", img) unless used
        end
      end
    end

  end

end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
jsduck-4.0.1 lib/jsduck/images.rb
jsduck-4.0.0 lib/jsduck/images.rb
jsduck-3.11.2 lib/jsduck/images.rb
jsduck-4.0.beta2 lib/jsduck/images.rb
jsduck-3.11.1 lib/jsduck/images.rb
jsduck-4.0.beta lib/jsduck/images.rb
jsduck-3.11.0 lib/jsduck/images.rb
jsduck-3.10.5 lib/jsduck/images.rb
jsduck-3.10.4 lib/jsduck/images.rb
jsduck-3.10.3 lib/jsduck/images.rb
jsduck-3.10.2 lib/jsduck/images.rb
jsduck-3.10.1 lib/jsduck/images.rb
jsduck-3.10.0 lib/jsduck/images.rb
jsduck-3.9.1 lib/jsduck/images.rb
jsduck-3.9.0 lib/jsduck/images.rb
jsduck-3.9.0.pre lib/jsduck/images.rb
jsduck-3.8.4 lib/jsduck/images.rb
jsduck-3.8.3 lib/jsduck/images.rb
jsduck-3.8.2 lib/jsduck/images.rb
jsduck-3.8.1 lib/jsduck/images.rb