Class: Jekyll::ThumbTag

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, _) ⇒ ThumbTag

Returns a new instance of ThumbTag.



12
13
14
15
# File 'lib/jekyll/thumb/tag.rb', line 12

def initialize(tag_name, markup, _)
  @markup = markup
  super
end

Instance Attribute Details

#markupObject

Returns the value of attribute markup.



6
7
8
# File 'lib/jekyll/thumb/tag.rb', line 6

def markup
  @markup
end

Class Method Details

.optipng?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/jekyll/thumb/tag.rb', line 8

def self.optipng?
  @optinpng ||= system("which optipng")
end

Instance Method Details

#cache_dir(site) ⇒ Object



49
50
51
# File 'lib/jekyll/thumb/tag.rb', line 49

def cache_dir(site)
  config(site)['cache']
end

#config(site) ⇒ Object



41
42
43
# File 'lib/jekyll/thumb/tag.rb', line 41

def config(site)
  site.config['thumb'] || {}
end

#generate_image(site, src, attrs) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jekyll/thumb/tag.rb', line 53

def generate_image(site, src, attrs)
  cache = cache_dir(site)
  
  sha = cache && Digest::SHA1.hexdigest(attrs.sort.inspect + File.read(File.join(site.source, src)) + (optimize?(site) ? "optimize" : ""))
  if sha
    if File.exists?(File.join(cache, sha))
      img_attrs = JSON.parse(File.read(File.join(cache,sha,"json")))
      filename = img_attrs["src"].sub(/^\//, '')
      dest = File.join(site.dest, filename)
      FileUtils.mkdir_p(File.dirname(dest))
      FileUtils.cp(File.join(cache,sha,"img"), dest)

      site.config['keep_files'] << filename unless site.config['keep_files'].include?(filename)

      return img_attrs
    end
  end
  original_img_path = File.join(site.source, src)
  original_img = Vips::Image.new_from_file original_img_path
  
  img_attrs = {}
  
  if attrs['width']
    scale = attrs['width'].to_f / original_img.width
    attrs['height'] = original_img.height * scale
  elsif attrs['height']
    scale = attrs['height'].to_f / original_img.height
    attrs['width'] = original_img.width * scale
  else
    raise 'must specify either width or height'
  end

  img_attrs["height"] = attrs["height"].to_i if attrs["height"]
  img_attrs["width"]  = attrs["width"].to_i  if attrs["width"]
  img_attrs["src"] = src.sub(/(\.\w+)$/, "-#{img_attrs["width"]}w" + '\1')

  filename = img_attrs["src"].sub(/^\//, '')
  dest = File.join(site.dest, filename)
  
  FileUtils.mkdir_p(File.dirname(dest))

  unless File.exist?(dest)
    thumb = Vips::Image.thumbnail original_img_path, img_attrs["width"]
    thumb.write_to_file(dest, strip: true)
    if dest.match(/\.png$/) && optimize?(site) && self.class.optipng?
      `optipng #{dest}`
    end
  end
  site.config['keep_files'] << filename unless site.config['keep_files'].include?(filename)
  # Keep files around for incremental builds in Jekyll 3
  site.regenerator.add(filename) if site.respond_to?(:regenerator)

  if sha
    FileUtils.mkdir_p(File.join(cache, sha))
    FileUtils.cp(dest, File.join(cache, sha, "img"))
    File.open(File.join(cache, sha, "json"), "w") do |f|
      f.write(JSON.generate(img_attrs))
    end
  end

  img_attrs
end

#optimize?(site) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/jekyll/thumb/tag.rb', line 45

def optimize?(site)
  config(site)['optipng']
end

#parse_options(markup, context) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll/thumb/tag.rb', line 29

def parse_options(markup, context)
  options = {}
  markup.scan(/(\w+)=((?:"[^"]+")|(?:'[^']+')|[\w\.\_-]+)/) do |key,value|
    if (value[0..0] == "'" && value[-1..-1]) == "'" || (value[0..0] == '"' && value[-1..-1] == '"')
      options[key] = value[1..-2]
    else
      options[key] = context[value]
    end
  end
  options
end

#render(context) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll/thumb/tag.rb', line 17

def render(context)
  options = parse_options(markup, context)

  return "Bad options to thumb_tag, syntax is: {% thumb_tag src=\"image.png\" width=\"100\"}" unless options["src"]
  return "Error resizing - can't set both width and height" if options["width"] && options["height"]

  site = context.registers[:site]
  img_attrs = generate_image(site, options["src"], options)

  %Q{<a href="#{options['src']}" target="_blank" class="thumb"><img #{options.merge(img_attrs).map {|k,v| "#{k}=\"#{v}\""}.join(" ")}></a>}
end