class Jekyll::PluginInclude::Tag

Constants

VARIABLE_SYNTAX

Public Class Methods

new(tagname, content, tokens) click to toggle source
Calls superclass method
# 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

Public Instance Methods

findfile(path, filename) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# 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