Class: Rouge::Guessers::Disambiguation

Inherits:
Rouge::Guesser show all
Includes:
Util, Lexers
Defined in:
lib/rouge/guessers/disambiguation.rb

Defined Under Namespace

Classes: Disambiguator

Constant Summary

Constants included from Lexers

Lexers::BASE_DIR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lexers

load_lexer

Methods included from Util

#get_source, #test_glob

Methods inherited from Rouge::Guesser

#collect_best, guess

Constructor Details

#initialize(filename, source) ⇒ Disambiguation

Returns a new instance of Disambiguation.



9
10
11
12
# File 'lib/rouge/guessers/disambiguation.rb', line 9

def initialize(filename, source)
  @filename = File.basename(filename)
  @source = source
end

Class Method Details

.disambiguate(*patterns, &decider) ⇒ Object



39
40
41
# File 'lib/rouge/guessers/disambiguation.rb', line 39

def self.disambiguate(*patterns, &decider)
  @disambiguators << Disambiguator.new(patterns, &decider)
end

.disambiguatorsObject



43
44
45
# File 'lib/rouge/guessers/disambiguation.rb', line 43

def self.disambiguators
  @disambiguators
end

Instance Method Details

#contains?(text) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rouge/guessers/disambiguation.rb', line 30

def contains?(text)
  return @analyzer.include?(text)
end

#filter(lexers) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rouge/guessers/disambiguation.rb', line 14

def filter(lexers)
  return lexers if lexers.size == 1
  return lexers if lexers.size == Lexer.all.size

  @analyzer = TextAnalyzer.new(get_source(@source))

  self.class.disambiguators.each do |disambiguator|
    next unless disambiguator.match?(@filename)

    filtered = disambiguator.decide!(self)
    return filtered if filtered
  end

  return lexers
end

#matches?(re) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rouge/guessers/disambiguation.rb', line 34

def matches?(re)
  return !!(@analyzer =~ re)
end