class Octopress::Tags::Wrap::Tag

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/octopress-wrap-tag.rb, line 17
def initialize(tag_name, markup, tokens)
  super
  @og_markup = @markup = markup
  @tag_name = tag_name
end

Public Instance Methods

content_for(markup, context) click to toggle source
# File lib/octopress-wrap-tag.rb, line 104
def content_for(markup, context)
  @block_name = TagHelpers::ContentFor.get_block_name(@tag_name, markup)
  TagHelpers::ContentFor.render(context, @block_name).strip
end
error_msg(error) click to toggle source
# File lib/octopress-wrap-tag.rb, line 97
def error_msg(error)
  error.message
  message = "Wrap failed: {% #{@tag_name} #{@og_markup}%}."
  message << $1 if error.message =~ /%}\.(.+)/
  raise IOError.new message
end
render(context) click to toggle source
Calls superclass method
# File lib/octopress-wrap-tag.rb, line 23
def render(context)
  return unless markup = TagHelpers::Conditional.parse(@markup, context)

  if markup =~ TagHelpers::Var::HAS_FILTERS
    markup = $1
    filters = $2
  end

  type = if markup =~ /^\s*yield\s(.+)/
    markup = $1
    'yield'
  elsif markup =~ /^\s*render\s(.+)/
    markup = $1
    'render'
  elsif markup =~ /^\s*include\s(.+)/
    markup = $1
    'include'
  elsif markup =~ /^\s*return\s(.+)/
    markup = $1
    'return'
  else
    raise IOError.new "Wrap Failed: {% wrap #{@og_markup}%} - Which type of wrap: inlcude, yield, render? - Correct syntax: {% wrap type path or var [filters] [conditions] %}"
  end

  # Liquid 3 requires Tag.parse insated of Tag.new
  parse = Liquid::Tag.respond_to?(:parse)

  case type
  when 'yield'
    if parse
      content = Octopress::Tags::Yield::Tag.parse('yield', markup, [], {}).render(context)
    else
      content = Octopress::Tags::Yield::Tag.new('yield', markup, []).render(context)
    end
    return '' if content.nil?
  when 'return'
    if parse
      content = Octopress::Tags::ReturnTag::Tag.parse('return', markup, [], {}).render(context)
    else
      content = Octopress::Tags::ReturnTag::Tag.new('return', markup, []).render(context)
    end
    return '' if content.nil?
  when 'render'
    begin
      if parse
        content = Octopress::Tags::Render::Tag.parse('render', markup, [], {}).render(context)
      else
        content = Octopress::Tags::Render::Tag.new('render', markup, []).render(context)
      end
    rescue => error
      error_msg error
    end
  when 'include'
    if parse
      content = Octopress::Tags::Include::Tag.parse('include', markup, [], {}).render(context)
    else
      content = Octopress::Tags::Include::Tag.new('include', markup, []).render(context)
    end
  end

  # just in case yield had a value
  old_yield = context.scopes.first['yield']
  context.scopes.first['yield'] = content
  
  content = super.strip
  context.scopes.first['yield'] = old_yield

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

  content
end