Class: Asciibook::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/asciibook/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, node:) ⇒ Page

Returns a new instance of Page.



5
6
7
8
9
10
# File 'lib/asciibook/page.rb', line 5

def initialize(path:, node:)
  @path = path
  @node = node

  @footnotes = []
end

Instance Attribute Details

#footnotesObject

Returns the value of attribute footnotes.



3
4
5
# File 'lib/asciibook/page.rb', line 3

def footnotes
  @footnotes
end

#next_pageObject

Returns the value of attribute next_page.



3
4
5
# File 'lib/asciibook/page.rb', line 3

def next_page
  @next_page
end

#nodeObject

Returns the value of attribute node.



3
4
5
# File 'lib/asciibook/page.rb', line 3

def node
  @node
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/asciibook/page.rb', line 3

def path
  @path
end

#prev_pageObject

Returns the value of attribute prev_page.



3
4
5
# File 'lib/asciibook/page.rb', line 3

def prev_page
  @prev_page
end

Instance Method Details

#contentObject



16
17
18
# File 'lib/asciibook/page.rb', line 16

def content
  @content ||= node.convert
end

#descriptionObject



28
29
30
# File 'lib/asciibook/page.rb', line 28

def description
  doc.css('p').first&.text
end

#docObject



20
21
22
# File 'lib/asciibook/page.rb', line 20

def doc
  @doc ||= Nokogiri::HTML.fragment(content)
end

#image_urlObject



24
25
26
# File 'lib/asciibook/page.rb', line 24

def image_url
  doc.css('img').first&.attr('src')
end

#outlineObject



32
33
34
# File 'lib/asciibook/page.rb', line 32

def outline
  outline_node(@node)
end

#outline_node(node) ⇒ Object

page outline only list sections that not split as page



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/asciibook/page.rb', line 37

def outline_node(node)
  data = []
  node.sections.each do |section|
    if !section.page
      section_data = {
        'title' => section.xreftext,
        'path' => "##{section.id}"
      }
      if section.sections.count > 0
        section_data['items'] = outline_node(section)
      end
      data << section_data
    end
  end
  data
end

#titleObject



12
13
14
# File 'lib/asciibook/page.rb', line 12

def title
  node.title
end

#to_hashObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/asciibook/page.rb', line 54

def to_hash
  {
    'path' => path,
    'title' => title,
    'content' => content,
    'image_url' => image_url,
    'description' => description,
    'outline' => outline,
    'prev_page' => prev_page && { 'path' => prev_page.path, 'title' => prev_page.title },
    'next_page' => next_page && { 'path' => next_page.path, 'title' => next_page.title }
  }
end