Class: Kitabu::Footnotes::PDF

Inherits:
Base
  • Object
show all
Defined in:
lib/kitabu/footnotes/pdf.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
9
# File 'lib/kitabu/footnotes/pdf.rb', line 6

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

#process_chapter(chapter) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/kitabu/footnotes/pdf.rb', line 21

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

  chapter.css(".footnotes").each(&:remove)
end

#process_footnote(chapter, footnote) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kitabu/footnotes/pdf.rb', line 30

def process_footnote(chapter, footnote)
  # Remove rev links
  footnote.css("[rev=footnote]").map(&:remove)

  # Create an element for storing the footnote description
  description = Nokogiri::XML::Node.new(
    "span",
    Nokogiri::HTML::DocumentFragment.parse("")
  )
  description.set_attribute "class", "footnote"
  description.inner_html = footnote.css("p").map(&:inner_html).join("\n")

  # Find ref based on footnote's id
  fn_id = footnote.get_attribute("id")

  chapter.css("a[href='##{fn_id}']").each do |ref|
    sup = ref.parent
    sup.after(description)
    sup.remove
  end
end

#remove_duplicated_attributesObject



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

def remove_duplicated_attributes
  # https://github.com/sparklemotion/nokogiri/issues/339
  html.css("html").first.tap do |element|
    next unless element

    element.delete("xmlns")
    element.delete("xml:lang")
  end
end