Permalink
Browse files

Add global extension registry

  • Loading branch information...
1 parent 90f8f1d commit 767f177908e297a6f2430f21b5a46fb67fa91885 @sstephenson committed May 26, 2011
View
@@ -11,6 +11,7 @@ module Sprockets
autoload :Context, "sprockets/context"
autoload :DirectiveProcessor, "sprockets/directive_processor"
autoload :EjsTemplate, "sprockets/ejs_template"
+ autoload :Engines, "sprockets/engines"
autoload :Environment, "sprockets/environment"
autoload :EnvironmentIndex, "sprockets/environment_index"
autoload :Error, "sprockets/errors"
@@ -20,6 +21,7 @@ module Sprockets
autoload :Server, "sprockets/server"
autoload :StaticAsset, "sprockets/static_asset"
autoload :StaticCompilation, "sprockets/static_compilation"
+ autoload :Utils, "sprockets/utils"
end
# TODO: Remove in 2.0.0 final
View
@@ -0,0 +1,38 @@
+require 'sprockets/ejs_template'
+require 'sprockets/jst_processor'
+require 'sprockets/utils'
+require 'tilt'
+
+module Sprockets
+ module Engines
+ def engines(ext = nil)
+ if ext
+ ext = Sprockets::Utils.normalize_extension(ext)
+ @engines[ext]
+ else
+ @engines.dup
+ end
+ end
+
+ def engine_extensions
+ @engines.keys
+ end
+
+ def register_engine(ext, klass)
+ ext = Sprockets::Utils.normalize_extension(ext)
+ @engines ||= {}
+ @engines[ext] = klass
+ end
+ end
+
+ extend Engines
+
+ register_engine '.coffee', Tilt::CoffeeScriptTemplate
+ register_engine '.erb', Tilt::ERBTemplate
+ register_engine '.ejs', EjsTemplate
+ register_engine '.jst', JstProcessor
+ register_engine '.less', Tilt::LessTemplate
+ register_engine '.sass', Tilt::SassTemplate
+ register_engine '.scss', Tilt::ScssTemplate
+ register_engine '.str', Tilt::StringTemplate
+end
@@ -23,24 +23,16 @@ def initialize(root = ".")
@static_root = nil
+ @engines = Sprockets.engines
+ @trail.extensions.replace(engine_extensions)
+
@mime_types = {}
- @engines = {}
@processors = Hash.new { |h, k| h[k] = [] }
@bundle_processors = Hash.new { |h, k| h[k] = [] }
register_mime_type 'text/css', '.css'
register_mime_type 'application/javascript', '.js'
- register_engine '.jst', JstProcessor
- register_engine '.ejs', EjsTemplate
-
- register_engine '.str', Tilt::StringTemplate
- register_engine '.erb', Tilt::ERBTemplate
- register_engine '.sass', Tilt::SassTemplate
- register_engine '.scss', Tilt::ScssTemplate
- register_engine '.less', Tilt::LessTemplate
- register_engine '.coffee', Tilt::CoffeeScriptTemplate
-
register_processor 'text/css', DirectiveProcessor
register_processor 'application/javascript', DirectiveProcessor
@@ -1,47 +1,36 @@
require 'sprockets/compressor'
+require 'sprockets/engines'
+require 'sprockets/utils'
require 'rack/mime'
-require 'tilt'
module Sprockets
module Processing
+ include Engines
+
def mime_types(ext = nil)
if ext.nil?
Rack::Mime::MIME_TYPES.merge(@mime_types)
else
- ext = normalize_extension(ext)
+ ext = Sprockets::Utils.normalize_extension(ext)
@mime_types[ext] || Rack::Mime::MIME_TYPES[ext]
end
end
def register_mime_type(mime_type, ext)
expire_index!
- ext = normalize_extension(ext)
+ ext = Sprockets::Utils.normalize_extension(ext)
@trail.extensions << ext
@mime_types[ext] = mime_type
end
- def engines(ext = nil)
- if ext
- ext = normalize_extension(ext)
- @engines[ext] || Tilt[ext]
- else
- @engines.dup
- end
- end
-
- def engine_extensions
- @engines.keys
- end
-
def format_extensions
@trail.extensions - @engines.keys
end
def register_engine(ext, klass)
expire_index!
- ext = normalize_extension(ext)
- @trail.extensions << ext
- @engines[ext] = klass
+ @trail.extensions << ext.to_s
+ super
end
def processors(mime_type = nil)
@@ -131,14 +120,5 @@ def deep_copy_hash(hash)
initial = Hash.new { |h, k| h[k] = [] }
hash.inject(initial) { |h, (k, a)| h[k] = a.dup; h }
end
-
- def normalize_extension(extension)
- extension = extension.to_s
- if extension[/^\./]
- extension
- else
- ".#{extension}"
- end
- end
end
end
View
@@ -0,0 +1,12 @@
+module Sprockets
+ module Utils
+ def self.normalize_extension(extension)
+ extension = extension.to_s
+ if extension[/^\./]
+ extension
+ else
+ ".#{extension}"
+ end
+ end
+ end
+end
@@ -0,0 +1 @@
+Hello world!
@@ -0,0 +1 @@
+console.log("Moo, #{RUBY_VERSION}");
View
@@ -0,0 +1,73 @@
+require 'sprockets_test'
+require 'sprockets/engines'
+require 'tilt'
+
+class AlertTemplate < Tilt::Template
+ def self.default_mime_type
+ 'application/javascript'
+ end
+
+ def prepare
+ end
+
+ def evaluate(scope, locals, &block)
+ "alert(#{data.inspect});"
+ end
+end
+
+class StringTemplate < Tilt::Template
+ def prepare
+ end
+
+ def evaluate(scope, locals, &block)
+ data.gsub(/#\{.*?\}/, "moo")
+ end
+end
+
+class TestEngines < Sprockets::TestCase
+ ORIGINAL_ENGINES = Sprockets.instance_variable_get(:@engines)
+
+ def setup
+ Sprockets.instance_variable_set(:@engines, ORIGINAL_ENGINES.dup)
+ end
+
+ def teardown
+ Sprockets.instance_variable_set(:@engines, ORIGINAL_ENGINES)
+ end
+
+ test "registering a global engine" do
+ Sprockets.register_engine ".alert", AlertTemplate
+ assert_equal AlertTemplate, Sprockets.engines("alert")
+ assert_equal AlertTemplate, Sprockets.engines(".alert")
+
+ env = new_environment
+ asset = env["hello.alert"]
+ assert_equal 'alert("Hello world!\n");', asset.to_s
+ assert_equal 'application/javascript', asset.content_type
+ end
+
+ test "overriding an engine globally" do
+ env1 = new_environment
+ assert_equal %(console.log("Moo, #{RUBY_VERSION}");\n), env1["moo.js"].to_s
+
+ Sprockets.register_engine ".str", StringTemplate
+ env2 = new_environment
+ assert_equal %(console.log("Moo, moo");\n), env2["moo.js"].to_s
+ end
+
+ test "overriding an engine in an environment" do
+ env1 = new_environment
+ env2 = new_environment
+
+ env1.register_engine ".str", StringTemplate
+ assert_equal %(console.log("Moo, moo");\n), env1["moo.js"].to_s
+
+ assert_equal %(console.log("Moo, #{RUBY_VERSION}");\n), env2["moo.js"].to_s
+ end
+
+ def new_environment
+ Sprockets::Environment.new.tap do |env|
+ env.append_path(fixture_path('engines'))
+ end
+ end
+end

0 comments on commit 767f177

Please sign in to comment.