blob: 615ac2b32e83716e319716d18bd5373ee6a1c513 (
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
38
39
40
41
42
43
44
45
|
# 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
|