lib/infoboxer/navigation/sections.rb in infoboxer-0.2.8 vs lib/infoboxer/navigation/sections.rb in infoboxer-0.3.0.pre
- old
+ new
@@ -79,10 +79,18 @@
else
@sections.select { |s| names.first === s.heading.text_ }.sections(*names[1..-1])
end
end
+ def lookup_children(*arg)
+ if arg.include?(:Section)
+ sections.find(*(arg - [:Section]))
+ else
+ super
+ end
+ end
+
private
def make_sections
res = Tree::Nodes[]
return res if headings.empty?
@@ -113,25 +121,29 @@
# List of sections current node contained in (bottom-to-top:
# smallest section first).
#
# @return {Tree::Nodes<Section>}
def in_sections
- main_node = parent.is_a?(Tree::Document) ? self : lookup_parents[-2]
+ return parent.in_sections unless parent.is_a?(Tree::Document)
+ return @in_sections if @in_sections
heading =
- if main_node.is_a?(Tree::Heading)
- main_node.lookup_prev_siblings(Tree::Heading, level: main_node.level - 1).last
+ if is_a?(Tree::Heading)
+ lookup_prev_sibling(Tree::Heading, level: level - 1)
else
- main_node.lookup_prev_siblings(Tree::Heading).last
+ lookup_prev_sibling(Tree::Heading)
end
- return Tree::Nodes[] unless heading
+ unless heading
+ @in_sections = Tree::Nodes[]
+ return @in_sections
+ end
body = heading.next_siblings
.take_while { |n| !n.is_a?(Tree::Heading) || n.level < heading.level }
section = Section.new(heading, body)
- Tree::Nodes[section, *heading.in_sections]
+ @in_sections = Tree::Nodes[section, *heading.in_sections]
end
end
# Part of {Sections} navigation, allowing chains of section search.
#
@@ -143,10 +155,18 @@
%i[sections in_sections].each do |sym|
define_method(sym) do |*args|
make_nodes(map { |n| n.send(sym, *args) })
end
end
+
+ def lookup_children(*arg)
+ if arg.include?(:Section)
+ sections.find(*(arg - [:Section]))
+ else
+ super
+ end
+ end
end
# Virtual node, representing logical section of the document.
# Is not, in fact, in the tree.
#
@@ -171,9 +191,13 @@
end
end
def empty?
false
+ end
+
+ def inspect
+ "#<#{descr}: #{children.count} nodes>"
end
include Container
end
end