Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect the language of a code snippet in memory #2497

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 2 additions & 43 deletions lib/linguist/file_blob.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'linguist/blob_helper'
require 'linguist/memory_blob'

module Linguist
# A FileBlob is a wrapper around a File object to make it quack
# like a Grit::Blob. It provides the basic interface: `name`,
# `data`, `path` and `size`.
class FileBlob
class FileBlob < MemoryBlob
include BlobHelper

# Public: Initialize a new FileBlob from a path
Expand All @@ -18,34 +19,13 @@ def initialize(path, base_path = nil)
@path = base_path ? path.sub("#{base_path}/", '') : path
end

# Public: Filename
#
# Examples
#
# FileBlob.new("/path/to/linguist/lib/linguist.rb").path
# # => "/path/to/linguist/lib/linguist.rb"
#
# FileBlob.new("/path/to/linguist/lib/linguist.rb",
# "/path/to/linguist").path
# # => "lib/linguist.rb"
#
# Returns a String
attr_reader :path

# Public: Read file permissions
#
# Returns a String like '100644'
def mode
File.stat(@fullpath).mode.to_s(8)
end

# Public: File name
#
# Returns a String
def name
File.basename(@fullpath)
end

# Public: Read file contents.
#
# Returns a String.
Expand All @@ -59,26 +39,5 @@ def data
def size
File.size(@fullpath)
end

# Public: Get file extension.
#
# Returns a String.
def extension
extensions.last || ""
end

# Public: Return an array of the file extensions
#
# >> Linguist::FileBlob.new("app/views/things/index.html.erb").extensions
# => [".html.erb", ".erb"]
#
# Returns an Array
def extensions
basename, *segments = name.downcase.split(".")

segments.map.with_index do |segment, index|
"." + segments[index..-1].join(".")
end
end
end
end
73 changes: 73 additions & 0 deletions lib/linguist/memory_blob.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'linguist/blob_helper'

module Linguist
# A MemoryBlob is a wrapper around the content of a file to make it quack
# like a Grit::Blob. It provides the basic interface: `name`,
# `data`, `path` and `size`.
class MemoryBlob
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just calling this Linguist::Blob?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that could make sense since Linguist::FileBlob inherits it.
I'll change it.

include BlobHelper

# Public: Initialize a new MemoryBlob.
#
# path - A path String (does not necessarily exists on the file system).
# content - Content of the file.
#
# Returns a FileBlob.
def initialize(path, content)
@path = path
@content = content
end

# Public: Filename
#
# Examples
#
# MemoryBlob.new("/path/to/linguist/lib/linguist.rb", "").path
# # => "/path/to/linguist/lib/linguist.rb"
#
# Returns a String
attr_reader :path

# Public: File name
#
# Returns a String
def name
File.basename(@path)
end

# Public: File contents.
#
# Returns a String.
def data
@content
end

# Public: Get byte size
#
# Returns an Integer.
def size
@content.bytesize
end

# Public: Get file extension.
#
# Returns a String.
def extension
extensions.last || ""
end

# Public: Return an array of the file extensions
#
# >> Linguist::FileBlob.new("app/views/things/index.html.erb").extensions
# => [".html.erb", ".erb"]
#
# Returns an Array
def extensions
basename, *segments = name.downcase.split(".")

segments.map.with_index do |segment, index|
"." + segments[index..-1].join(".")
end
end
end
end
21 changes: 17 additions & 4 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@
require "mocha/setup"
require "linguist"
require 'color-proximity'
require "linguist/memory_blob"

def fixtures_path
File.expand_path("../fixtures", __FILE__)
end

def fixture_blob(name)
name = File.join(fixtures_path, name) unless name =~ /^\//
Linguist::FileBlob.new(name, fixtures_path)
filepath = (name =~ /^\//)? name : File.join(fixtures_path, name)
Linguist::FileBlob.new(filepath, fixtures_path)
end

def fixture_blob_memory(name)
filepath = (name =~ /^\//)? name : File.join(fixtures_path, name)
content = File.read(filepath)
Linguist::MemoryBlob.new(name, content)
end

def samples_path
File.expand_path("../../samples", __FILE__)
end

def sample_blob(name)
name = File.join(samples_path, name) unless name =~ /^\//
Linguist::FileBlob.new(name, samples_path)
filepath = (name =~ /^\//)? name : File.join(samples_path, name)
Linguist::FileBlob.new(filepath, samples_path)
end

def sample_blob_memory(name)
filepath = (name =~ /^\//)? name : File.join(samples_path, name)
content = File.read(filepath)
Linguist::MemoryBlob.new(name, content)
end
Loading