Class: Kitabu::Footnotes::HTML

Inherits:
Base
  • Object
show all
Defined in:
lib/kitabu/footnotes/html.rb

Instance Attribute Summary

Attributes inherited from Base

#content, #footnote_index, #html

Instance Method Summary collapse

Methods inherited from Base

#increment_footnote_index!, #initialize, process

Constructor Details

This class inherits a constructor from Kitabu::Footnotes::Base

Instance Method Details

#processObject



6
7
8
# File 'lib/kitabu/footnotes/html.rb', line 6

def process
  html.css(".chapter").each(&method(:process_chapter))
end

#process_chapter(chapter) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kitabu/footnotes/html.rb', line 10

def process_chapter(chapter)
  footnotes = chapter.css(".footnotes").first
  return unless footnotes

  list = footnotes.css("ol").first
  list.set_attribute "start", footnote_index

  chapter.css(".footnotes li").each do |footnote|
    process_footnote(chapter, footnote)
    increment_footnote_index!
  end
end

#process_footnote(chapter, footnote) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/kitabu/footnotes/html.rb', line 23

def process_footnote(chapter, footnote)
  current_index = footnote.get_attribute("id").gsub(/[^\d]/m, "")
  footnote.set_attribute "id", "fn#{footnote_index}"

  process_links_to_footnote(chapter, current_index)
  process_rev_links(chapter, current_index)
  process_ref_elements(chapter, current_index)
end


32
33
34
35
36
# File 'lib/kitabu/footnotes/html.rb', line 32

def process_links_to_footnote(chapter, current_index)
  chapter.css("a[href='#fn#{current_index}']").each do |link|
    link.set_attribute "href", "#fn#{footnote_index}"
  end
end

#process_ref_elements(chapter, current_index) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kitabu/footnotes/html.rb', line 44

def process_ref_elements(chapter, current_index)
  selector = "sup[id=fnref#{current_index}]"

  chapter.css(selector).each_with_index do |sup, index|
    if index.zero?
      sup.set_attribute "id", "fnref#{footnote_index}"
    else
      sup.remove_attribute "id"
    end

    sup.css("a").first.content = footnote_index
  end
end


38
39
40
41
42
# File 'lib/kitabu/footnotes/html.rb', line 38

def process_rev_links(chapter, current_index)
  chapter.css("a[href='#fnref#{current_index}']").each do |link|
    link.set_attribute "href", "#fnref#{footnote_index}"
  end
end