class JsDuck::Img::Dir
Looks up images from a directory.
Public Class Methods
new(full_path, relative_path)
click to toggle source
# File lib/jsduck/img/dir.rb, line 9 def initialize(full_path, relative_path) @full_path = full_path @relative_path = relative_path @images = {} end
Public Instance Methods
all_used()
click to toggle source
Returns all used images.
# File lib/jsduck/img/dir.rb, line 34 def all_used @images.values end
get(filename)
click to toggle source
Retrieves hash of information for a given relative image filename. It will have the fields:
-
:filename - the same as the parameter of this method
-
:full_path - actual path in the filesystem.
-
:relative_path - relative path to be used inside <img> tag.
-
:width - Image width
-
:height - Image height
When the image is not found, returns nil.
# File lib/jsduck/img/dir.rb, line 25 def get(filename) img = scan_img(filename) if img @images[filename] = img end img end
report_unused()
click to toggle source
Print warnings about all unused images.
# File lib/jsduck/img/dir.rb, line 39 def report_unused scan_for_unused_images.each {|img| warn_unused(img) } end
Private Instance Methods
img_record(filename)
click to toggle source
# File lib/jsduck/img/dir.rb, line 69 def img_record(filename) full_path = File.join(@full_path, filename) width, height = Dimensions.dimensions(full_path) return { :filename => filename, :relative_path => File.join(@relative_path, filename), :full_path => full_path, :width => width, :height => height, } end
relative_path(dir_path, file_path)
click to toggle source
Given a path to directory and a path to file, returns the path to this file relative to the given dir. For example:
base_path("/foo/bar", "/foo/bar/baz/img.jpg") --> "baz/img.jpg"
# File lib/jsduck/img/dir.rb, line 87 def relative_path(dir_path, file_path) file_path.slice(dir_path.length+1, file_path.length) end
scan_for_unused_images()
click to toggle source
Scans directory for image files, building a hash of image files found in that directory.
# File lib/jsduck/img/dir.rb, line 56 def scan_for_unused_images unused = [] ::Dir[@full_path+"/**/*.{png,jpg,jpeg,gif}"].each do |path| filename = relative_path(@full_path, path) unused << img_record(filename) unless @images[filename] end unused end
scan_img(filename)
click to toggle source
# File lib/jsduck/img/dir.rb, line 45 def scan_img(filename) full_path = File.join(@full_path, filename) if File.exists?(File.join(@full_path, filename)) img_record(filename) else nil end end
warn_unused(img)
click to toggle source
# File lib/jsduck/img/dir.rb, line 65 def warn_unused(img) Logger.warn(:image_unused, "Image not used.", {:filename => img[:full_path]}) end