Sha256: 494e18b13377e5824dcfdab9a8d096b346e40512bc0254a4e5d5e4c988f82776

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

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|
          score = (@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

6 entries across 6 versions & 2 rubygems

Version Path
rouge-3.2.1 lib/rouge/guessers/glob_mapping.rb
rouge-3.2.0 lib/rouge/guessers/glob_mapping.rb
rouge_ecl-0.0.1 lib/rouge/guessers/glob_mapping.rb
rouge-3.1.1 lib/rouge/guessers/glob_mapping.rb
rouge-3.1.0 lib/rouge/guessers/glob_mapping.rb
rouge-3.0.0 lib/rouge/guessers/glob_mapping.rb