Class: Rouge::Guessers::GlobMapping

Inherits:
Rouge::Guesser show all
Includes:
Util
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 included from Util

#get_source, #test_glob

Methods inherited from Rouge::Guesser

#collect_best, guess

Constructor Details

#initialize(glob_map, filename) ⇒ GlobMapping

Returns a new instance of GlobMapping.



26
27
28
29
# File 'lib/rouge/guessers/glob_mapping.rb', line 26

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



25
26
27
# File 'lib/rouge/guessers/glob_mapping.rb', line 25

def filename
  @filename
end

#glob_mapObject (readonly)

Returns the value of attribute glob_map.



25
26
27
# File 'lib/rouge/guessers/glob_mapping.rb', line 25

def glob_map
  @glob_map
end

Class Method Details

.by_pairs(mapping, filename) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rouge/guessers/glob_mapping.rb', line 10

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rouge/guessers/glob_mapping.rb', line 31

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