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.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jekyll-conrefifier.rb', line 127

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



143
144
145
146
147
148
149
150
151
# File 'lib/jekyll-conrefifier.rb', line 143

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



153
154
155
156
157
158
159
160
161
# File 'lib/jekyll-conrefifier.rb', line 153

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