aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2012-08-03 16:03:06 -0500
committerJoshua Peek <josh@joshpeek.com>2012-08-03 16:03:06 -0500
commit804e23e99536187207863eb780e1e30a20c12c8d (patch)
tree025fb0f5e1e09659608493cfe2f94165757fcb3e
parent41b7d13aa7541bc03b3bfd4e3eda36f6cde9cca1 (diff)
Extract seperate language detection method
-rw-r--r--lib/linguist/blob_helper.rb35
-rw-r--r--lib/linguist/language.rb30
2 files changed, 32 insertions, 33 deletions
diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb
index d805df1..e56d095 100644
--- a/lib/linguist/blob_helper.rb
+++ b/lib/linguist/blob_helper.rb
@@ -1,8 +1,6 @@
-require 'linguist/classifier'
require 'linguist/generated'
require 'linguist/language'
require 'linguist/mime'
-require 'linguist/samples'
require 'charlock_holmes'
require 'escape_utils'
@@ -261,37 +259,8 @@ module Linguist
def language
if defined? @language
@language
- else
- @language = guess_language
- end
- end
-
- # Internal: Guess language
- #
- # Please add additional test coverage to
- # `test/test_blob.rb#test_language` if you make any changes.
- #
- # Returns a Language or nil
- def guess_language
- return if binary_mime_type?
-
- name = self.name.to_s
-
- # A bit of an elegant hack. If the file is exectable but extensionless,
- # append a "magic" extension so it can be classified with other
- # languages that have shebang scripts.
- if extname.empty? && mode && (mode.to_i(8) & 05) == 05
- name += ".script!"
- end
-
- possible_languages = Language.find_by_filename(name)
-
- if possible_languages.length > 1
- if result = Classifier.classify(Samples::DATA, data, possible_languages.map(&:name)).first
- Language[result[0]]
- end
- else
- possible_languages.first
+ elsif !binary_mime_type?
+ @language = Language.detect(name.to_s, lambda { data }, mode)
end
end
diff --git a/lib/linguist/language.rb b/lib/linguist/language.rb
index 643a523..fb24204 100644
--- a/lib/linguist/language.rb
+++ b/lib/linguist/language.rb
@@ -2,6 +2,7 @@ require 'escape_utils'
require 'pygments'
require 'yaml'
+require 'linguist/classifier'
require 'linguist/samples'
module Linguist
@@ -62,6 +63,35 @@ module Linguist
language
end
+ # Public: Detects the Language of the blob.
+ #
+ # name - String filename
+ # data - String blob data. A block also maybe passed in for lazy
+ # loading. This behavior is deprecated and you should always
+ # pass in a String.
+ # mode - Optional String mode (defaults to nil)
+ #
+ # Returns Language or nil.
+ def self.detect(name, data, mode = nil)
+ # A bit of an elegant hack. If the file is exectable but extensionless,
+ # append a "magic" extension so it can be classified with other
+ # languages that have shebang scripts.
+ if File.extname(name).empty? && mode && (mode.to_i(8) & 05) == 05
+ name += ".script!"
+ end
+
+ possible_languages = find_by_filename(name)
+
+ if possible_languages.length > 1
+ data = data.call() if data.respond_to?(:call)
+ if result = Classifier.classify(Samples::DATA, data, possible_languages.map(&:name)).first
+ Language[result[0]]
+ end
+ else
+ possible_languages.first
+ end
+ end
+
# Public: Get all Languages
#
# Returns an Array of Languages