Sha256: 5c4b9b9cc9c1606971b7044e7cd39e38ee31ba478086e18e57a313f0c9b7e593

Contents?: true

Size: 1.01 KB

Versions: 6

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+):/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

6 entries across 6 versions & 1 rubygems

Version Path
github-linguist-4.5.11 lib/linguist/strategy/modeline.rb
github-linguist-4.5.10 lib/linguist/strategy/modeline.rb
github-linguist-4.5.9 lib/linguist/strategy/modeline.rb
github-linguist-4.5.8 lib/linguist/strategy/modeline.rb
github-linguist-4.5.6 lib/linguist/strategy/modeline.rb
github-linguist-4.5.5 lib/linguist/strategy/modeline.rb