mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Clearer regex
This commit is contained in:
@@ -23,8 +23,20 @@ module Linguist
|
||||
#
|
||||
# Returns a String or nil
|
||||
def self.modeline(data)
|
||||
data.lines.first(5).any? { |l| l.match(/\W(?:filetype=|ft=|-\*- mode:|-\*-)\s*(\w+)/) }
|
||||
lang = $1
|
||||
pattern = %q{(?:
|
||||
(-\*- \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
|
||||
)
|
||||
)}
|
||||
|
||||
data.lines.first(5).any? { |l| l.match(/#{pattern}/x) }
|
||||
lang = $3
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user