Class: LanguageSniffer::Language

Inherits:
Object
  • Object
show all
Defined in:
lib/language_sniffer/language.rb

Overview

Language names that are recognizable by GitHub. Defined languages can be highlighted, searched and listed under the Top Languages page.

Languages are defined in ‘lib/language_sniffer/languages.yml`.

Constant Summary collapse

TYPES =

Valid Languages types

[:data, :markup, :programming]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Language

Internal: Initialize a new Language

attributes - A hash of attributes



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/language_sniffer/language.rb', line 174

def initialize(attributes = {})
  # @name is required
  @name = attributes[:name] || raise(ArgumentError, "missing name")

  # Set type
  @type = attributes[:type] ? attributes[:type].to_sym : nil

  # Set aliases
  @aliases = [default_alias_name] + (attributes[:aliases] || [])

  # Set pygments lexer
  @lexer = attributes[:lexer] || name

  # Set legacy search term
  @search_term = attributes[:search_term] || default_alias_name

  # Set extensions or default to [].
  @extensions = attributes[:extensions] || []
  @overrides  = attributes[:overrides]  || []
  @filenames  = attributes[:filenames]  || []

  @primary_extension = attributes[:primary_extension] || default_primary_extension || extensions.first

  # Prepend primary extension unless its already included
  if primary_extension && !extensions.include?(primary_extension)
    @extensions = [primary_extension] + extensions
  end

  # If group name is set, save the name so we can lazy load it later
  if attributes[:group_name]
    @group = nil
    @group_name = attributes[:group_name]

  # Otherwise we can set it to self now
  else
    @group = self
  end
end

Instance Attribute Details

#aliasesObject (readonly)

Public: Get aliases

Examples

Language['C++'].aliases
# => ["cpp"]

Returns an Array of String names



242
243
244
# File 'lib/language_sniffer/language.rb', line 242

def aliases
  @aliases
end

#extensionsObject (readonly)

Public: Get extensions

Examples

# => ['.rb', '.rake', ...]

Returns the extensions Array



262
263
264
# File 'lib/language_sniffer/language.rb', line 262

def extensions
  @extensions
end

#filenamesObject (readonly)

Public: Get filenames

Examples

# => ['Rakefile', ...]

Returns the extensions Array



290
291
292
# File 'lib/language_sniffer/language.rb', line 290

def filenames
  @filenames
end

#lexerObject (readonly)

Public: Get pygments lexer name.

Returns a lexer name or nil.



232
233
234
# File 'lib/language_sniffer/language.rb', line 232

def lexer
  @lexer
end

#nameObject (readonly)

Public: Get proper name

Examples

# => "Ruby"
# => "Python"
# => "Perl"

Returns the name String



222
223
224
# File 'lib/language_sniffer/language.rb', line 222

def name
  @name
end

#overridesObject (readonly)

Internal: Get overridden extensions.

Returns the extensions Array.



281
282
283
# File 'lib/language_sniffer/language.rb', line 281

def overrides
  @overrides
end

#primary_extensionObject (readonly)

Deprecated: Get primary extension

Defaults to the first extension but can be overriden in the languages.yml.

The primary extension can not be nil. Tests should verify this.

This attribute is only used by app/helpers/gists_helper.rb for creating the language dropdown. It really should be using ‘name` instead. Would like to drop primary extension.

Returns the extension String.



276
277
278
# File 'lib/language_sniffer/language.rb', line 276

def primary_extension
  @primary_extension
end

#search_termObject (readonly)

Deprecated: Get code search term

Examples

# => "ruby"
# => "python"
# => "perl"

Returns the name String



253
254
255
# File 'lib/language_sniffer/language.rb', line 253

def search_term
  @search_term
end

#typeObject (readonly)

Public: Get type.

Returns a type Symbol or nil.



227
228
229
# File 'lib/language_sniffer/language.rb', line 227

def type
  @type
end

Class Method Details

.[](name) ⇒ Object

Public: Look up Language by its name.

name - The String name of the Language

Examples

Language['Ruby']
# => #<Language name="Ruby">

Language['ruby']
# => #<Language name="Ruby">

Returns the Language or nil if none was found.



167
168
169
# File 'lib/language_sniffer/language.rb', line 167

def self.[](name)
  @index[name]
end

.allObject

Public: Get all Languages

Returns an Array of Languages



93
94
95
# File 'lib/language_sniffer/language.rb', line 93

def self.all
  @languages
end

.ambiguous?(extension) ⇒ Boolean

Internal: Test if extension maps to multiple Languages.

Returns true or false.

Returns:

  • (Boolean)


23
24
25
# File 'lib/language_sniffer/language.rb', line 23

def self.ambiguous?(extension)
  @overrides.include?(extension)
end

.create(attributes = {}) ⇒ Object

Internal: Create a new Language object

attributes - A hash of attributes

Returns a Language object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/language_sniffer/language.rb', line 39

def self.create(attributes = {})
  language = new(attributes)

  @languages << language

  # All Language names should be unique. Warn if there is a duplicate.
  if @name_index.key?(language.name)
    warn "Duplicate language name: #{language.name}"
  end

  # Language name index
  @index[language.name] = @name_index[language.name] = language

  language.aliases.each do |name|
    # All Language aliases should be unique. Warn if there is a duplicate.
    if @alias_index.key?(name)
      warn "Duplicate alias: #{name}"
    end

    @index[name] = @alias_index[name] = language
  end

  language.extensions.each do |extension|
    if extension !~ /^\./
      warn "Extension is missing a '.': #{extension.inspect}"
    end

    unless ambiguous?(extension)
      # Index the extension with a leading ".": ".rb"
      @extension_index[extension] = language

      # Index the extension without a leading ".": "rb"
      @extension_index[extension.sub(/^\./, '')] = language
    end
  end

  language.overrides.each do |extension|
    if extension !~ /^\./
      warn "Extension is missing a '.': #{extension.inspect}"
    end

    @overrides[extension] = language
  end

  language.filenames.each do |filename|
    @filename_index[filename] = language
  end

  language
end

.find_by_alias(name) ⇒ Object

Public: Look up Language by one of its aliases.

name - A String alias of the Language

Examples

Language.find_by_alias('cpp')
# => #<Language name="C++">

Returns the Language or nil if none was found.



121
122
123
# File 'lib/language_sniffer/language.rb', line 121

def self.find_by_alias(name)
  @alias_index[name]
end

.find_by_extension(extension) ⇒ Object

Public: Look up Language by extension.

extension - The extension String. May include leading “.”

Examples

Language.find_by_extension('.rb')
# => #<Language name="Ruby">

Returns the Language or nil if none was found.



135
136
137
# File 'lib/language_sniffer/language.rb', line 135

def self.find_by_extension(extension)
  @extension_index[extension]
end

.find_by_filename(filename) ⇒ Object

Public: Look up Language by filename.

filename - The path String.

Examples

Language.find_by_filename('foo.rb')
# => #<Language name="Ruby">

Returns the Language or nil if none was found.



149
150
151
152
# File 'lib/language_sniffer/language.rb', line 149

def self.find_by_filename(filename)
  basename, extname = File.basename(filename), File.extname(filename)
  @filename_index[basename] || @extension_index[extname]
end

.find_by_name(name) ⇒ Object

Public: Look up Language by its proper name.

name - The String name of the Language

Examples

Language.find_by_name('Ruby')
# => #<Language name="Ruby">

Returns the Language or nil if none was found.



107
108
109
# File 'lib/language_sniffer/language.rb', line 107

def self.find_by_name(name)
  @name_index[name]
end

.overridden_extensionsObject

Include?: Return overridden extensions.

Returns extensions Array.



30
31
32
# File 'lib/language_sniffer/language.rb', line 30

def self.overridden_extensions
  @overrides.keys
end

Instance Method Details

#==(other) ⇒ Object



318
319
320
# File 'lib/language_sniffer/language.rb', line 318

def ==(other)
  eql?(other)
end

#default_alias_nameObject

Internal: Get default alias name

Returns the alias name String



295
296
297
# File 'lib/language_sniffer/language.rb', line 295

def default_alias_name
  name.downcase.gsub(/\s/, '-')
end

#default_primary_extensionObject

Internal: Get default primary extension.

Returns the extension String.



302
303
304
# File 'lib/language_sniffer/language.rb', line 302

def default_primary_extension
  extensions.first
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/language_sniffer/language.rb', line 322

def eql?(other)
  equal?(other)
end

#groupObject

Public: Get Language group

Returns a Language



309
310
311
# File 'lib/language_sniffer/language.rb', line 309

def group
  @group ||= Language.find_by_name(@group_name)
end

#hashObject



326
327
328
# File 'lib/language_sniffer/language.rb', line 326

def hash
  name.hash
end

#to_sObject

Public: Return name as String representation



314
315
316
# File 'lib/language_sniffer/language.rb', line 314

def to_s
  name
end