Class: Jekyll::JekyllRetinamagick::GeneratedImageFile

Inherits:
StaticFile
  • Object
show all
Defined in:
lib/jekyll-retinamagick.rb

Instance Method Summary collapse

Constructor Details

#initialize(site, base, dir, name, preset, retina) ⇒ GeneratedImageFile

Initialize a new GeneratedImage.

+site+ is the Site
+base+ is the String path to the <source>
+dir+ is the String path between <source> and the file
+name+ is the String filename of the file
+preset+ is the Preset hash from the config.

Returns <GeneratedImageFile>



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jekyll-retinamagick.rb', line 15

def initialize(site, base, dir, name, preset, retina)
  @site = site
  @base = base
  @dir  = dir
  @name = name
  @dst_dir = preset.delete('destination')
  @src = File.join(@base, preset.delete('source'), name)
  @commands = preset
  if retina
    filepath, extension = name.match(/(.+)(\.[a-zA-Z]{3,4})/i).captures
    @name = "#{filepath}@2x#{extension}"
    if @commands.has_key?("resize")
      size = @commands["resize"]
      width, height = size.match(/([0-9]+)x([0-9]+)/i).captures
      @commands["resize"] = "#{Integer(width) * 2}x#{Integer(height) * 2}"
    end
  end
end

Instance Method Details

#pathObject

Obtains source file path by substituting the preset’s source directory for the destination directory.

Returns source file path.



38
39
40
# File 'lib/jekyll-retinamagick.rb', line 38

def path
  @src
end

#write(dest) ⇒ Object

Use MiniMagick to create a derivative image at the destination specified (if the original is modified).

+dest+ is the String path to the destination dir

Returns false if the file was not modified since last time (no-op).



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll-retinamagick.rb', line 47

def write(dest)
  dest_path = destination(dest)

  return false if File.exist? dest_path

  @@mtimes[path] = mtime

  cache_path = File.join(@base, ".minimagick-cache", @dst_dir, @name)

  FileUtils.mkdir_p(File.dirname(cache_path))
  FileUtils.mkdir_p(File.dirname(dest_path))

  # If the file isn't cached, generate it
  if not (File.size? cache_path and File.stat(cache_path).mtime.to_i > mtime)
    image = ::MiniMagick::Image.open(path)
    @commands.each_pair do |command, arg|
      image.send command, arg
    end
    image.write cache_path
  end

  FileUtils.cp(cache_path, dest_path)
  
  true
end