Class: Rouge::Guessers::Disambiguation

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

Defined Under Namespace

Classes: Disambiguator

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.


7
8
9
10
# File 'lib/rouge/guessers/disambiguation.rb', line 7

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

Class Method Details

.disambiguate(*patterns, &decider) ⇒ Object


37
38
39
# File 'lib/rouge/guessers/disambiguation.rb', line 37

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

.disambiguatorsObject


41
42
43
# File 'lib/rouge/guessers/disambiguation.rb', line 41

def self.disambiguators
  @disambiguators
end

Instance Method Details

#contains?(text) ⇒ Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/rouge/guessers/disambiguation.rb', line 28

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

#filter(lexers) ⇒ Object


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

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)

32
33
34
# File 'lib/rouge/guessers/disambiguation.rb', line 32

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