Class: Jekyll::ResponsiveImage::Tag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/responsive_image.rb

Constant Summary collapse

DEFAULT_QUALITY =
85

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Tag

Returns a new instance of Tag.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jekyll/responsive_image.rb', line 9

def initialize(tag_name, markup, tokens)
  super

  @attributes = {}
  @markup = markup

  @markup.scan(::Liquid::TagAttributes) do |key, value|
    # Strip quotes from around attribute values
    @attributes[key] = value.gsub(/^['"]|['"]$/, '')
  end
end

Instance Method Details

#ensure_output_dir_exists!(dir) ⇒ Object



77
78
79
80
81
82
# File 'lib/jekyll/responsive_image.rb', line 77

def ensure_output_dir_exists!(dir)
  unless Dir.exists?(dir)
    Jekyll.logger.info "Creating output directory #{dir}"
    Dir.mkdir(dir)
  end
end

#needs_resizing?(img, width) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/jekyll/responsive_image.rb', line 73

def needs_resizing?(img, width)
  img.columns > width
end

#render(context) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jekyll/responsive_image.rb', line 84

def render(context)
  config = context.registers[:site].config['responsive_image']
  config['output_dir'] ||= 'assets/resized'
  config['sizes'] ||= []

  @attributes['template'] ||= config['template']
  @attributes['resized'] = resize_image(@attributes['path'], config)

  partial = File.read(@attributes['template'])
  template = Liquid::Template.parse(partial)

  template.render!(@attributes)
end

#resize_image(path, config) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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/responsive_image.rb', line 21

def resize_image(path, config)
  sizes = config['sizes']

  return if sizes.empty?

  output_dir = config['output_dir']
  ensure_output_dir_exists!(output_dir)

  resized = []
  img = Magick::Image::read(path).first

  sizes.each do |size|
    width = size['width']
    ratio = width.to_f / img.columns.to_f
    height = (img.rows.to_f * ratio).round

    filename = resized_filename(path, width, height)
    newpath = "#{output_dir}/#{filename}"

    next unless needs_resizing?(img, width)

    resized.push({
      'width'  => width,
      'height' => height,
      'path'   => newpath,
    })

    # Don't resize images more than once
    next if File.exists?(newpath)

    Jekyll.logger.info "Generating #{newpath}"

    i = img.scale(ratio)
    i.write(newpath) do |f|
      f.quality = size['quality'] || DEFAULT_QUALITY
    end

    i.destroy!
  end

  resized
end

#resized_filename(path, width, height) ⇒ Object

Insert resize information into a file path

resized_filename(/foo/bar/file.name.jpg, 500, 300)
  => /foo/bar/file.name-500x300.jpg


69
70
71
# File 'lib/jekyll/responsive_image.rb', line 69

def resized_filename(path, width, height)
  File.basename(path).sub(/\.([^.]+)$/, "-#{width}x#{height}.\\1")
end