diff --git a/lib/linguist/lexer.rb b/lib/linguist/lexer.rb index d3642081..0b256337 100644 --- a/lib/linguist/lexer.rb +++ b/lib/linguist/lexer.rb @@ -10,6 +10,7 @@ module Linguist # mimetypes - Mime types (application/javascript) class Lexer < Struct.new(:name, :aliases, :filenames, :mimetypes) @lexers = [] + @index = {} @name_index = {} @alias_index = {} @mimetypes_index = {} @@ -31,6 +32,18 @@ module Linguist @lexers end + # Public: Look up Lexer by name or alias. + # + # name - A String name or alias + # + # Lexer['Ruby'] + # => # + # + # Returns the Lexer or nil if none was found. + def self.[](name) + @index[name] + end + # Public: Look up Lexer by its proper name. # # name - The String name of the Lexer @@ -73,18 +86,6 @@ module Linguist @mimetypes_index[type] end - # Public: Look up Lexer by name or alias. - # - # name - A String name or alias - # - # Lexer['Ruby'] - # => # - # - # Returns the Lexer or nil if none was found. - def self.[](name) - find_by_name(name) || find_by_alias(name) - end - # Public: Return a alias of the Lexer to pass to Pygments. # # The alias we choose is arbitrary. @@ -137,7 +138,7 @@ module Linguist warn "Duplicate lexer name: #{lexer.name}" end - @name_index[lexer.name] = lexer + @index[lexer.name] = @name_index[lexer.name] = lexer lexer.aliases.each do |name| # All Lexer aliases should be unique. Warn if there is a duplicate. @@ -145,7 +146,7 @@ module Linguist warn "Duplicate alias: #{name}" end - @alias_index[name] = lexer + @index[name] = @alias_index[name] = lexer end lexer.mimetypes.each do |type|