Skip to content

Commit

Permalink
Support all versions of sprockets. Remove tilt template and dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
yivo committed Sep 19, 2016
1 parent 85fe02b commit d634870
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
27 changes: 22 additions & 5 deletions lib/jade-rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ class Railtie < Rails::Engine
config.jade.pretty = Rails.env.development?
config.jade.compile_debug = Rails.env.development?

config.before_initialize do |app|
register_template = -> (env) { (env || app.assets).register_engine('.jade', Jade::Template) }
if app.config.assets.respond_to?(:configure)
app.config.assets.configure { |env| register_template.call(env) }
def configure_assets(app)
if config.respond_to?(:assets) && config.assets.respond_to?(:configure)
# Rails 4.x
config.assets.configure { |env| yield(env) }
else
register_template.call(nil)
# Rails 3.2
yield(app.assets)
end
end

initializer 'sprockets.jade', group: :all, after: 'sprockets.environment' do |app|
configure_assets(app) do |env|
# Sprockets 2, 3, and 4
if env.respond_to?(:register_transformer)
env.register_mime_type 'text/x-jade', extensions: ['.jade']
env.register_transformer 'text/x-jade', 'application/javascript', Jade::SprocketsTransformer
end

if env.respond_to?(:register_engine)
args = ['.jade', Jade::SprocketsTransformer]
args << { mime_type: 'text/x-jade', silence_deprecation: true } if Sprockets::VERSION.start_with?('3')
env.register_engine(*args)
end
end
end
end
Expand Down
26 changes: 21 additions & 5 deletions lib/jade-rails/template.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# frozen_string_literal: true
require 'tilt'
module Jade
class Template < Tilt::Template
def prepare
# Sprockets 2, 3 & 4 interface
# https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#registering-all-versions-of-sprockets-in-processors
class SprocketsTransformer
def initialize(filename, &block)
@filename = filename
@source = block.call
end

def evaluate(context, locals, &block)
Jade.compile(data, filename: file, client: true)
def render(context, empty_hash_wtf)
self.class.run(@filename, @source, context)
end

def self.run(filename, source, context)
Jade.compile(source, filename: filename, client: true)
end

def self.call(input)
filename = input[:filename]
source = input[:data]
context = input[:environment].context_class.new(input)

result = run(filename, source, context)
context.metadata.merge(data: result)
end
end
end
27 changes: 22 additions & 5 deletions lib/pug-rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@ class Railtie < Rails::Engine
config.pug.pretty = Rails.env.development?
config.pug.compile_debug = Rails.env.development?

config.before_initialize do |app|
register_template = -> (env) { (env || app.assets).register_engine('.pug', Pug::Template) }
if app.config.assets.respond_to?(:configure)
app.config.assets.configure { |env| register_template.call(env) }
def configure_assets(app)
if config.respond_to?(:assets) && config.assets.respond_to?(:configure)
# Rails 4.x
config.assets.configure { |env| yield(env) }
else
register_template.call(nil)
# Rails 3.2
yield(app.assets)
end
end

initializer 'sprockets.pug', group: :all, after: 'sprockets.environment' do |app|
configure_assets(app) do |env|
# Sprockets 2, 3, and 4
if env.respond_to?(:register_transformer)
env.register_mime_type 'text/x-pug', extensions: ['.pug']
env.register_transformer 'text/x-pug', 'application/javascript+function', Pug::SprocketsTransformer
end

if env.respond_to?(:register_engine)
args = ['.pug', Pug::SprocketsTransformer]
args << { mime_type: 'text/x-pug', silence_deprecation: true } if Sprockets::VERSION.start_with?('3')
env.register_engine(*args)
end
end
end
end
Expand Down
26 changes: 21 additions & 5 deletions lib/pug-rails/template.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
# frozen_string_literal: true
require 'tilt'
module Pug
class Template < Tilt::Template
def prepare
# Sprockets 2, 3 & 4 interface
# https://github.com/rails/sprockets/blob/master/guides/extending_sprockets.md#registering-all-versions-of-sprockets-in-processors
class SprocketsTransformer
def initialize(filename, &block)
@filename = filename
@source = block.call
end

def evaluate(context, locals, &block)
Pug.compile(data, filename: file, client: true)
def render(context, empty_hash_wtf)
self.class.run(@filename, @source, context)
end

def self.run(filename, source, context)
Pug.compile(source, filename: filename, client: true)
end

def self.call(input)
filename = input[:filename]
source = input[:data]
context = input[:environment].context_class.new(input)

result = run(filename, source, context)
context.metadata.merge(data: result)
end
end
end
3 changes: 1 addition & 2 deletions pug-rails.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
Gem::Specification.new do |s|
s.name = 'pug-rails'
s.version = '2.0.1'
s.version = '2.0.2'
s.author = 'Yaroslav Konoplov'
s.email = 'eahome00@gmail.com'
s.summary = 'Pug/Jade template engine integration with Rails asset pipeline.'
Expand All @@ -15,5 +15,4 @@ Gem::Specification.new do |s|
s.require_paths = ['lib']

s.add_dependency 'pug-ruby', '~> 1.0'
s.add_dependency 'tilt', '~> 2.0'
end

0 comments on commit d634870

Please sign in to comment.