Module: Infoboxer::Navigation::Sections::Container

Included in:
Section, Tree::Document
Defined in:
lib/infoboxer/navigation/sections.rb

Overview

This module is included in Document, allowing you to navigate through document's logical sections (and also included in each Section instance, allowing to navigate recursively).

See also parent module docs.

Instance Method Summary collapse

Instance Method Details

#introTree::Nodes

All container's paragraph-level nodes before first heading.

Returns:



38
39
40
41
42
# File 'lib/infoboxer/navigation/sections.rb', line 38

def intro
  children
    .take_while { |n| !n.is_a?(Tree::Heading) }
    .select { |n| n.is_a?(Tree::BaseParagraph) }
end

#lookup_children(*arg) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/infoboxer/navigation/sections.rb', line 97

def lookup_children(*arg)
  if arg.include?(:Section)
    sections.find(*(arg - [:Section]))
  else
    super
  end
end

#sections(*names) ⇒ Tree::Nodes<Section>

List of sections inside current container.

Examples of usage:

document.sections                 # all top-level sections
document.sections('Culture')      # only "Culture" section
document.sections(/^List of/)     # all sections with heading matching pattern

document.
  sections('Culture').            # long way of recieve nested section
    sections('Music')             # (Culture / Music)

document.
  sections('Culture', 'Music')    # the same as above

document.
  sections('Culture' => 'Music')  # pretty-looking version for 2 levels of nesting

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/infoboxer/navigation/sections.rb', line 65

def sections(*names)
  @sections ||= make_sections

  if names.first.is_a?(Hash)
    h = names.shift
    h.count == 1 or fail(ArgumentError, "Undefined behavior with #{h}")
    names.unshift(h.keys.first, h.values.first)
  end

  case names.count
  when 0
    @sections
  when 1
    @sections.select { |s| names.first === s.heading.text_ }
  else
    @sections.select { |s| names.first === s.heading.text_ }.sections(*names[1..])
  end
end

#subsections(*names) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/infoboxer/navigation/sections.rb', line 84

def subsections(*names)
  sections = names.map { |name|
    heading = lookup_children(:Heading, text_: name).first
    next unless heading

    body = heading.next_siblings
                  .take_while { |n| !n.is_a?(Tree::Heading) || n.level > heading.level }

    Section.new(heading, body)
  }.compact
  Tree::Nodes.new(sections)
end