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) ⇒ 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
# File 'lib/jekyll-retinamagick.rb', line 15

def initialize(site, base, dir, name, preset)
  @site = site
  @base = base
  @dir  = dir
  @name = name
  @dst_dir = preset.delete('destination')
  @src_dir = preset.delete('source')
  @commands = preset
end

Instance Method Details

#pathObject

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

Returns source file path.



29
30
31
# File 'lib/jekyll-retinamagick.rb', line 29

def path
  File.join(@base, @dir.sub(@dst_dir, @src_dir), @name)
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).



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jekyll-retinamagick.rb', line 38

def write(dest)
  dest_path = destination(dest)

  return false if File.exist? dest_path and !modified?

  @@mtimes[path] = mtime

  FileUtils.mkdir_p(File.dirname(dest_path))
  image = ::MiniMagick::Image.open(path)
  @commands.each_pair do |command, arg|
    image.resize arg
    @size = arg
  end
  image.write dest_path

  width, height = @size.match(/([0-9]+)x([0-9]+)/i).captures
  retinaSize = "#{Integer(width) * 2}x#{Integer(height) * 2}"
  filepath, extension = dest_path.match(/(.+)(\.[a-zA-Z]{3,4})/i).captures
  dest_path = "#{filepath}@2x#{extension}"
  retinaImage = ::MiniMagick::Image.open(path)
  retinaImage.resize retinaSize
  retinaImage.write dest_path

  true
end