Class: Rouge::Guessers::Source

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Rouge::Guesser

#collect_best, guess

Constructor Details

#initialize(source) ⇒ Source

Returns a new instance of Source.



5
6
7
# File 'lib/rouge/guessers/source.rb', line 5

def initialize(source)
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/rouge/guessers/source.rb', line 4

def source
  @source
end

Instance Method Details

#filter(lexers) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rouge/guessers/source.rb', line 9

def filter(lexers)
  # don't bother reading the input if
  # we've already filtered to 1
  return lexers if lexers.size == 1

  # If we're filtering against *all* lexers, we only use confident return
  # values from analyze_text.  But if we've filtered down already, we can trust
  # the analysis more.
  threshold = lexers.size < 10 ? 0 : 0.5

  source_text = case @source
  when String
    @source
  when ->(s){ s.respond_to? :read }
    @source.read
  else
    raise 'invalid source'
  end

  Lexer.assert_utf8!(source_text)

  source_text = TextAnalyzer.new(source_text)

  collect_best(lexers, threshold: threshold) do |lexer|
    next unless lexer.methods(false).include? :analyze_text
    lexer.analyze_text(source_text)
  end
end