mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Break modelines into two regular expressions
This makes them easier to read and maintains Ruby 1.9 compatibility
This commit is contained in:
@@ -1,19 +1,8 @@
|
|||||||
module Linguist
|
module Linguist
|
||||||
module Strategy
|
module Strategy
|
||||||
class Modeline
|
class Modeline
|
||||||
Regex = /
|
EmacsModeline = /-\*-\s*(?:mode:)?\s*(\w+);?\s*-\*-/
|
||||||
(?:
|
VimModeline = /\/\*\s*vim:\s*set\s*(?:ft|filetype)=(\w+):\s*\*\//
|
||||||
(-\*- \s* (?:mode:)? \s*) | # $1: Emacs
|
|
||||||
(\/\* \s* vim: \s* set \s* (?:ft|filetype)=) # $2: Vim
|
|
||||||
)
|
|
||||||
(\w+) # $3: language
|
|
||||||
(?:
|
|
||||||
(?(1) # If $1 matched...
|
|
||||||
;?\s* -\*- | # then close Emacs syntax
|
|
||||||
: \s* \*\/ # otherwise close Vim syntax
|
|
||||||
)
|
|
||||||
)
|
|
||||||
/x
|
|
||||||
|
|
||||||
# Public: Detects language based on Vim and Emacs modelines
|
# Public: Detects language based on Vim and Emacs modelines
|
||||||
#
|
#
|
||||||
@@ -37,8 +26,8 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns a String or nil
|
# Returns a String or nil
|
||||||
def self.modeline(data)
|
def self.modeline(data)
|
||||||
data.match(Regex)
|
match = data.match(EmacsModeline) || data.match(VimModeline)
|
||||||
lang = $3
|
match[1] if match
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user