class Octopress::Tags::Include::Tag

Constants

PLUGIN_SYNTAX

Attributes

filters[RW]
path[RW]
tag_markup[R]
tag_name[R]

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/octopress-include-tag.rb, line 15
def initialize(tag_name, markup, tokens)
  @tag_markup = markup
  @tag_name = tag_name

  if matched = markup.strip.match(PLUGIN_SYNTAX)
    @plugin = matched['plugin'].strip
    @path = matched['path'].strip
  end

  # Trigger Jekyll's Include tag with compatible markup
  #
  super(tag_name, safe_markup(markup).join(' '), tokens)
end

Public Instance Methods

parse_markup(context) click to toggle source

Parses special markup, handling vars, conditions, and filters Returns:

- include path or nil if markup conditionals evaluate false
# File lib/octopress-include-tag.rb, line 82
def parse_markup(context)
  # If conditional statements are present, only continue if they are true
  #
  return unless markup = TagHelpers::Conditional.parse(tag_markup, context)

  # If there are filters, store them for use later and strip them out of markup
  #
  if matched = markup.match(TagHelpers::Var::HAS_FILTERS)
    markup = matched['markup']
    @filters = matched['filters']
  end

  # If there is a ternary expression, replace it with the true result
  #
  markup = TagHelpers::Var.evaluate_ternary(markup, context)

  # Paths may be variables, check context to retrieve proper path
  #
  markup = TagHelpers::Path.parse(markup, context)

  markup
end
render(context) click to toggle source
Calls superclass method
# File lib/octopress-include-tag.rb, line 46
def render(context)

  # Parse special markup until markup is simplified
  return unless markup = parse_markup(context)

  # If markup references a plugin e.g. plugin-name:include-file.html
  #
  if matched = markup.strip.match(PLUGIN_SYNTAX)

    # Call Octopress Ink to render the plugin's include file
    #
    content = render_ink_include(matched['plugin'], matched['path'], context)
    
  else

    # use Jekyll's default include tag
    #
    # Why safe_markup again? In initialize we didn't know what the path would be becuase
    # we needed the context to parse vars and conditions. Now that we know them, we'll
    # reset @file and @params as intended in order to render with Jekyll's include tag.
    #
    @file, @params = safe_markup(markup)
    content = super(context).strip
  end

  unless content.nil? || filters.nil?
    content = TagHelpers::Var.render_filters(content, filters, context)
  end

  content
end
render_ink_include(plugin, file, context) click to toggle source

Call Octopress Ink to render the plugin’s include file

# File lib/octopress-include-tag.rb, line 107
def render_ink_include(plugin, file, context)
  begin
    content = Octopress::Ink::Plugins.include(plugin, path).read
  rescue => error
    msg = "Include failed: {% #{tag_name} #{tag_markup}%}.\n"
    if !defined?(Octopress::Ink)
      msg += "To include plugin partials, first install Octopress Ink."
    else
      msg += "The plugin '#{plugin}' does not have an include named '#{path}'."
    end
    raise IOError.new(msg)
  end

  partial = Liquid::Template.parse(content)

  context.stack {
    context['include'] = parse_params(context)
    context['plugin'] = Octopress::Ink::Plugins.plugin(plugin).config(context['lang'])
    partial.render!(context)
  }.strip
end
safe_markup(markup) click to toggle source

Strip specials out of markup so that it is suitable for Jekyll include tag

# File lib/octopress-include-tag.rb, line 31
def safe_markup(markup)
  file = markup.strip.match(/\S+/)[0]
  params = []

  markup.scan(Regexp.compile('('+VALID_SYNTAX.to_s+')+')).each do |param|
    params << param.first
  end

  if matched = tag_markup.match(VARIABLE_SYNTAX)
    file = matched['variable']
  end

  [file, params.join(' ')]
end