2014-05-02 17:46:03 -04:00
|
|
|
require_dependency 'sass/discourse_sass_importer'
|
2014-10-15 04:23:29 -04:00
|
|
|
require 'pathname'
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2015-08-20 16:10:54 -04:00
|
|
|
module Sass::Script::Functions
|
|
|
|
def _error(message)
|
|
|
|
raise Sass::SyntaxError, mesage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-02 17:46:03 -04:00
|
|
|
class DiscourseSassCompiler
|
|
|
|
|
|
|
|
def self.compile(scss, target, opts={})
|
|
|
|
self.new(scss, target).compile(opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Takes a Sass::SyntaxError and generates css that will show the
|
|
|
|
# error at the bottom of the page.
|
|
|
|
def self.error_as_css(sass_error, label)
|
|
|
|
error = sass_error.sass_backtrace_str(label)
|
|
|
|
error.gsub!("\n", '\A ')
|
|
|
|
error.gsub!("'", '\27 ')
|
|
|
|
|
|
|
|
"footer { white-space: pre; }
|
|
|
|
footer:after { content: '#{error}' }"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(scss, target)
|
|
|
|
@scss = scss
|
|
|
|
@target = target
|
2014-06-17 12:45:33 -04:00
|
|
|
|
|
|
|
unless Sass::Script::Functions < Sprockets::SassFunctions
|
|
|
|
Sass::Script::Functions.send :include, Sprockets::SassFunctions
|
|
|
|
end
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Compiles the given scss and output the css as a string.
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
# safe: (boolean) if true, theme and plugin stylesheets will not be included. Default is false.
|
|
|
|
def compile(opts={})
|
2016-04-17 22:47:52 -04:00
|
|
|
app = Rails.application
|
|
|
|
env = app.assets || Sprockets::Railtie.build_environment(app)
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-10-15 04:23:29 -04:00
|
|
|
pathname = Pathname.new("app/assets/stylesheets/#{@target}.scss")
|
2016-04-17 22:47:52 -04:00
|
|
|
|
|
|
|
context = env.context_class.new(
|
|
|
|
environment: env,
|
|
|
|
filename: "#{@target}.scss",
|
|
|
|
pathname: pathname,
|
|
|
|
metadata: {}
|
|
|
|
)
|
2014-05-02 17:46:03 -04:00
|
|
|
|
2014-05-20 14:23:41 -04:00
|
|
|
debug_opts = Rails.env.production? ? {} : {
|
|
|
|
line_numbers: true,
|
|
|
|
# debug_info: true, # great with Firebug + FireSass, but not helpful elsewhere
|
|
|
|
style: :expanded
|
|
|
|
}
|
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
importer_class = opts[:safe] ? DiscourseSafeSassImporter : DiscourseSassImporter
|
|
|
|
|
2014-08-08 02:31:31 -04:00
|
|
|
css = ::Sass::Engine.new(@scss, {
|
2014-05-02 17:46:03 -04:00
|
|
|
syntax: :scss,
|
|
|
|
cache: false,
|
|
|
|
read_cache: false,
|
2014-05-20 14:23:41 -04:00
|
|
|
style: :compressed,
|
2016-04-17 22:47:52 -04:00
|
|
|
filesystem_importer: importer_class,
|
|
|
|
load_paths: context.environment.paths.map { |path| importer_class.new(path.to_s) },
|
2014-05-02 17:46:03 -04:00
|
|
|
sprockets: {
|
|
|
|
context: context,
|
|
|
|
environment: context.environment
|
|
|
|
}
|
2014-05-20 14:23:41 -04:00
|
|
|
}.merge(debug_opts)).render
|
2014-08-08 02:31:31 -04:00
|
|
|
|
2014-08-08 09:06:29 -04:00
|
|
|
css_output = css
|
2015-05-20 01:56:54 -04:00
|
|
|
if opts[:rtl]
|
2015-04-14 23:14:04 -04:00
|
|
|
begin
|
|
|
|
require 'r2'
|
|
|
|
css_output = R2.r2(css) if defined?(R2)
|
|
|
|
rescue; end
|
2014-08-08 02:31:31 -04:00
|
|
|
end
|
2014-08-08 09:06:29 -04:00
|
|
|
css_output
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|