mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
docs
This commit is contained in:
@@ -27,21 +27,40 @@ module Linguist
|
|||||||
[] # No heuristics matched
|
[] # No heuristics matched
|
||||||
end
|
end
|
||||||
|
|
||||||
@heuristics = []
|
# Internal: Define a new heuristic.
|
||||||
|
#
|
||||||
|
# languages - String names of languages to disambiguate.
|
||||||
|
# heuristic - Block which takes data as an argument and returns a Language or nil.
|
||||||
|
#
|
||||||
|
# Examples
|
||||||
|
#
|
||||||
|
# create "Perl", "Prolog" do |data|
|
||||||
|
# if data.include?("use strict")
|
||||||
|
# Language["Perl"]
|
||||||
|
# elsif data.include?(":-")
|
||||||
|
# Language["Prolog"]
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
def self.create(*languages, &heuristic)
|
def self.create(*languages, &heuristic)
|
||||||
@heuristics << new(languages, &heuristic)
|
@heuristics << new(languages, &heuristic)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Internal: Array of defined heuristics
|
||||||
|
@heuristics = []
|
||||||
|
|
||||||
|
# Internal
|
||||||
def initialize(languages, &heuristic)
|
def initialize(languages, &heuristic)
|
||||||
@languages = languages
|
@languages = languages
|
||||||
@heuristic = heuristic
|
@heuristic = heuristic
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Internal: Check if this heuristic matches the candidate languages.
|
||||||
def matches?(candidates)
|
def matches?(candidates)
|
||||||
candidates.all? { |l| @languages.include?(l.name) }
|
candidates.all? { |l| @languages.include?(l.name) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Internal: Perform the heuristic
|
||||||
def call(data)
|
def call(data)
|
||||||
@heuristic.call(data)
|
@heuristic.call(data)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user