Class: JsDuck::Images

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/images.rb

Overview

Looks up images from directories specified through –images option.

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ Images

Returns a new instance of Images.



8
9
10
11
# File 'lib/jsduck/images.rb', line 8

def initialize(paths)
  @paths = scan_for_images(paths)
  @images = {}
end

Instance Method Details

#add(filename) ⇒ Object

Adds relative image path of an image



28
29
30
31
32
# File 'lib/jsduck/images.rb', line 28

def add(filename)
  unless @images[filename]
    @images[filename] = true
  end
end

#copy(output_dir) ⇒ Object

Copys over images to given output dir



35
36
37
38
39
40
41
42
# File 'lib/jsduck/images.rb', line 35

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

#copy_img(img, output_dir) ⇒ Object

Attempts to copy one image, returns true on success



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jsduck/images.rb', line 45

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_unusedObject

Report unused images



62
63
64
65
66
67
68
# File 'lib/jsduck/images.rb', line 62

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

#scan_for_images(paths) ⇒ Object

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.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jsduck/images.rb', line 15

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