From e7f57796595f11117b53ca37502056ce83aac85b Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Thu, 29 Jan 2015 16:16:41 -0600 Subject: [PATCH] Break modelines into two regular expressions This makes them easier to read and maintains Ruby 1.9 compatibility --- lib/linguist/strategy/modeline.rb | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/lib/linguist/strategy/modeline.rb b/lib/linguist/strategy/modeline.rb index b23fe08e..176bd292 100644 --- a/lib/linguist/strategy/modeline.rb +++ b/lib/linguist/strategy/modeline.rb @@ -1,19 +1,8 @@ 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 + EmacsModeline = /-\*-\s*(?:mode:)?\s*(\w+);?\s*-\*-/ + VimModeline = /\/\*\s*vim:\s*set\s*(?:ft|filetype)=(\w+):\s*\*\// # Public: Detects language based on Vim and Emacs modelines # @@ -37,8 +26,8 @@ module Linguist # # Returns a String or nil def self.modeline(data) - data.match(Regex) - lang = $3 + match = data.match(EmacsModeline) || data.match(VimModeline) + match[1] if match end end end