Class: Jekyll::DataRenderTag

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ DataRenderTag

Returns a new instance of DataRenderTag.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/jekyll-conrefifier.rb', line 180

def initialize(tag_name, text, tokens)
  super

  keys = text.strip.split('.', 3)
  keys.shift(2) # this is just site.data
  paren_arg = keys.last.match(/\((.+?)\)/)
  unless paren_arg.nil?
    last_key = keys.last.sub(paren_arg[0], '')
    keys.pop
    keys << last_key
    @hash_args = paren_arg[1].gsub(/[{}:]/,'').split(', ').map{|h| h1,h2 = h.split('=>'); {h1.strip => eval(h2.strip)}}.reduce(:merge)
  end
  @keys = keys
  @id = keys.join('-')
end

Instance Method Details

#fetch_datafile(context, keys) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/jekyll-conrefifier.rb', line 196

def fetch_datafile(context, keys)
  data_file = context.registers[:site].data["data_render_#{@id}"]
  return data_file unless data_file.nil?

  path = @id.gsub('.', '/')
  data_source = File.join(context.registers[:site].source, context.registers[:site].config['data_source'])
  data_file = File.read("#{data_source}/#{path}.yml")
  context.registers[:site].data["data_render_#{@id}"] = data_file
end

#render(context) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/jekyll-conrefifier.rb', line 206

def render(context)
  datafile = fetch_datafile(context, @keys)

  config = { 'page' => @hash_args }
  config = { 'site' => { "data" => context.registers[:site].data } }.merge(config)
  versioned_yaml = SafeYAML.load(Liquid::Template.parse(datafile).render(config))
  context.registers[:site].data['data_render'] = versioned_yaml
  nil
end