class Octopress::Tags::Render::Tag

Constants

SYNTAX

Attributes

filters[RW]
raw[R]
tag_markup[R]
tag_name[R]

Public Class Methods

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

  @tag_markup = markup
  @tag_name = tag_name

  if matched = markup.strip.match(/^(\s*raw\s)?(.+?)(\sraw\s*)?$/)
    @tag_markup = $2
    @raw = true unless $1.nil? and $3.nil?
  end
end

Public Instance Methods

parse_convertible(content, context) click to toggle source
# File lib/octopress-render-tag.rb, line 126
def parse_convertible(content, context)
  page = Partial.new(context.registers[:site], @path, content)
  page.render({})
  page.output.strip
end
parse_markup(context) click to toggle source

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

- render tag path or nil if markup conditionals evaluate false
# File lib/octopress-render-tag.rb, line 78
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
parse_params(context) click to toggle source
# File lib/octopress-render-tag.rb, line 65
def parse_params(context)
  if Jekyll::Tags::IncludeTag.respond_to?(:parse)
    include_tag = Jekyll::Tags::IncludeTag.parse('include', @markup, [], {})
  else
    include_tag = Jekyll::Tags::IncludeTag.new('include', @markup, [])
  end
  include_tag.parse_params(context)
end
read(markup, context) click to toggle source
# File lib/octopress-render-tag.rb, line 116
def read(markup, context)
  path = markup.match(SYNTAX)[1]
  @path = TagHelpers::Path.expand(path, context)
  begin
    File.open(@path).read
  rescue
    raise IOError.new "Render failed: {% #{@tag_name} #{@og_markup}%}. The file '#{path}' could not be found at #{@path}."
  end
end
render(context) click to toggle source
# File lib/octopress-render-tag.rb, line 29
def render(context)
  return unless @markup = parse_markup(context)

  content = read(@markup, context)

  if matched = content.match(/\A-{3}(?<vars>.+[^\A])-{3}\n(?<content>.+)/m)
    local_vars = SafeYAML.load(matched['vars'].strip)
    content = matched['content'].strip
  end

  if raw
    content
  else

    content = strip_raw(content)

    partial = Liquid::Template.parse(content)
    content = context.stack {
      context['include'] = parse_params(context)
      if local_vars
        context['page'] = Jekyll::Utils.deep_merge_hashes(context['page'], local_vars)
      end
      partial.render!(context)
    }.strip

    content = replace_raw(content)
    content = parse_convertible(content, context).strip

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

    content
  end
end
replace_raw(content) click to toggle source
# File lib/octopress-render-tag.rb, line 111
def replace_raw(content)
  @raw_content.each { |k, v| content.sub!(k, v) }
  content
end
strip_raw(content) click to toggle source
# File lib/octopress-render-tag.rb, line 101
def strip_raw(content)
  @raw_content = {}
  content.gsub /{%\s*raw\s*%}(.+?){% endraw %}/m do
    data = $1
    key = Digest::MD5.hexdigest(data)
    @raw_content[key] = "{% raw %}#{data}{% endraw %}"
    key
  end
end