class Inkcite::Renderer::Footnotes

Public Instance Methods

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

  # Nothing to do if footnotes are blank.
  return if ctx.footnotes.blank?

  # Grab the active footnotes.
  active_footnotes = ctx.footnotes.select(&:active)
  return if active_footnotes.blank?

  # Check to see if a template has been provided.  Otherwise use a default one based
  # on the format of the email.
  tmpl = opt[:tmpl] || opt[:template]
  if tmpl.blank?
    tmpl = ctx.text? ? '($symbol$) $text$\n\n' : '<sup>$symbol$</sup> $text$<br><br>'

  elsif ctx.text?

    # If there are new-lines encoded in the custom template, make sure
    # they get converted to real new lines.
    tmpl.gsub!('\\n', "\n")

  end

  # For the emailed version, append a line break between each footnote so that we don't
  # end up with lines that exceed the allowed limit in certain versions of Outlook.
  tmpl << "\n" if ctx.email?

  # First, collect all symbols in the natural order they are defined
  # in the email.
  footnotes = active_footnotes.select(&:symbol?)

  # Now add to the list all numeric footnotes ordered naturally
  # regardless of how they were ordered in the email.
  footnotes += active_footnotes.select(&:numeric?).sort { |f1, f2| f1.number <=> f2.number }

  html = ''

  # Iterate through each of the footnotes and render them based on the
  # template that was provided.
  footnotes.each do |f|
    html << tmpl.gsub('$symbol$', f.symbol).gsub('$text$', f.text)
  end

  html
end