Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reference_querying.rb « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37b7f622c36e21cb749d18cdbb3743f191daea64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Banzai
  class ReferenceQuerying
    def self.document_nodes(documents, types = [])
      documents.map { |document| DocumentNodes.new(document, types) }
    end

    class DocumentNodes
      def initialize(document, types = [])
        @document = document
        @types    = types
      end

      attr_reader :document, :types

      def nodes
        types.empty? ? raw_nodes : nodes_by_type.values.flatten
      end

      def nodes_by_type
        @nodes_by_type ||= begin
          per_type = Hash.new { |hash, key| hash[key] = [] }
          raw_nodes.group_by { |node| node.attr('data-reference-type') }.each do |type, nodes|
            type_sym = type.to_sym
            per_type[type_sym] = nodes if types.include?(type_sym)
          end
          per_type
        end
      end

      private

      def raw_nodes
        @raw_nodes ||= Querying.css(document, 'a.gfm[data-reference-type]')
      end
    end
  end
end