Class: Alula::Plugins::ImageAsset

Inherits:
GenericAsset
  • Object
show all
Defined in:
lib/alula/plugins/assets.rb

Instance Method Summary collapse

Methods inherited from GenericAsset

default, type

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ ImageAsset

Returns a new instance of ImageAsset.



55
56
57
58
59
60
61
62
# File 'lib/alula/plugins/assets.rb', line 55

def initialize(tag_name, markup, tokens)
  /(?<src>(?:https?:\/\/|\/|\S+\/)\S+)(?<title>\s+.+)/ =~ markup
  /(?:"|')(?<title>[^"']+)?(?:"|')\s+(?:"|')(?<alt>[^"']+)?(?:"|')/ =~ title
  
  @name = src
  @title = title
  @alt = alt
end

Instance Method Details

#render(context) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/alula/plugins/assets.rb', line 64

def render(context)
  asset_path = context.registers[:site].config["asset_path"]
  manifest = context.registers[:site].config["manifest"]
  
  asset = File.join(asset_path, manifest.assets[@name])
  
  # Fetch image size
  img = Magick::Image.read(File.join("public", asset)).first
  width = img.columns
  height = img.rows
  
  "<img src=\"#{asset}\" alt=\"#{@alt}\" title=\"#{@title}\" width=\"#{width}\" height=\"#{height}\">"
end