mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add find lexer by mimetype
This commit is contained in:
@@ -12,6 +12,7 @@ module Linguist
|
||||
@lexers = []
|
||||
@name_index = {}
|
||||
@alias_index = {}
|
||||
@mimetypes_index = {}
|
||||
|
||||
# Internal: Test if system has Pygments
|
||||
#
|
||||
@@ -58,6 +59,20 @@ module Linguist
|
||||
@alias_index[name]
|
||||
end
|
||||
|
||||
# Public: Look up Lexer by one of it's mime types.
|
||||
#
|
||||
# type - A mime type String.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Lexer.find_by_mimetype('application/x-ruby')
|
||||
# # => #<Lexer name="Ruby">
|
||||
#
|
||||
# Returns the Lexer or nil if none was found.
|
||||
def self.find_by_mimetype(type)
|
||||
@mimetypes_index[type]
|
||||
end
|
||||
|
||||
# Public: Look up Lexer by name or alias.
|
||||
#
|
||||
# name - A case-insensitive String name or alias
|
||||
@@ -132,6 +147,15 @@ module Linguist
|
||||
|
||||
@alias_index[name] = lexer
|
||||
end
|
||||
|
||||
lexer.mimetypes.each do |type|
|
||||
# All Lexer mimetypes should be unique. Warn if there is a duplicate.
|
||||
if @mimetypes_index.key?(name)
|
||||
warn "Duplicate mimetype: #{name}"
|
||||
end
|
||||
|
||||
@mimetypes_index[type] = lexer
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,6 +32,19 @@ class TestLexer < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
def test_find_by_mimetype
|
||||
assert_equal Lexer['Ruby'], Lexer.find_by_mimetype('text/x-ruby')
|
||||
assert_equal Lexer['Ruby'], Lexer.find_by_mimetype('application/x-ruby')
|
||||
end
|
||||
|
||||
def test_find_all_by_mimetype
|
||||
Lexer.all.each do |lexer|
|
||||
lexer.mimetypes.each do |type|
|
||||
assert_equal lexer, Lexer.find_by_mimetype(type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_name
|
||||
assert_equal 'Ruby', Lexer['Ruby'].name
|
||||
assert_equal 'Python', Lexer['Python'].name
|
||||
|
||||
Reference in New Issue
Block a user