Sha256: edc3d4b6d8b356c21685f21ea3f14f233acd97125eac250832e78df2c4297f8b

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module Rouge
  class Guesser
    def self.guess(guessers, lexers)
      original_size = lexers.size

      guessers.each do |g|
        new_lexers = case g
        when Guesser then g.filter(lexers)
        when proc { |x| x.respond_to? :call } then g.call(lexers)
        else raise "bad guesser: #{g}"
        end

        lexers = new_lexers && new_lexers.any? ? new_lexers : lexers
      end

      # if we haven't filtered the input at *all*,
      # then we have no idea what language it is,
      # so we bail and return [].
      lexers.size < original_size ? lexers : []
    end

    def collect_best(lexers, opts={}, &scorer)
      best = []
      best_score = opts[:threshold]

      lexers.each do |lexer|
        score = scorer.call(lexer)

        next if score.nil?

        if best_score.nil? || score > best_score
          best_score = score
          best = [lexer]
        elsif score == best_score
          best << lexer
        end
      end

      best
    end

    def filter(lexers)
      raise 'abstract'
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rouge-2.0.2 lib/rouge/guesser.rb
rouge-2.0.1 lib/rouge/guesser.rb
rouge-2.0.0 lib/rouge/guesser.rb
rouge-1.11.1 lib/rouge/guesser.rb