Class: Jekyll::VibrantJS::VibrantJSTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tagName, image_arg, tokens) ⇒ VibrantJSTag

Returns a new instance of VibrantJSTag.



5
6
7
8
# File 'lib/jekyll-vibrantjs/jekyll-vibrantjs-tag.rb', line 5

def initialize(tagName, image_arg, tokens)
  super
  @image_arg = image_arg
end

Instance Method Details

#render(context) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll-vibrantjs/jekyll-vibrantjs-tag.rb', line 10

def render(context)
  # if we pass in something like "post.image.feature" instead of a path,
  # the variable name gets passed in as a string, so we can look it up in the context
  image_path = "#{context[@image_arg.strip]}"

  if File.exist?(image_path)
    script_path = File.expand_path('./call-vibrant.js', File.dirname(__FILE__))
    node_modules_path = File.join Dir.pwd, "node_modules"

    stdout, status = Open3.capture2({"NODE_PATH" => node_modules_path}, 'node', script_path, image_path)
    
    raise "Failed to call vibrantjs" unless status.success?
  else
    raise "Given image file `#{image_path}` doesn't exist"
  end

  @palette = eval(stdout)
  
  tmpl_path = File.join Dir.pwd, "_includes", "vibrantjs-css.html"
  unless File.exist?(tmpl_path)
    tmpl_path = File.expand_path('./vibrantjs-css.html', File.dirname(__FILE__))
  end

  tmpl = File.read tmpl_path
  site = context.registers[:site]
  tmpl = (Liquid::Template.parse tmpl).render site.site_payload.merge!({
    "rgbVibrant" => @palette["Vibrant"],
    "rgbLightVibrant" => @palette["LightVibrant"],
    "rgbDarkVibrant" => @palette["DarkVibrant"],
    "rgbMuted" => @palette["Muted"],
    "rgbLightMuted" => @palette["LightMuted"],
    "rgbDarkMuted" => @palette["DarkMuted"]
  })
end