class PFM::FootnoteProcessor

Converts Pandoc-style footnotes into appropriate HTML

Public Class Methods

new(options={}) click to toggle source
# File lib/pfm.rb, line 64
def initialize(options={})
  # looking for footnote refs like [^some_identifier]
  @note_regex = /
    ^
    \[\^
      ([\d\w\-_]+)
    \]:
  /x
  @ref_regex = /
    \[\^
      ([\d\w\-_]+)
    \]
  /x
end

Public Instance Methods

format_anchor(number, identifier) click to toggle source
# File lib/pfm.rb, line 125
def format_anchor(number, identifier)
  %Q{<a name="#{identifier}" href="#__#{identifier}">#{number} &#8617;</a>.}
end
format_ref(number, identifier) click to toggle source
# File lib/pfm.rb, line 129
def format_ref(number, identifier)
  %Q{
    <sup><a name="__#{identifier}" href="##{identifier}">#{number}</a>.</sup>
  }.strip
end
process(lines) click to toggle source
# File lib/pfm.rb, line 79
def process(lines)
  out = []
  refs = []
  notes = {}
  ref_counter = 0
  while line = lines.shift
    if md = @note_regex.match(line)
      full, identifier = md.to_a
      note = notes[identifier] = [line]
      while (next_line = lines.shift) && next_line !~ /^\s*$/
        note << next_line
      end

    elsif md = @ref_regex.match(line)
      full, identifier = md.to_a
      ref_counter += 1
      refs << identifier
      out << line.sub(full, format_ref(ref_counter, identifier))
    else
      out << line
    end
  end

  if refs.size > 0
    out << ""
    out << "# Notes"
    out << ""
    refs.each_with_index do |identifier, index|
      if note = notes[identifier]
        start = note.shift
        anchor = format_anchor(index + 1, identifier)
        start.sub! /^\[.*\]: /, ""
        out << "#{anchor} #{start}"

        note.each do |line|
          out << line
        end
        out << ""
      end
    end
  end
  out << ""
  out
end