module Cog::Config::LanguageConfig
{Config} methods related to languages
Public Instance Methods
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. @param key [:String] the lanuage identifier. Type cog language list
to see the possible values @option opt [:String] :ext (nil) a file extension which will map to a language identifier. Type cog language map
to see mapped extensions @option opt [:String] :filename (nil) a filename or path to file which will map to a language identifier @yield within this block the {#active_language} will be set to the desired value @return [Object] the value returned by the block
# 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
@return [Language] language which is active in the current context
# File lib/cog/config/language_config.rb, line 8 def active_language @active_languages.last end
@param key [String] the language key @return [Language] the language for the given key
# File lib/cog/config/language_config.rb, line 50 def language(key) @language[key] end
@return [Array<String>] list of file extensions for supported languages
# File lib/cog/config/language_config.rb, line 44 def language_extensions @language_extension_map.keys end
@param path [String] the file system extension, or full path to a file @return [Language, nil] the language for the given extension
# 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
@return [Array<Language>] current configuration of supported languages
# File lib/cog/config/language_config.rb, line 66 def language_summary @language.values.sort end