Class: Jekyll::PluginInclude::Tag

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

Constant Summary collapse

VARIABLE_SYNTAX =
/
   (?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)
   (?<params>.*)
/x

Instance Method Summary collapse

Constructor Details

#initialize(tagname, content, tokens) ⇒ Tag

Returns a new instance of Tag.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jekyll-plugin-include.rb', line 12

def initialize(tagname, content, tokens)
  super

  @attributes = { '_allow_override' => 'true' }
  content.scan(::Liquid::TagAttributes) do |key, value|
    # Strip quotes from around attribute values
    @attributes[key] = value.gsub(/^['"]|['"]$/, '')
  end

  gem_root = Gem::Specification.find_by_name(@attributes['_plugin']).gem_dir
  @gem_includes = File.join(gem_root, '_includes')
  @site_root = Jekyll.configuration({})['source']
  @site_includes_dir = File.join(
    @site_root,
    Jekyll.configuration({})['includes_dir']
  )
end

Instance Method Details

#findfile(path, filename) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jekyll-plugin-include.rb', line 77

def findfile(path, filename)
  file = nil

  path.each do |dir|
    testfile = File.join(dir, filename)
    if File.file?(testfile)
      file = testfile
      break
    end
  end

  return file
end

#render(context) ⇒ Object



30
31
32
33
34
35
36
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
62
63
64
# File 'lib/jekyll-plugin-include.rb', line 30

def render(context)
  searchpath = if @attributes['_allow_override'] == 'true'
                 [
                   @site_includes_dir,
                   @gem_includes,
                 ]
               else
                 [
                   @gem_includes,
                   @site_includes_dir,
                 ]
               end

  file = render_variable(
    context,
    @attributes['_file']
  ) || @attributes['_file']

  # Pass on the include params minus ours
  pass_params = @attributes
  pass_params.delete('_plugin')
  pass_params.delete('_file')
  pass_params.delete('_allow_override')
  context['include'] = pass_params

  file = findfile(searchpath, file)
  if file.nil?
    raise 'podcast_include could not find the file '\
      "\"#{@attributes['_file']}\"."
  else
    f = File.read(file, context.registers[:site].file_read_opts)
    partial = Liquid::Template.parse(f)
    partial.render!(context)
  end
end

#render_variable(context, var) ⇒ Object

Render the variable if required (@see goo.gl/N5sMV3)



67
68
69
70
71
72
73
74
75
# File 'lib/jekyll-plugin-include.rb', line 67

def render_variable(context, var)
  return unless var.match(VARIABLE_SYNTAX)

  partial = context.registers[:site]
                   .liquid_renderer
                   .file('(variable)')
                   .parse(var)
  return partial.render!(context)
end