Class: Kitchen::Directions::BakeFootnotes::V1

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/directions/bake_footnotes/v1.rb

Instance Method Summary collapse

Instance Method Details

#bake(book:, number_format: :arabic) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kitchen/directions/bake_footnotes/v1.rb', line 6

def bake(book:, number_format: :arabic)
  # Footnotes are numbered either within their top-level pages (preface,
  # appendices, etc) or within chapters. Tackle each case separately

  book.body.element_children.only(Kitchen::PageElement,
                                  Kitchen::CompositePageElement,
                                  Kitchen::CompositeChapterElement).each do |page|
    bake_footnotes_within(page, number_format: number_format)
  end

  book.chapters.each do |chapter|
    bake_footnotes_within(chapter, number_format: number_format)
  end
end

#bake_footnotes_within(container, number_format:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kitchen/directions/bake_footnotes/v1.rb', line 21

def bake_footnotes_within(container, number_format:)
  footnote_count = 0
  aside_id_to_footnote_number = {}

  container.search("a[role='doc-noteref']").each do |anchor|
    footnote_count += 1
    footnote_number = footnote_count.to_format(number_format)
    anchor.replace_children(with: footnote_number)
    aside_id = anchor[:href][1..-1]
    aside_id_to_footnote_number[aside_id] = footnote_number
    if anchor.parent.name == 'p'
      anchor.parent.add_class('has-noteref')
    elsif anchor.parent.name != 'p' && anchor.parent.parent.name == 'p'
      anchor.parent.parent.add_class('has-noteref')
    end
  end

  container.search('aside').each do |aside|
    footnote_number = aside_id_to_footnote_number[aside.id]
    aside.prepend(child: "<div data-type='footnote-number'>#{footnote_number}</div>")
  end

  footnote_count
end