module Sprockets::Transformers

def expand_transform_accepts(parsed_accepts)

Returns an expanded Array of q values.

# => [['application/javascript', 1.0], ['text/coffeescript', 0.8]]
expand_transform_accepts([['application/javascript', 1.0]])

Examples

parsed_accepts - Array of accept q values

Internal: Expand accept type list to include possible transformed types.
def expand_transform_accepts(parsed_accepts)
  accepts = []
  parsed_accepts.each do |(type, q)|
    accepts.push([type, q])
    inverted_transformers[type].keys.each do |subtype|
      accepts.push([subtype, q * 0.8])
    end
  end
  accepts
end

def register_transformer(from, to, proc)

Returns nothing.

register_transformer 'image/svg+xml', 'image/png', ConvertSvgToPng

ConvertCoffeeScriptToJavaScript
register_transformer 'text/coffeescript', 'application/javascript',

Examples

proc - Callable block that accepts an input Hash.
to - String mime type
from - String mime type

Public: Register a transformer from and to a mime type.
def register_transformer(from, to, proc)
  mutate_hash_config(:transformers, from) do |transformers|
    transformers.merge(to => proc)
  end
  mutate_hash_config(:inverted_transformers, to) do |transformers|
    transformers.merge(from => proc)
  end
end

def resolve_transform_type(type, accept)

Returns String mime type or nil is no type satisfied the accept value.

# => nil
resolve_transform_type('text/css', 'image/png')

# => 'image/png'
resolve_transform_type('image/svg+xml', 'image/png, image/*')

# => 'text/plain'
resolve_transform_type('text/plain', 'text/plain')

Examples

accept - String accept type list (default: '*/*')
type - String from mime type

transformed to.
Internal: Resolve target mime type that the source type should be
def resolve_transform_type(type, accept)
  find_best_mime_type_match(accept || '*/*', [type].compact + transformers[type].keys)
end

def unwrap_transformer(from, to)

Returns Array of Procs.

to - String mime type
from - String mime type

Internal: Find and load transformer by from and to mime type.
def unwrap_transformer(from, to)
  if processor = transformers[from][to]
    [unwrap_processor(processor)]
  else
    []
  end
end