Skip to content

Commit

Permalink
Template -> Processor
Browse files Browse the repository at this point in the history
Closes #681
  • Loading branch information
josh committed Dec 9, 2014
1 parent b4d8a66 commit 2c9333b
Show file tree
Hide file tree
Showing 23 changed files with 452 additions and 415 deletions.
18 changes: 12 additions & 6 deletions lib/sprockets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ module Sprockets
# Processing
autoload :Bundle, 'sprockets/bundle'
autoload :ClosureCompressor, 'sprockets/closure_compressor'
autoload :CoffeeScriptProcessor, 'sprockets/coffee_script_processor'
autoload :CoffeeScriptTemplate, 'sprockets/coffee_script_template'
autoload :Context, 'sprockets/context'
autoload :DirectiveProcessor, 'sprockets/directive_processor'
autoload :EcoProcessor, 'sprockets/eco_processor'
autoload :EcoTemplate, 'sprockets/eco_template'
autoload :EjsProcessor, 'sprockets/ejs_processor'
autoload :EjsTemplate, 'sprockets/ejs_template'
autoload :ERBProcessor, 'sprockets/erb_processor'
autoload :ERBTemplate, 'sprockets/erb_template'
autoload :JstProcessor, 'sprockets/jst_processor'
autoload :SassCompressor, 'sprockets/sass_compressor'
autoload :SassProcessor, 'sprockets/sass_processor'
autoload :SassTemplate, 'sprockets/sass_template'
autoload :ScssProcessor, 'sprockets/sass_processor'
autoload :ScssTemplate, 'sprockets/sass_template'
autoload :UglifierCompressor, 'sprockets/uglifier_compressor'
autoload :YUICompressor, 'sprockets/yui_compressor'
Expand Down Expand Up @@ -130,21 +136,21 @@ module Sprockets

# Mmm, CoffeeScript
register_mime_type 'text/coffeescript', extensions: ['.coffee']
register_engine '.coffee', LazyProcessor.new { CoffeeScriptTemplate }, mime_type: 'application/javascript'
register_engine '.coffee', LazyProcessor.new { CoffeeScriptProcessor }, mime_type: 'application/javascript'

# JST engines
register_mime_type 'text/eco', extensions: ['.eco']
register_mime_type 'text/ejs', extensions: ['.ejs']
register_engine '.jst', LazyProcessor.new { JstProcessor }, mime_type: 'application/javascript'
register_engine '.eco', LazyProcessor.new { EcoTemplate }, mime_type: 'application/javascript'
register_engine '.ejs', LazyProcessor.new { EjsTemplate }, mime_type: 'application/javascript'
register_engine '.eco', LazyProcessor.new { EcoProcessor }, mime_type: 'application/javascript'
register_engine '.ejs', LazyProcessor.new { EjsProcessor }, mime_type: 'application/javascript'

# CSS engines
register_mime_type 'text/sass', extensions: ['.sass']
register_mime_type 'text/scss', extensions: ['.scss']
register_engine '.sass', LazyProcessor.new { SassTemplate }, mime_type: 'text/css'
register_engine '.scss', LazyProcessor.new { ScssTemplate }, mime_type: 'text/css'
register_engine '.sass', LazyProcessor.new { SassProcessor }, mime_type: 'text/css'
register_engine '.scss', LazyProcessor.new { ScssProcessor }, mime_type: 'text/css'

# Other
register_engine '.erb', LazyProcessor.new { ERBTemplate }, mime_type: 'text/plain'
register_engine '.erb', LazyProcessor.new { ERBProcessor }, mime_type: 'text/plain'
end
23 changes: 23 additions & 0 deletions lib/sprockets/coffee_script_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'coffee_script'

module Sprockets
# Processor engine class for the CoffeeScript compiler.
# Depends on the `coffee-script` and `coffee-script-source` gems.
#
# For more infomation see:
#
# https://github.com/josh/ruby-coffee-script
#
module CoffeeScriptProcessor
VERSION = '1'
SOURCE_VERSION = ::CoffeeScript::Source.version

def self.call(input)
data = input[:data]
key = [self.name, SOURCE_VERSION, VERSION, data]
input[:cache].fetch(key) do
::CoffeeScript.compile(data)
end
end
end
end
23 changes: 3 additions & 20 deletions lib/sprockets/coffee_script_template.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
require 'coffee_script'
require 'sprockets/coffee_script_processor'

module Sprockets
# Template engine class for the CoffeeScript compiler.
# Depends on the `coffee-script` and `coffee-script-source` gems.
#
# For more infomation see:
#
# https://github.com/josh/ruby-coffee-script
#
module CoffeeScriptTemplate
VERSION = '1'
SOURCE_VERSION = ::CoffeeScript::Source.version

def self.call(input)
data = input[:data]
key = ['CoffeeScriptTemplate', SOURCE_VERSION, VERSION, data]
input[:cache].fetch(key) do
::CoffeeScript.compile(data)
end
end
end
# Deprecated
CoffeeScriptTemplate = CoffeeScriptProcessor
end
2 changes: 1 addition & 1 deletion lib/sprockets/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'sprockets/errors'

module Sprockets
# Deprecated: `Context` provides helper methods to all `Template` processors.
# Deprecated: `Context` provides helper methods to all processors.
# They are typically accessed by ERB templates. You can mix in custom helpers
# by injecting them into `Environment#context_class`. Do not mix them into
# `Context` directly.
Expand Down
29 changes: 29 additions & 0 deletions lib/sprockets/eco_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'eco'

module Sprockets
# Processor engine class for the Eco compiler. Depends on the `eco` gem.
#
# For more infomation see:
#
# https://github.com/sstephenson/ruby-eco
# https://github.com/sstephenson/eco
#
module EcoProcessor
VERSION = '1'

# Compile template data with Eco compiler.
#
# Returns a JS function definition String. The result should be
# assigned to a JS variable.
#
# # => "function(...) {...}"
#
def self.call(input)
data = input[:data]
key = [self.name, ::Eco::Source::VERSION, VERSION, data]
input[:cache].fetch(key) do
::Eco.compile(data)
end
end
end
end
29 changes: 3 additions & 26 deletions lib/sprockets/eco_template.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
require 'eco'
require 'sprockets/eco_processor'

module Sprockets
# Template engine class for the Eco compiler. Depends on the `eco` gem.
#
# For more infomation see:
#
# https://github.com/sstephenson/ruby-eco
# https://github.com/sstephenson/eco
#
module EcoTemplate
VERSION = '1'

# Compile template data with Eco compiler.
#
# Returns a JS function definition String. The result should be
# assigned to a JS variable.
#
# # => "function(...) {...}"
#
def self.call(input)
data = input[:data]
key = ['EcoTemplate', ::Eco::Source::VERSION, VERSION, data]
input[:cache].fetch(key) do
::Eco.compile(data)
end
end
end
# Deprecated
EcoTemplate = EcoProcessor
end
28 changes: 28 additions & 0 deletions lib/sprockets/ejs_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'ejs'

module Sprockets
# Processor engine class for the EJS compiler. Depends on the `ejs` gem.
#
# For more infomation see:
#
# https://github.com/sstephenson/ruby-ejs
#
module EjsProcessor
VERSION = '1'

# Compile template data with EJS compiler.
#
# Returns a JS function definition String. The result should be
# assigned to a JS variable.
#
# # => "function(obj){...}"
#
def self.call(input)
data = input[:data]
key = [self.name, VERSION, data]
input[:cache].fetch(key) do
::EJS.compile(data)
end
end
end
end
28 changes: 3 additions & 25 deletions lib/sprockets/ejs_template.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
require 'ejs'
require 'sprockets/ejs_processor'

module Sprockets
# Template engine class for the EJS compiler. Depends on the `ejs` gem.
#
# For more infomation see:
#
# https://github.com/sstephenson/ruby-ejs
#
module EjsTemplate
VERSION = '1'

# Compile template data with EJS compiler.
#
# Returns a JS function definition String. The result should be
# assigned to a JS variable.
#
# # => "function(obj){...}"
#
def self.call(input)
data = input[:data]
key = ['EjsTemplate', VERSION, data]
input[:cache].fetch(key) do
::EJS.compile(data)
end
end
end
# Deprecated
EjsTemplate = EjsProcessor
end
12 changes: 6 additions & 6 deletions lib/sprockets/engines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module Sprockets
#
# An engine is a type of processor that is bound to a filename
# extension. `application.js.coffee` indicates that the
# `CoffeeScriptTemplate` engine will be ran on the file.
# `CoffeeScriptProcessor` 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`.
# left. `application.js.coffee.erb` will first run `ERBProcessor`
# then `CoffeeScriptProcessor`.
#
# All `Engine`s must follow the `Template` interface. It is
# recommended to subclass `Template`.
Expand All @@ -23,15 +23,15 @@ module Sprockets
#
# The global registry is exposed for plugins to register themselves.
#
# Sprockets.register_engine '.sass', SassTemplate
# Sprockets.register_engine '.sass', SassProcessor
#
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, ...}
# # => {".coffee" => CoffeeScriptProcessor, ".sass" => SassProcessor, ...}
#
attr_reader :engines

Expand All @@ -56,7 +56,7 @@ def unwrap_engines(extnames)
# Registers a new Engine `klass` for `ext`. If the `ext` already
# has an engine registered, it will be overridden.
#
# environment.register_engine '.coffee', CoffeeScriptTemplate
# environment.register_engine '.coffee', CoffeeScriptProcessor
#
def register_engine(ext, klass, options = {})
ext = Sprockets::Utils.normalize_extension(ext)
Expand Down
23 changes: 23 additions & 0 deletions lib/sprockets/erb_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'erb'

module Sprockets
class ERBProcessor
def self.call(input)
new.call(input)
end

def initialize(&block)
@block = block
end

def call(input)
engine = ::ERB.new(input[:data], nil, '<>')
context = input[:environment].context_class.new(input)
klass = (class << context; self; end)
klass.class_eval(&@block) if @block
engine.def_method(klass, :_evaluate_template, input[:filename])
data = context._evaluate_template
context.metadata.merge(data: data)
end
end
end
23 changes: 3 additions & 20 deletions lib/sprockets/erb_template.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
require 'erb'
require 'sprockets/erb_processor'

module Sprockets
class ERBTemplate
def self.call(input)
new.call(input)
end

def initialize(&block)
@block = block
end

def call(input)
engine = ::ERB.new(input[:data], nil, '<>')
context = input[:environment].context_class.new(input)
klass = (class << context; self; end)
klass.class_eval(&@block) if @block
engine.def_method(klass, :_evaluate_template, input[:filename])
data = context._evaluate_template
context.metadata.merge(data: data)
end
end
# Deprecated
ERBTemplate = ERBProcessor
end
2 changes: 1 addition & 1 deletion lib/sprockets/lazy_processor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Sprockets
# Internal: Used for lazy loading processors.
#
# LazyProcessor.new { CoffeeScriptTemplate }
# LazyProcessor.new { CoffeeScriptProcessor }
#
class LazyProcessor
def initialize(&block)
Expand Down
2 changes: 1 addition & 1 deletion lib/sprockets/legacy_tilt_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Sprockets
#
# Will be removed in Sprockets 4.x.
#
# LegacyTiltProcessor.new(Tilt::CoffeeScriptTemplate)
# LegacyTiltProcessor.new(Tilt::CoffeeScriptProcessor)
#
class LegacyTiltProcessor < Delegator
def initialize(klass)
Expand Down
4 changes: 2 additions & 2 deletions lib/sprockets/sass_cache_store.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Deprecated: Require sprockets/sass_template instead
require 'sprockets/sass_template'
# Deprecated: Require sprockets/sass_processor instead
require 'sprockets/sass_processor'
4 changes: 2 additions & 2 deletions lib/sprockets/sass_functions.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Deprecated: Require sprockets/sass_template instead
require 'sprockets/sass_template'
# Deprecated: Require sprockets/sass_processor instead
require 'sprockets/sass_processor'
4 changes: 2 additions & 2 deletions lib/sprockets/sass_importer.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Deprecated: Require sprockets/sass_template instead
require 'sprockets/sass_template'
# Deprecated: Require sprockets/sass_processor instead
require 'sprockets/sass_processor'
Loading

0 comments on commit 2c9333b

Please sign in to comment.