From fadca563bc1a60f56ea5939a2f171f763902218e Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 29 Jan 2015 13:09:12 -0600 Subject: [PATCH] Move regex to a constant --- lib/linguist/strategy/modeline.rb | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/linguist/strategy/modeline.rb b/lib/linguist/strategy/modeline.rb index 7e4fda84..96a73ff8 100644 --- a/lib/linguist/strategy/modeline.rb +++ b/lib/linguist/strategy/modeline.rb @@ -1,6 +1,20 @@ module Linguist module Strategy 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 # # blob - An object that quacks like a blob. @@ -23,20 +37,7 @@ module Linguist # # Returns a String or nil def self.modeline(data) - 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) } + data.lines.first(5).any? { |l| l.match(Regex) } lang = $3 end end