Class: Vivlio::PDF::TocItem

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vivlio/pdf/toc_item.rb

Overview

One entry of a publication's table of contents, as reported by window.coreViewer.getTOC(). Immutable value object holding a subtree.

id is the anchor id of the heading the entry points at; Chromium turns it into a PDF named destination while printing, and Outline::Toc later references that name. No page numbers are involved.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, title: nil, children: []) ⇒ TocItem

Returns a new instance of TocItem.



23
24
25
26
27
28
# File 'lib/vivlio/pdf/toc_item.rb', line 23

def initialize(id:, title: nil, children: [])
  @id = id
  @title = title
  @children = children.freeze
  freeze
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



14
15
16
# File 'lib/vivlio/pdf/toc_item.rb', line 14

def children
  @children
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/vivlio/pdf/toc_item.rb', line 14

def id
  @id
end

#titleObject (readonly)

Returns the value of attribute title.



14
15
16
# File 'lib/vivlio/pdf/toc_item.rb', line 14

def title
  @title
end

Class Method Details

.build(raw) ⇒ Object

Builds a forest from the viewer's raw TOC array.



17
18
19
20
21
# File 'lib/vivlio/pdf/toc_item.rb', line 17

def self.build(raw)
  Array(raw).map do |entry|
    new(id: entry['id'], title: entry['title'], children: build(entry['children']))
  end
end

Instance Method Details

#each {|_self| ... } ⇒ Object

Depth-first traversal over self and all descendants.

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
# File 'lib/vivlio/pdf/toc_item.rb', line 31

def each(&block)
  return enum_for(:each) unless block

  yield self
  children.each { |child| child.each(&block) }
end

#inspectObject



47
48
49
# File 'lib/vivlio/pdf/toc_item.rb', line 47

def inspect
  "#<#{self.class} #{label.inspect} children=#{children.size}>"
end

#labelObject

Falls back to the anchor id so an entry is never blank in a PDF reader.



43
44
45
# File 'lib/vivlio/pdf/toc_item.rb', line 43

def label
  title || id.to_s
end

#leaf?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/vivlio/pdf/toc_item.rb', line 38

def leaf?
  children.empty?
end