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, selector: nil) ⇒ 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, selector: nil)
  # 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, selector: selector)
  end

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

#bake_footnotes_within(container, number_format:, selector: nil) ⇒ 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
45
46
47
48
49
50
51
52
53
# File 'lib/kitchen/directions/bake_footnotes/v1.rb', line 21

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

  container.search("a[role='doc-noteref']#{selector}").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..]
    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
    anchor[:id] = "#{aside_id}-a" if selector
  end

  container.search("aside#{selector}").each do |aside|
    footnote_number = aside_id_to_footnote_number[aside.id]
    if selector
      aside.wrap_children('span')

      aside.prepend(
        child: "<a href='##{aside.id}-a' data-type='footnote-number'>#{footnote_number}.</a>"
      )
    else
      aside.prepend(child: "<div data-type='footnote-number'>#{footnote_number}</div>")
    end
  end

  footnote_count
end