This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 24
/
engines.rb
74 lines (70 loc) · 2.22 KB
/
engines.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
require 'sprockets/eco_template'
require 'sprockets/ejs_template'
require 'sprockets/jst_processor'
require 'sprockets/utils'
require 'tilt'
module Sprockets
# `Engines` provides a global and `Environment` instance registry.
#
# An engine is a type of processor that is bound to an filename
# extension. `application.js.coffee` indicates that the
# `CoffeeScriptTemplate` engine will be ran on the file.
#
# Extensions can be stacked and will be evaulated from right to
# left. `application.js.coffee.erb` will first run `ERBTemplate`
# then `CoffeeScriptTemplate`.
#
# All `Engine`s must follow the `Tilt::Template` interface. It is
# recommended to subclass `Tilt::Template`.
#
# Its recommended that you register engine changes on your local
# `Environment` instance.
#
# environment.register_engine '.foo', FooProcessor
#
# The global registry is exposed for plugins to register themselves.
#
# Sprockets.register_engine '.sass', SassTemplate
#
module Engines
# Returns a `Hash` of `Engine`s registered on the `Environment`.
# If an `ext` argument is supplied, the `Engine` associated with
# that extension will be returned.
#
# environment.engines
# # => {".coffee" => CoffeeScriptTemplate, ".sass" => SassTemplate, ...}
#
# environment.engines('.coffee')
# # => CoffeeScriptTemplate
#
def engines(ext = nil)
if ext
ext = Sprockets::Utils.normalize_extension(ext)
@engines[ext]
else
@engines.dup
end
end
# Returns an `Array` of engine extension `String`s.
#
# environment.engine_extensions
# # => ['.coffee', '.sass', ...]
def engine_extensions
@engines.keys
end
# Registers a new Engine `klass` for `ext`. If the `ext` already
# has an engine registered, it will be overridden.
#
# environment.register_engine '.coffee', CoffeeScriptTemplate
#
def register_engine(ext, klass)
ext = Sprockets::Utils.normalize_extension(ext)
@engines[ext] = klass
end
private
def deep_copy_hash(hash)
initial = Hash.new { |h, k| h[k] = [] }
hash.inject(initial) { |h, (k, a)| h[k] = a.dup; h }
end
end
end