diff --git a/lib/referator/footnotes.rb b/lib/referator/footnotes.rb index 8b8a48d..a272c8a 100644 --- a/lib/referator/footnotes.rb +++ b/lib/referator/footnotes.rb @@ -20,7 +20,7 @@ module Referator end def render(format) - config.formats.render :footnotes, format, @notes.map(&:to_h).freeze + config.formats.render :footnotes, format, notes end private @@ -32,5 +32,14 @@ module Referator @config = config end + + def notes + config.kinds.names.to_h do |name| + [ + name, + @notes.select { |note| note.kind == name }.map(&:to_h).freeze, + ] + end.freeze + end end end diff --git a/lib/referator/note.rb b/lib/referator/note.rb index a95afec..18176af 100644 --- a/lib/referator/note.rb +++ b/lib/referator/note.rb @@ -6,7 +6,7 @@ module Referator attr_reader :footnotes, :index, :reference - def_delegators :reference, :anchor, :fragment + def_delegators :reference, :kind, :anchor, :fragment def initialize(footnotes, index, reference) self.footnotes = footnotes diff --git a/test/footnotes.html.erb b/test/footnotes.html.erb index c95e307..ee8293b 100644 --- a/test/footnotes.html.erb +++ b/test/footnotes.html.erb @@ -1,12 +1,9 @@ -<% self_notes = notes.select { |note| note['kind'] == 'self' } -%> -<% link_notes = notes.select { |note| note['kind'] == 'link' } -%> -<%# -%> -<% unless self_notes.empty? -%> +<% unless notes['self'].empty? -%>

Self references

    -<% self_notes.each do |note| -%> +<% notes['self'].each do |note| -%>
  1. <%= note['text'] %>
  2. @@ -14,12 +11,12 @@
<% end -%> <%# -%> -<% unless link_notes.empty? -%> +<% unless notes['link'].empty? -%>

Link references

    -<% link_notes.each do |note| -%> +<% notes['link'].each do |note| -%>
  1. <%= note['text'] %>
  2. diff --git a/test/footnotes.json.erb b/test/footnotes.json.erb index f2e058e..64f5859 100644 --- a/test/footnotes.json.erb +++ b/test/footnotes.json.erb @@ -1,9 +1,6 @@ -<% self_notes = notes.select { |note| note['kind'] == 'self' } -%> -<% link_notes = notes.select { |note| note['kind'] == 'link' } -%> -<%# -%> { "self": [ - <%- self_notes.each_with_index do |note, index| -%> + <%- notes['self'].each_with_index do |note, index| -%> { "index": <%= Integer(note['index']).to_json %>, "kind": "self", @@ -13,11 +10,11 @@ "fragment": <%= String(note['fragment']).to_json %>, "url": <%= "#{note['id']}.html".to_json %>, "text": <%= String(note['text']).to_json %> - }<%= ',' unless index == self_notes.count - 1 %> + }<%= ',' unless index == notes['self'].count - 1 %> <%- end -%> ], "link": [ - <%- link_notes.each_with_index do |note, index| -%> + <%- notes['link'].each_with_index do |note, index| -%> { "index": <%= Integer(note['index']).to_json %>, "kind": "link", @@ -27,7 +24,7 @@ "fragment": <%= String(note['fragment']).to_json %>, "url": <%= String(note['url']).to_json %>, "text": <%= String(note['text']).to_json %> - }<%= ',' unless index == link_notes.count - 1 %> + }<%= ',' unless index == notes['link'].count - 1 %> <%- end -%> ] }