Add find lexer by mimetype

This commit is contained in:
Joshua Peek
2011-06-18 20:09:11 -05:00
parent c35519c893
commit e8c09e91d5
2 changed files with 40 additions and 3 deletions

View File

@@ -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

View File

@@ -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