Improve template data

This commit is contained in:
Alex Kotov 2023-09-30 04:17:06 +04:00
parent 2fe72d3ea4
commit 5944a20b56
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 19 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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? -%>
<h2>Self references</h2>
<ol>
<% self_notes.each do |note| -%>
<% notes['self'].each do |note| -%>
<li id="<%= note['anchor'] %>" value="<%= note['index'] %>">
<a href="<%= note['id'] %>.html"><%= note['text'] %></a>
</li>
@ -14,12 +11,12 @@
</ol>
<% end -%>
<%# -%>
<% unless link_notes.empty? -%>
<% unless notes['link'].empty? -%>
<h2>Link references</h2>
<ol>
<% link_notes.each do |note| -%>
<% notes['link'].each do |note| -%>
<li id="<%= note['anchor'] %>" value="<%= note['index'] %>">
<a href="<%= note['url'] %>"><%= note['text'] %></a>
</li>

View File

@ -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 -%>
]
}