Break modelines into two regular expressions

This makes them easier to read and maintains Ruby 1.9 compatibility
This commit is contained in:
Brandon Keepers
2015-01-29 16:16:41 -06:00
parent 512cfc4858
commit e7f5779659

View File

@@ -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