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.



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

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#glob_mapObject (readonly)

Returns the value of attribute glob_map.



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

def glob_map
  @glob_map
end

Class Method Details

.by_pairs(mapping, filename) ⇒ Object



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

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



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

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