Class: Rouge::Guessers::GlobMapping

Inherits:
Rouge::Guesser show all
Defined in:
lib/rouge/guessers/glob_mapping.rb

Overview

This class allows for custom behavior with glob -> lexer name mappings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rouge::Guesser

#collect_best, guess

Constructor Details

#initialize(glob_map, filename) ⇒ GlobMapping

Returns a new instance of GlobMapping.



22
23
24
25
# File 'lib/rouge/guessers/glob_mapping.rb', line 22

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



21
22
23
# File 'lib/rouge/guessers/glob_mapping.rb', line 21

def filename
  @filename
end

#glob_mapObject (readonly)

Returns the value of attribute glob_map.



21
22
23
# File 'lib/rouge/guessers/glob_mapping.rb', line 21

def glob_map
  @glob_map
end

Class Method Details

.by_pairs(mapping, filename) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rouge/guessers/glob_mapping.rb', line 6

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

Instance Method Details

#filter(lexers) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rouge/guessers/glob_mapping.rb', line 27

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

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