Sha256: 917c48d77c9ed546712a8e1851b68473440adc30b49c443bbf9b08b2823443b6

Contents?: true

Size: 1.07 KB

Versions: 44

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Rouge
  module Guessers
    # This class allows for custom behavior
    # with glob -> lexer name mappings
    class GlobMapping < Guesser
      include Util

      def self.by_pairs(mapping, filename)
        glob_map = {}
        mapping.each do |(glob, lexer_name)|
          lexer = Lexer.find(lexer_name)

          # ignore unknown lexers
          next unless lexer

          glob_map[lexer.name] ||= []
          glob_map[lexer.name] << glob
        end

        new(glob_map, filename)
      end

      attr_reader :glob_map, :filename
      def initialize(glob_map, filename)
        @glob_map = glob_map
        @filename = filename
      end

      def filter(lexers)
        basename = File.basename(filename)

        collect_best(lexers) do |lexer|
          (@glob_map[lexer.name] || []).map do |pattern|
            if test_glob(pattern, basename)
              # specificity is better the fewer wildcards there are
              -pattern.scan(/[*?\[]/).size
            end
          end.compact.min
        end
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 2 rubygems

Version Path
rouge-4.5.1 lib/rouge/guessers/glob_mapping.rb
rouge-4.5.0 lib/rouge/guessers/glob_mapping.rb
rouge-4.4.0 lib/rouge/guessers/glob_mapping.rb
rouge-4.3.0 lib/rouge/guessers/glob_mapping.rb
rouge-4.2.1 lib/rouge/guessers/glob_mapping.rb
rouge-4.2.0 lib/rouge/guessers/glob_mapping.rb
rouge-4.1.3 lib/rouge/guessers/glob_mapping.rb
rouge-4.1.2 lib/rouge/guessers/glob_mapping.rb
rouge-4.1.1 lib/rouge/guessers/glob_mapping.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/guessers/glob_mapping.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/rouge-3.30.0/lib/rouge/guessers/glob_mapping.rb
rouge-4.1.0 lib/rouge/guessers/glob_mapping.rb
rouge-4.0.1 lib/rouge/guessers/glob_mapping.rb
rouge-4.0.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.30.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.29.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.28.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.27.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.26.1 lib/rouge/guessers/glob_mapping.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/rouge-3.26.0/lib/rouge/guessers/glob_mapping.rb