module Sprockets::Mime

def compute_extname_map

def compute_extname_map
  graph = {}
  engine_extname_permutation = []
  4.times do |n|
    config[:engines].keys.permutation(n).each do |engine_extnames|
      engine_extname_permutation << engine_extnames
    end
  end
  mime_exts_grouped_by_mime_type = {}
  config[:mime_exts].each do |format_extname,format_type|
    mime_exts_grouped_by_mime_type[format_type] ||= []
    mime_exts_grouped_by_mime_type[format_type] << format_extname
  end
  ([nil] + pipelines.keys.map(&:to_s)).each do |pipeline|
    pipeline_extname = pipeline ? ".#{pipeline}" : ''.freeze
    engine_extname_permutation.each do |engine_extnames|
      mime_exts_grouped_by_mime_type.each do |format_type, format_extnames|
        type = format_type
        value = [type, engine_extnames, pipeline]
        format_extnames.each do |format_extname|
          key = "#{pipeline_extname}#{format_extname}#{engine_extnames.join}"
          graph[key] = value
        end
        if format_type == config[:engine_mime_types][engine_extnames.first]
          key =  "#{pipeline_extname}#{engine_extnames.join}"
          graph[key] = value
        end
      end
    end
    graph[pipeline_extname] = [nil, [], pipeline]
  end
  graph
end

def extname_map

def extname_map
  self.computed_config[:_extnames] ||= compute_extname_map
end

def mime_exts

Returns Hash.

value - MIME Type String
key - MIME extension String

mime_exts['.js'] #=> 'application/javascript'

Examples:

Used for internal fast lookup purposes.

Internal: Mapping of MIME extension Strings to MIME type Strings.
def mime_exts
  config[:mime_exts]
end

def mime_type_charset_detecter(mime_type)

Returns Proc detector or nil if none is available.

mime_type - String MIME type

Internal: Get detecter function for MIME type.
def mime_type_charset_detecter(mime_type)
  if type = config[:mime_types][mime_type]
    if detect = type[:charset]
      return detect
    end
  end
end

def mime_types

Returns Hash.

charset - Default Encoding or function to detect encoding
extensions - Array of extnames
value - Hash
key - MIME Type String

Public: Mapping of MIME type Strings to properties Hash.
def mime_types
  config[:mime_types]
end

def read_file(filename, content_type = nil)

encoding.
Returns String file contents transcoded to UTF-8 or in its external

content_type - String MIME type
filename - String path

Public: Read file on disk with MIME type specific encoding.
def read_file(filename, content_type = nil)
  data = File.binread(filename)
  if detect = mime_type_charset_detecter(content_type)
    detect.call(data).encode(Encoding::UTF_8, :universal_newline => true)
  else
    data
  end
end

def register_mime_type(mime_type, options = {})

Returns nothing.

See EncodingUtils.
charset: Proc/Method that detects the charset of a file.
extensions: Array of String extnames
options - Hash
mime_type - String MIME Type

Public: Register a new mime type.
def register_mime_type(mime_type, options = {})
  # Legacy extension argument, will be removed from 4.x
  if options.is_a?(String)
    options = { extensions: [options] }
  end
  extnames = Array(options[:extensions]).map { |extname|
    Sprockets::Utils.normalize_extension(extname)
  }
  charset = options[:charset]
  charset ||= :default if mime_type.start_with?('text/')
  charset = EncodingUtils::CHARSET_DETECT[charset] if charset.is_a?(Symbol)
  self.computed_config = {}
  self.config = hash_reassoc(config, :mime_exts) do |mime_exts|
    extnames.each do |extname|
      mime_exts[extname] = mime_type
    end
    mime_exts
  end
  self.config = hash_reassoc(config, :mime_types) do |mime_types|
    type = { extensions: extnames }
    type[:charset] = charset if charset
    mime_types.merge(mime_type => type)
  end
end