Module: Cog::Config::LanguageConfig

Included in:
Cog::Config
Defined in:
lib/cog/config/language_config.rb

Overview

Cog::Config methods related to languages

Instance Method Summary collapse

Instance Method Details

#activate_language(key, opt = {}) { ... } ⇒ Object

Activate a given language within the scope of the provided block. Either provide key, :ext, or :filename but not more than one. If the extension does not match any of the supported languages, the #active_language will not change, but the block will still be called.

Options Hash (opt):

  • :ext (:String) — default: nil

    a file extension which will map to a language identifier. Type cog language map to see mapped extensions

  • :filename (:String) — default: nil

    a filename or path to file which will map to a language identifier

Yields:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cog/config/language_config.rb', line 19

def activate_language(key, opt={}, &block)
  opt, key = key, nil if key.is_a? Hash
  key = if opt[:ext]
    ext = opt[:ext].to_s.downcase
    ext = ext.slice(1..-1) if ext.start_with?('.')
    @language_extension_map[ext] unless ext.empty?
  elsif opt[:filename]
    ext = File.extname(opt[:filename]).slice(1..-1)
    @language_extension_map[ext] unless ext.nil? || ext.empty?
  else
    key
  end
  if key
    @active_languages << @language[key]
    if block
      r = block.call
      @active_languages.pop
      r
    end
  else
    block.call
  end
end

#active_languageLanguage



8
9
10
# File 'lib/cog/config/language_config.rb', line 8

def active_language
  @active_languages.last
end

#language(key) ⇒ Language



50
51
52
# File 'lib/cog/config/language_config.rb', line 50

def language(key)
  @language[key]
end

#language_extensionsArray<String>



44
45
46
# File 'lib/cog/config/language_config.rb', line 44

def language_extensions
  @language_extension_map.keys
end

#language_for(path) ⇒ Language?



56
57
58
59
60
61
62
63
# File 'lib/cog/config/language_config.rb', line 56

def language_for(path)
  ext = File.extname(path.to_s)
  ext = path.to_s if ext.empty?
  ext = ext.downcase
  ext = ext.slice(1..-1) if ext.start_with? '.'
  key = @language_extension_map[ext]
  @language[key] if key
end

#language_summaryArray<Language>



66
67
68
# File 'lib/cog/config/language_config.rb', line 66

def language_summary
  @language.values.sort
end