Class: Linguist::Strategy::Modeline

Inherits:
Object
  • Object
show all
Defined in:
lib/linguist/strategy/modeline.rb

Constant Summary collapse

EMACS_MODELINE =
/-\*-\s*(?:(?!mode)[\w-]+\s*:\s*(?:[\w+-]+)\s*;?\s*)*(?:mode\s*:)?\s*([\w+-]+)\s*(?:;\s*(?!mode)[\w-]+\s*:\s*[\w+-]+\s*)*;?\s*-\*-/i
VIM_MODELINE_1 =

First form vim modeline [text]whitevi:|vim:|ex:options ex: ‘vim: syntax=ruby’

/(?:vim|vi|ex):\s*(?:ft|filetype|syntax)=(\w+)\s?/i
VIM_MODELINE_2 =

Second form vim modeline (compatible with some versions of Vi)

text]whitevi:|vim:|Vim:|ex:se[t

options:[text]

ex: ‘vim set syntax=ruby:’

/(?:vim|vi|Vim|ex):\s*se(?:t)?.*\s(?:ft|filetype|syntax)=(\w+)\s?.*:/i
MODELINES =
[EMACS_MODELINE, VIM_MODELINE_1, VIM_MODELINE_2]

Class Method Summary collapse

Class Method Details

.call(blob, _ = nil) ⇒ Object

Public: Detects language based on Vim and Emacs modelines

blob - An object that quacks like a blob.

Examples

Modeline.call(FileBlob.new("path/to/file"))

Returns an Array with one Language if the blob has a Vim or Emacs modeline that matches a Language name or alias. Returns an empty array if no match.



28
29
30
# File 'lib/linguist/strategy/modeline.rb', line 28

def self.call(blob, _ = nil)
  Array(Language.find_by_alias(modeline(blob.data)))
end

.modeline(data) ⇒ Object

Public: Get the modeline from the first n-lines of the file

Returns a String or nil



35
36
37
38
# File 'lib/linguist/strategy/modeline.rb', line 35

def self.modeline(data)
  match = MODELINES.map { |regex| data.match(regex) }.reject(&:nil?).first
  match[1] if match
end