mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-08 20:38:47 +00:00
Move regex to a constant
This commit is contained in:
@@ -1,6 +1,20 @@
|
|||||||
module Linguist
|
module Linguist
|
||||||
module Strategy
|
module Strategy
|
||||||
class Modeline
|
class Modeline
|
||||||
|
Regex = /
|
||||||
|
(?:
|
||||||
|
(-\*- \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
|
||||||
#
|
#
|
||||||
# blob - An object that quacks like a blob.
|
# blob - An object that quacks like a blob.
|
||||||
@@ -23,20 +37,7 @@ module Linguist
|
|||||||
#
|
#
|
||||||
# Returns a String or nil
|
# Returns a String or nil
|
||||||
def self.modeline(data)
|
def self.modeline(data)
|
||||||
regex =
|
data.lines.first(5).any? { |l| l.match(Regex) }
|
||||||
/(?:
|
|
||||||
(-\*- \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
|
|
||||||
|
|
||||||
data.lines.first(5).any? { |l| l.match(regex) }
|
|
||||||
lang = $3
|
lang = $3
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user