lib/infoboxer/tree/nodes.rb in infoboxer-0.3.0 vs lib/infoboxer/tree/nodes.rb in infoboxer-0.3.1.pre

- old
+ new

@@ -36,14 +36,23 @@ # Just like Array#flatten, but returns Nodes # @!method compact # Just like Array#compact, but returns Nodes + # @!method grep(pattern) + # Just like Array#grep, but returns Nodes + + # @!method grep_v(pattern) + # Just like Array#grep_v, but returns Nodes + # @!method -(other) # Just like Array#-, but returns Nodes - %i[select reject sort_by flatten compact -].each do |sym| + # @!method +(other) + # Just like Array#+, but returns Nodes + + %i[select reject sort_by flatten compact grep grep_v - +].each do |sym| define_method(sym) do |*args, &block| Nodes[*super(*args, &block)] end end @@ -73,10 +82,25 @@ else res end end + # Just like Array#flat_map, but returns Nodes, **if** all map results are Node + def flat_map + res = super + if res.all? { |n| n.is_a?(Node) || n.is_a?(Nodes) } + Nodes[*res] + else + res + end + end + + # Just like Array#group, but returns hash with `{<grouping variable> => Nodes}` + def group_by + super.map { |title, group| [title, Nodes[*group]] }.to_h + end + # @!method prev_siblings # Previous siblings (flat list) of all nodes inside. # @!method next_siblings # Next siblings (flat list) of all nodes inside. @@ -137,16 +161,18 @@ # It will be fixed in next releases. # # @return [Nodes<MediaWiki::Page>] It is still `Nodes`, so you # still can process them uniformely. def follow - links = select { |n| n.respond_to?(:link) }.map(&:link) + links = grep(Linkable) return Nodes[] if links.empty? page = first.lookup_parents(MediaWiki::Page).first or fail('Not in a page from real source') page.client or fail('MediaWiki client not set') - page.client.get(*links) + pages = links.group_by(&:interwiki) + .flat_map { |iw, ls| page.client.get(*ls.map(&:link), interwiki: iw) } + pages.count == 1 ? pages.first : Nodes[*pages] end # @private # Internal, used by {Parser} def <<(node) @@ -171,10 +197,12 @@ end # @private # Internal, used by {Parser} def flow_templates - make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n }) + # TODO: will it be better?.. + # make_nodes(map { |n| n.is_a?(Paragraph) ? n.to_templates? : n }) + self end private # @private For inspect shortening