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:



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

def intro
  children.
    take_while{|n| !n.is_a?(Tree::Heading)}.
    select{|n| n.is_a?(Tree::BaseParagraph)}
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:



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

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..-1])
  end
end