mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Basic Vim modeline detection strategy
This commit is contained in:
@@ -11,6 +11,7 @@ require 'linguist/samples'
|
|||||||
require 'linguist/file_blob'
|
require 'linguist/file_blob'
|
||||||
require 'linguist/blob_helper'
|
require 'linguist/blob_helper'
|
||||||
require 'linguist/strategy/filename'
|
require 'linguist/strategy/filename'
|
||||||
|
require 'linguist/strategy/modeline'
|
||||||
require 'linguist/shebang'
|
require 'linguist/shebang'
|
||||||
|
|
||||||
module Linguist
|
module Linguist
|
||||||
@@ -95,6 +96,7 @@ module Linguist
|
|||||||
|
|
||||||
STRATEGIES = [
|
STRATEGIES = [
|
||||||
Linguist::Strategy::Filename,
|
Linguist::Strategy::Filename,
|
||||||
|
Linguist::Strategy::Modeline,
|
||||||
Linguist::Shebang,
|
Linguist::Shebang,
|
||||||
Linguist::Heuristics,
|
Linguist::Heuristics,
|
||||||
Linguist::Classifier
|
Linguist::Classifier
|
||||||
|
|||||||
31
lib/linguist/strategy/modeline.rb
Normal file
31
lib/linguist/strategy/modeline.rb
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
module Linguist
|
||||||
|
module Strategy
|
||||||
|
class Modeline
|
||||||
|
# Public: Detects language based on Vim and Emacs modelines
|
||||||
|
#
|
||||||
|
# blob - An object that quacks like a blob.
|
||||||
|
#
|
||||||
|
# Examples
|
||||||
|
#
|
||||||
|
# Modeline.call(FileBlob.new("path/to/file"))
|
||||||
|
#
|
||||||
|
# Returns an Array with one Language if the blob has a shebang with a valid
|
||||||
|
# interpreter, or empty if there is no shebang.
|
||||||
|
def self.call(blob, _ = nil)
|
||||||
|
if language = Language.find_by_alias(modeline(blob.data))
|
||||||
|
return [language]
|
||||||
|
else
|
||||||
|
return []
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Public: Get the modeline from the first n-lines of the file
|
||||||
|
#
|
||||||
|
# Returns a String or nil
|
||||||
|
def self.modeline(data)
|
||||||
|
data.lines.first(5).any? { |l| l.match(/\W(?:filetype|ft)=\s*(\w+)/) }
|
||||||
|
lang = $1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user