Class: RedQuilt::FootnotePass

Inherits:
Object
  • Object
show all
Defined in:
lib/red_quilt/footnote_pass.rb

Overview

Post-inline pass for the footnotes extension. After the inline pass has resolved ‘[^label]` references (assigning numbers in first-reference order on the shared FootnoteRegistry), this reorders the definition nodes under the document-level footnotes section into that order, drops definitions that were never referenced, and removes the whole section when nothing referenced it.

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ FootnotePass

Returns a new instance of FootnotePass.



11
12
13
14
15
# File 'lib/red_quilt/footnote_pass.rb', line 11

def initialize(document)
  @document = document
  @arena = document.arena
  @registry = document.footnotes
end

Instance Method Details

#applyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/red_quilt/footnote_pass.rb', line 17

def apply
  return if @registry.nil?

  # BlockParser moves the footnotes section to be root's last child, so
  # that's where it is (if any definitions were collected at all).
  section_id = @arena.raw_last_child_id(@document.root_id)
  return if section_id == -1 || @arena.type(section_id) != NodeType::FOOTNOTES_SECTION

  unless @registry.any_referenced?
    @arena.detach(section_id)
    return
  end

  # Re-append referenced definitions in first-reference order; detaching
  # all current children first means unreferenced definitions are left
  # orphaned (and so never rendered).
  @arena.child_ids(section_id).to_a.each { |child| @arena.detach(child) }
  @registry.referenced_labels.each do |label|
    @arena.append_child(section_id, @registry.definition_node(label))
  end
end