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
-
#intro ⇒ Tree::Nodes
All container's paragraph-level nodes before first heading.
- #lookup_children(*arg) ⇒ Object
-
#sections(*names) ⇒ Tree::Nodes<Section>
List of sections inside current container.
- #subsections(*names) ⇒ Object
Instance Method Details
#intro ⇒ Tree::Nodes
All container's paragraph-level nodes before first heading.
36 37 38 39 40 |
# File 'lib/infoboxer/navigation/sections.rb', line 36 def intro children .take_while { |n| !n.is_a?(Tree::Heading) } .select { |n| n.is_a?(Tree::BaseParagraph) } end |
#lookup_children(*arg) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/infoboxer/navigation/sections.rb', line 94 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/infoboxer/navigation/sections.rb', line 63 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 |
#subsections(*names) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/infoboxer/navigation/sections.rb', line 82 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 |