Class: JekyllIncludeWithFrontmatter

Inherits:
Jekyll::Tags::IncludeTag
  • Object
show all
Defined in:
lib/jekyll-include-with-frontmatter.rb,
lib/jekyll-include-with-frontmatter/version.rb

Overview

Implement includes that can handle frontmatter.

Direct Known Subclasses

JekyllIncludeRelativeWithFrontmatter

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#load_cached_partial(path, context) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jekyll-include-with-frontmatter.rb', line 37

def load_cached_partial(path, context)
  context.registers[:cached_partials] ||= {}
  cached_partial = context.registers[:cached_partials]

  unless cached_partial.key?(path)
    unparsed_file = context.registers[:site]
                           .liquid_renderer
                           .file(path)
    begin
      file_data = read_file(path, context)
      if (matchdata = file_data.match(/(---\n.*---\n)(.*)/m))
        params = YAML.safe_load(matchdata[1])
        cached_partial[path] = [unparsed_file.parse(matchdata[2]), params]
      else
        cached_partial[path] = [unparsed_file.parse(file_data), {}]
      end
    rescue Liquid::Error => e
      e.template_name = path
      e.markup_context = 'included ' if e.markup_context.nil?
      raise e
    end
  end

  cached_partial[path]
end

#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
# File 'lib/jekyll-include-with-frontmatter.rb', line 10

def render(context)
  site = context.registers[:site]

  file = render_variable(context) || @file
  validate_file_name(file)

  path = locate_include_file(context, file, site.safe)
  return unless path

  add_include_to_dependency(site, path, context)

  partial, include_params = load_cached_partial(path, context)

  context.stack do
    parsed_params = @params ? parse_params(context) : nil

    context['include'] = parsed_params ? include_params.merge(parsed_params) : include_params
    begin
      partial.render!(context)
    rescue Liquid::Error => e
      e.template_name = path
      e.markup_context = 'included ' if e.markup_context.nil?
      raise e
    end
  end
end