class Inkcite::Renderer::Footnote

Public Instance Methods

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

  # Grab the optional id for this footnote.  This would only be
  # populated if the designer intends on referencing this footnote
  # in multiple spots.
  id = opt[:id] || opt[:name]

  # If an id was specified, check to see if an existing footnote has
  # already been associated with this.
  instance = ctx.footnotes.detect { |f| f.id == id } unless id.blank?
  unless instance

    # Grab the optional symbol that was specified by the designer.  If
    # this isn't specified count the number of existing numeric footnotes
    # and increment it for this new footnote's symbol.
    symbol = opt[:symbol]

    # Grab the text associated with this footnote.
    text = opt[:text]
    if text.blank?
      ctx.error("Footnote requires text attribute", { :id => id, :symbol => symbol })
      return
    end

    # Create a new Footnote instance
    instance = Instance.new(id, symbol, text)

    # Push the new footnote onto the stack.
    ctx.footnotes << instance

  end

  # Check to see if the footnote's symbol is blank (either because one
  # wasn't defined in the source.html or because the one read from the
  # footnotes.tsv had no symbol associated with it) and if so, generate
  # one based on the number of previously declared numeric footnotes.
  if instance.symbol.blank?

    # Grab the last numeric footnote that was specified and, assuming
    # there is one, increment the count.  Otherwise, start the count
    # off at one.
    last_instance = ctx.footnotes.select { |fn| fn.numeric? && fn.active? }.collect(&:number).max.to_i
    instance.symbol = last_instance + 1

  end

  # Make sure the instance is marked as having been used so it will
  # appear in the {footnotes} rendering.
  instance.active = true

  unless id.blank?

    # Check to see if the once flag is enabled.  If enabled, check to see if
    # this footnote has appeared before.
    once = opt[:once]
    return '' if once && !ctx.once?("#{id}-footnote")

  end

  # Allow footnotes to be defined without showing a symbol
  hidden = opt[:hidden] || (opt[:hidden] == '1')
  return '' if hidden

  # Check to see if the footnote should be automatically surrounded
  # by the superscript helper.
  sup = opt[:sup] && !ctx.text?

  html = ''
  html << '{sup}' if sup
  html << instance.symbol
  html << '{/sup}' if sup

  html
end