summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.7.0/lib/rouge/guessers/filename.rb
blob: 2f01b4c9d869fc62bf9dbb0217fddf92b293e5f7 (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
# frozen_string_literal: true

module Rouge
  module Guessers
    class Filename < Guesser
      attr_reader :fname
      def initialize(filename)
        @filename = filename
      end

      # returns a list of lexers that match the given filename with
      # equal specificity (i.e. number of wildcards in the pattern).
      # This helps disambiguate between, e.g. the Nginx lexer, which
      # matches `nginx.conf`, and the Conf lexer, which matches `*.conf`.
      # In this case, nginx will win because the pattern has no wildcards,
      # while `*.conf` has one.
      def filter(lexers)
        mapping = {}
        lexers.each do |lexer|
          mapping[lexer.name] = lexer.filenames || []
        end

        GlobMapping.new(mapping, @filename).filter(lexers)
      end
    end
  end
end