Class: Starter::Markdown::FootnoteProcessor
- Inherits:
-
Object
- Object
- Starter::Markdown::FootnoteProcessor
- Defined in:
- lib/starter/markdown/extender.rb
Overview
Converts Pandoc-style footnotes into appropriate HTML
Instance Method Summary collapse
- #format_anchor(number, identifier) ⇒ Object
- #format_ref(number, identifier) ⇒ Object
-
#initialize(options = {}) ⇒ FootnoteProcessor
constructor
A new instance of FootnoteProcessor.
- #process(lines) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FootnoteProcessor
Returns a new instance of FootnoteProcessor.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/starter/markdown/extender.rb', line 85 def initialize(={}) # looking for footnote refs like [^some_identifier] @note_regex = / ^ \[\^ ([\d\w\-_]+) \]: /x @ref_regex = / \[\^ ([\d\w\-_]+) \] /x end |
Instance Method Details
#format_anchor(number, identifier) ⇒ Object
146 147 148 |
# File 'lib/starter/markdown/extender.rb', line 146 def format_anchor(number, identifier) %Q{<a name="#{identifier}" href="#__#{identifier}">#{number} ↩</a>.} end |
#format_ref(number, identifier) ⇒ Object
150 151 152 153 154 |
# File 'lib/starter/markdown/extender.rb', line 150 def format_ref(number, identifier) %Q{ <sup><a name="__#{identifier}" href="##{identifier}">#{number}</a>.</sup> }.strip end |
#process(lines) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/starter/markdown/extender.rb', line 100 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 |