module Linguist::BlobHelper

DEPRECATED Avoid mixing into Blob classes. Prefer functional interfaces like ‘Language.detect` over `Blob#language`. Functions are much easier to cache and compose.

Avoid adding additional bloat to this module.

BlobHelper is a mixin for Blobish classes that respond to “name”, “data” and “size” such as Grit::Blob.

Constants

MEGABYTE
VendoredRegexp

Public Instance Methods

_mime_type() click to toggle source

Internal: Lookup mime type for extension.

Returns a MIME::Type

# File lib/linguist/blob_helper.rb, line 34
def _mime_type
  if defined? @_mime_type
    @_mime_type
  else
    guesses = ::MIME::Types.type_for(extname.to_s)

    # Prefer text mime types over binary
    @_mime_type = guesses.detect { |type| type.ascii? } ||
      # Otherwise use the first guess
      guesses.first
  end
end
binary?() click to toggle source

Public: Is the blob binary?

Return true or false

# File lib/linguist/blob_helper.rb, line 126
def binary?
  # Large blobs aren't even loaded into memory
  if data.nil?
    true

  # Treat blank files as text
  elsif data == ""
    false

  # Charlock doesn't know what to think
  elsif encoding.nil?
    true

  # If Charlock says its binary
  else
    detect_encoding[:type] == :binary
  end
end
binary_mime_type?() click to toggle source

Internal: Is the blob binary according to its mime type

Return true or false

# File lib/linguist/blob_helper.rb, line 62
def binary_mime_type?
  _mime_type ? _mime_type.binary? : false
end
colorize(options = {}) click to toggle source

Public: Highlight syntax of blob

options - A Hash of options (defaults to {})

Returns html String

# File lib/linguist/blob_helper.rb, line 301
def colorize(options = {})
  return unless safe_to_colorize?
  options[:options] ||= {}
  options[:options][:encoding] ||= encoding
  lexer.highlight(data, options)
end
content_type() click to toggle source

Public: Get the Content-Type header value

This value is used when serving raw blobs.

Examples

# => 'text/plain; charset=utf-8'
# => 'application/octet-stream'

Returns a content type String.

# File lib/linguist/blob_helper.rb, line 85
def content_type
  @content_type ||= (binary_mime_type? || binary?) ? mime_type :
    (encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain")
end
csv?() click to toggle source

Public: Is this blob a CSV file?

Return true or false

# File lib/linguist/blob_helper.rb, line 169
def csv?
  text? && extname.downcase == '.csv'
end
detect_encoding() click to toggle source

Try to guess the encoding

Returns: a Hash, with :encoding, :confidence, :type

this will return nil if an error occurred during detection or
no valid encoding could be found
# File lib/linguist/blob_helper.rb, line 119
def detect_encoding
  @detect_encoding ||= CharlockHolmes::EncodingDetector.new.detect(data) if data
end
disposition() click to toggle source

Public: Get the Content-Disposition header value

This value is used when serving raw blobs.

# => "attachment; filename=file.tar"
# => "inline"

Returns a content disposition String.

# File lib/linguist/blob_helper.rb, line 98
def disposition
  if text? || image?
    'inline'
  elsif name.nil?
    "attachment"
  else
    "attachment; filename=#{EscapeUtils.escape_url(File.basename(name))}"
  end
end
encoding() click to toggle source
# File lib/linguist/blob_helper.rb, line 108
def encoding
  if hash = detect_encoding
    hash[:encoding]
  end
end
extname() click to toggle source

Public: Get the extname of the path

Examples

blob(name='foo.rb').extname
# => '.rb'

Returns a String

# File lib/linguist/blob_helper.rb, line 27
def extname
  File.extname(name.to_s)
end
generated?() click to toggle source

Public: Is the blob a generated file?

Generated source code is suppressed in diffs and is ignored by language statistics.

May load Blob#data

Return true or false

# File lib/linguist/blob_helper.rb, line 268
def generated?
  @_generated ||= Generated.generated?(name, lambda { data })
end
high_ratio_of_long_lines?() click to toggle source

Internal: Does the blob have a ratio of long lines?

Return true or false

# File lib/linguist/blob_helper.rb, line 199
def high_ratio_of_long_lines?
  return false if loc == 0
  size / loc > 5000
end
image?() click to toggle source

Public: Is the blob a supported image format?

Return true or false

# File lib/linguist/blob_helper.rb, line 155
def image?
  ['.png', '.jpg', '.jpeg', '.gif'].include?(extname.downcase)
end
language() click to toggle source

Public: Detects the Language of the blob.

May load Blob#data

Returns a Language or nil if none is detected

# File lib/linguist/blob_helper.rb, line 277
def language
  return @language if defined? @language

  if defined?(@data) && @data.is_a?(String)
    data = @data
  else
    data = lambda { (binary_mime_type? || binary?) ? "" : self.data }
  end

  @language = Language.detect(name.to_s, data, mode)
end
large?() click to toggle source

Public: Is the blob too big to load?

Return true or false

# File lib/linguist/blob_helper.rb, line 185
def large?
  size.to_i > MEGABYTE
end
lexer() click to toggle source

Internal: Get the lexer of the blob.

Returns a Lexer.

# File lib/linguist/blob_helper.rb, line 292
def lexer
  language ? language.lexer : nil
end
likely_binary?() click to toggle source

Internal: Is the blob binary according to its mime type, overriding it if we have better data from the languages.yml database.

Return true or false

# File lib/linguist/blob_helper.rb, line 71
def likely_binary?
  binary_mime_type? && !Language.find_by_filename(name)
end
lines() click to toggle source

Public: Get each line of data

Requires Blob#data

Returns an Array of lines

# File lib/linguist/blob_helper.rb, line 233
def lines
  @lines ||=
    if viewable? && data
      data.split(/\r\n|\r|\n/, -1)
    else
      []
    end
end
loc() click to toggle source

Public: Get number of lines of code

Requires Blob#data

Returns Integer

# File lib/linguist/blob_helper.rb, line 247
def loc
  lines.size
end
mime_type() click to toggle source

Public: Get the actual blob mime type

Examples

# => 'text/plain'
# => 'text/html'

Returns a mime type String.

# File lib/linguist/blob_helper.rb, line 55
def mime_type
  _mime_type ? _mime_type.to_s : 'text/plain'
end
pdf?() click to toggle source

Public: Is the blob a PDF?

Return true or false

# File lib/linguist/blob_helper.rb, line 176
def pdf?
  extname.downcase == '.pdf'
end
safe_to_colorize?() click to toggle source

Public: Is the blob safe to colorize?

Return true or false

# File lib/linguist/blob_helper.rb, line 192
def safe_to_colorize?
  !large? && text? && !high_ratio_of_long_lines?
end
sloc() click to toggle source

Public: Get number of source lines of code

Requires Blob#data

Returns Integer

# File lib/linguist/blob_helper.rb, line 256
def sloc
  lines.grep(/\S/).size
end
solid?() click to toggle source

Public: Is the blob a supported 3D model format?

Return true or false

# File lib/linguist/blob_helper.rb, line 162
def solid?
  extname.downcase == '.stl'
end
text?() click to toggle source

Public: Is the blob text?

Return true or false

# File lib/linguist/blob_helper.rb, line 148
def text?
  !binary?
end
vendored?() click to toggle source

Public: Is the blob in a vendored directory?

Vendored files are ignored by language statistics.

See “vendor.yml” for a list of vendored conventions that match this pattern.

Return true or false

# File lib/linguist/blob_helper.rb, line 224
def vendored?
  name =~ VendoredRegexp ? true : false
end
viewable?() click to toggle source

Public: Is the blob viewable?

Non-viewable blobs will just show a “View Raw” link

Return true or false

# File lib/linguist/blob_helper.rb, line 209
def viewable?
  !large? && text?
end