Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/rtlize/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ class Railtie < ::Rails::Railtie
config.rtlize.rtl_locales = Rtlize.rtl_locales

initializer "rtlize.railtie", :after => "sprockets.environment" do |app|
app.assets.register_postprocessor 'text/css', Rtlize::RtlProcessor
# Support Sprockets 4
if app.assets.respond_to?(:register_transformer)
app.assets.register_mime_type 'text/css', extensions: ['.css'], charset: :css
app.assets.register_postprocessor 'text/css', 'text/css', Rtlize::RtlProcessor
end

# Support Sprockets 2, 3
if app.assets.respond_to?(:register_engine)
args = ['.css', Rtlize::RtlProcessor]
args << { mime_type: 'text/css', silence_deprecation: true } if Sprockets::VERSION.start_with?("3")
app.assets.register_engine(*args)
end

Rtlize.rtl_selector = config.rtlize.rtl_selector
Rtlize.rtl_locales = config.rtlize.rtl_locales
Expand Down
24 changes: 18 additions & 6 deletions lib/rtlize/rtl_processor.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
module Rtlize
class RtlProcessor
attr_reader :data

def initialize(file, &block)
@data = block.call
def initialize(filename, &block)
@filename = filename
@source = block.call
end

def render(context, locals, &block)
self.class.run(@filename, @source, context)
end

def self.run(filename, source, context)
if context.pathname.basename.to_s.match(/\.rtl\.s?css/i)
Rtlize::RTLizer.transform(data)
Rtlize::RTLizer.transform(source)
else
data
source
end
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