Sha256: e8d263d42e44bf451fdf5a6c6e322e1a344fff87ec977e56da74a6fdc04e76ad

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

Contents

module Linguist
  module Strategy
    class Modeline
      EmacsModeline = /-\*-\s*(?:(?!mode)[\w-]+\s*:\s*(?:[\w+-]+)\s*;?\s*)*(?:mode\s*:)?\s*([\w+-]+)\s*(?:;\s*(?!mode)[\w-]+\s*:\s*[\w+-]+\s*)*;?\s*-\*-/i
      VimModeline = /vim:\s*set.*\s(?:ft|filetype)=(\w+)\s?.*:/i

      # 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.
      def self.call(blob, _ = nil)
        Array(Language.find_by_alias(modeline(blob.data)))
      end

      # Public: Get the modeline from the first n-lines of the file
      #
      # Returns a String or nil
      def self.modeline(data)
        match = data.match(EmacsModeline) || data.match(VimModeline)
        match[1] if match
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
github-linguist-4.7.4 lib/linguist/strategy/modeline.rb
github-linguist-4.7.3 lib/linguist/strategy/modeline.rb
github-linguist-4.7.2 lib/linguist/strategy/modeline.rb
github-linguist-4.7.1 lib/linguist/strategy/modeline.rb
github-linguist-4.7.0 lib/linguist/strategy/modeline.rb
github-linguist-4.6.4 lib/linguist/strategy/modeline.rb
github-linguist-4.6.3 lib/linguist/strategy/modeline.rb
github-linguist-4.6.0 lib/linguist/strategy/modeline.rb
github-linguist-4.5.15 lib/linguist/strategy/modeline.rb
github-linguist-4.5.14 lib/linguist/strategy/modeline.rb
github-linguist-4.5.13 lib/linguist/strategy/modeline.rb