engines.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. require 'sprockets/eco_template'
  2. require 'sprockets/ejs_template'
  3. require 'sprockets/jst_processor'
  4. require 'sprockets/utils'
  5. require 'tilt'
  6. module Sprockets
  7. # `Engines` provides a global and `Environment` instance registry.
  8. #
  9. # An engine is a type of processor that is bound to an filename
  10. # extension. `application.js.coffee` indicates that the
  11. # `CoffeeScriptTemplate` engine will be ran on the file.
  12. #
  13. # Extensions can be stacked and will be evaulated from right to
  14. # left. `application.js.coffee.erb` will first run `ERBTemplate`
  15. # then `CoffeeScriptTemplate`.
  16. #
  17. # All `Engine`s must follow the `Tilt::Template` interface. It is
  18. # recommended to subclass `Tilt::Template`.
  19. #
  20. # Its recommended that you register engine changes on your local
  21. # `Environment` instance.
  22. #
  23. # environment.register_engine '.foo', FooProcessor
  24. #
  25. # The global registry is exposed for plugins to register themselves.
  26. #
  27. # Sprockets.register_engine '.sass', SassTemplate
  28. #
  29. module Engines
  30. # Returns an `Array` of `Engine`s registered on the
  31. # `Environment`. If an `ext` argument is supplied, the `Engine`
  32. # register under that extension will be returned.
  33. #
  34. # environment.engines
  35. # # => [CoffeeScriptTemplate, SassTemplate, ...]
  36. #
  37. # environment.engines('.coffee')
  38. # # => CoffeeScriptTemplate
  39. #
  40. def engines(ext = nil)
  41. if ext
  42. ext = Sprockets::Utils.normalize_extension(ext)
  43. @engines[ext]
  44. else
  45. @engines.dup
  46. end
  47. end
  48. # Returns an `Array` of engine extension `String`s.
  49. #
  50. # environment.engine_extensions
  51. # # => ['.coffee', '.sass', ...]
  52. def engine_extensions
  53. @engines.keys
  54. end
  55. # Registers a new Engine `klass` for `ext`. If the `ext` already
  56. # has an engine registered, it will be overridden.
  57. #
  58. # environment.register_engine '.coffee', CoffeeScriptTemplate
  59. #
  60. def register_engine(ext, klass)
  61. ext = Sprockets::Utils.normalize_extension(ext)
  62. @engines[ext] = klass
  63. end
  64. private
  65. def deep_copy_hash(hash)
  66. initial = Hash.new { |h, k| h[k] = [] }
  67. hash.inject(initial) { |h, (k, a)| h[k] = a.dup; h }
  68. end
  69. end
  70. end