Class: Vivlio::PDF::TocItem
- Inherits:
-
Object
- Object
- Vivlio::PDF::TocItem
- 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
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
-
.build(raw) ⇒ Object
Builds a forest from the viewer's raw TOC array.
Instance Method Summary collapse
-
#each {|_self| ... } ⇒ Object
Depth-first traversal over self and all descendants.
-
#initialize(id:, title: nil, children: []) ⇒ TocItem
constructor
A new instance of TocItem.
- #inspect ⇒ Object
-
#label ⇒ Object
Falls back to the anchor id so an entry is never blank in a PDF reader.
- #leaf? ⇒ Boolean
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
14 15 16 |
# File 'lib/vivlio/pdf/toc_item.rb', line 14 def children @children end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/vivlio/pdf/toc_item.rb', line 14 def id @id end |
#title ⇒ Object (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.
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 |
#inspect ⇒ Object
47 48 49 |
# File 'lib/vivlio/pdf/toc_item.rb', line 47 def inspect "#<#{self.class} #{label.inspect} children=#{children.size}>" end |
#label ⇒ Object
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
38 39 40 |
# File 'lib/vivlio/pdf/toc_item.rb', line 38 def leaf? children.empty? end |