class Inkcite::Renderer::Property

Constants

DOLLAR
EQUALS
SLASH
VARIABLE_REGEX

Public Instance Methods

render(tag, opt, ctx) click to toggle source
# File lib/inkcite/renderer/property.rb, line 5
def render tag, opt, ctx

  html = ctx[tag]
  if html.nil?
    ctx.error 'Unknown tag or property', { :tag => tag, :opt => "[#{opt.to_query}]" }
    return nil
  end

  # True if this is a opening tag - e.g. {feature ...}, not {/feature}
  is_open_tag = !tag.starts_with?(SLASH)

  # When a custom opening tag is encountered, if there is a corresponding
  # closing tag, we'll save the options provided at open so that they
  # can be pop'd by the closing tag and used in the closing HTML.
  if is_open_tag

    # Verify that a closing tag has been defined and push the opts
    # onto the stack.  No need to push opts and pollute the stack if
    # there is no closing tag to take advantage of them.
    close_tag = "#{SLASH}#{tag}"
    ctx.tag_stack(tag) << opt unless ctx[close_tag].nil?

  else

    # Chop off the forward slash to reveal the original open tag.  Then
    # grab the tag stack for said open tag.  Pop the most recently provided
    # opts off the stack so those values are available again.
    open_tag = tag[1..-1]
    tag_stack = ctx.tag_stack(open_tag)

    # The provided opts take precedence over the ones passed to the open tag.
    if tag_stack

      # Need to verify that there are open opts to pop - if a tag is closed that
      # hasn't been opened (e.g. {h3}...{/h2}) the open_opt can be nil.
      open_opt = tag_stack.pop
      opt = open_opt.merge(opt) if open_opt

    end

  end

  # Need to clone the property - otherwise, we modify the original property.
  # Which is bad.
  html = html.clone

  Parser.each html, VARIABLE_REGEX do |pair|

    # Split the declaration on the equals sign.
    variable, default = pair.split(EQUALS, 2)

    # Check to see if the variable has been defined in the parameters.  If so, use that
    # value - otherwise, inherit the default.
    (opt[variable.to_sym] || default).to_s

  end

end