2014-04-01 07:42:14 -04:00
|
|
|
# This custom importer is used for site customizations. This is similar to the
|
|
|
|
# Sprockets::SassImporter implementation provided in sass-rails since that is used
|
|
|
|
# during asset precompilation.
|
|
|
|
class DiscourseSassImporter < Sass::Importers::Filesystem
|
2016-04-17 22:47:52 -04:00
|
|
|
module Sass
|
|
|
|
def extensions
|
|
|
|
{
|
|
|
|
'css' => :scss,
|
|
|
|
'css.scss' => :scss,
|
|
|
|
'css.sass' => :sass,
|
|
|
|
'css.erb' => :scss,
|
|
|
|
'scss.erb' => :scss,
|
|
|
|
'sass.erb' => :sass,
|
|
|
|
'css.scss.erb' => :scss,
|
|
|
|
'css.sass.erb' => :sass
|
|
|
|
}.merge!(super)
|
|
|
|
end
|
2014-04-01 07:42:14 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def special_imports
|
|
|
|
{
|
|
|
|
"plugins" => DiscoursePluginRegistry.stylesheets,
|
|
|
|
"plugins_mobile" => DiscoursePluginRegistry.mobile_stylesheets,
|
|
|
|
"plugins_desktop" => DiscoursePluginRegistry.desktop_stylesheets,
|
|
|
|
"plugins_variables" => DiscoursePluginRegistry.sass_variables,
|
|
|
|
"theme_variables" => [ColorScheme::BASE_COLORS_FILE],
|
2016-12-02 02:15:34 -05:00
|
|
|
"category_backgrounds" => Proc.new { |c| "body.category-#{c.full_slug} { background-image: url(#{apply_cdn(c.uploaded_background.url)}) }\n" }
|
2016-04-17 22:47:52 -04:00
|
|
|
}
|
|
|
|
end
|
2014-04-25 04:26:37 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def find_relative(name, base, options)
|
2014-04-01 07:42:14 -04:00
|
|
|
engine_from_path(name, File.dirname(base), options)
|
|
|
|
end
|
2014-06-27 17:06:59 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def apply_cdn(url)
|
|
|
|
"#{GlobalSetting.cdn_url}#{url}"
|
2014-06-27 17:06:59 -04:00
|
|
|
end
|
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def find(name, options)
|
|
|
|
|
|
|
|
if special_imports.has_key? name
|
2016-04-19 02:58:03 -04:00
|
|
|
case name
|
|
|
|
when "theme_variables"
|
2016-04-17 22:47:52 -04:00
|
|
|
contents = ""
|
2017-01-12 15:43:09 -05:00
|
|
|
ColorScheme.base_colors.each do |n, base_hex|
|
|
|
|
hex_val = ColorScheme.hex_for_name(n) || base_hex
|
|
|
|
contents << "$#{n}: ##{hex_val} !default;\n"
|
2014-05-02 17:46:03 -04:00
|
|
|
end
|
2016-04-19 02:58:03 -04:00
|
|
|
when "category_backgrounds"
|
|
|
|
contents = ""
|
2016-12-02 02:15:34 -05:00
|
|
|
Category.where('uploaded_background_id IS NOT NULL').each do |c|
|
|
|
|
contents << special_imports[name].call(c) if c.uploaded_background
|
2016-04-19 02:58:03 -04:00
|
|
|
end
|
2016-04-17 22:47:52 -04:00
|
|
|
else
|
|
|
|
stylesheets = special_imports[name]
|
|
|
|
contents = ""
|
|
|
|
stylesheets.each do |css_file|
|
|
|
|
if css_file =~ /\.scss$/
|
|
|
|
contents << "@import '#{css_file}';"
|
|
|
|
else
|
|
|
|
contents << File.read(css_file)
|
|
|
|
end
|
|
|
|
depend_on(css_file)
|
|
|
|
end
|
|
|
|
end
|
2016-04-19 02:58:03 -04:00
|
|
|
|
|
|
|
::Sass::Engine.new(contents, options.merge(
|
|
|
|
filename: "#{name}.scss",
|
|
|
|
importer: self,
|
|
|
|
syntax: :scss
|
|
|
|
))
|
2016-04-17 22:47:52 -04:00
|
|
|
else
|
|
|
|
engine_from_path(name, root, options)
|
2014-04-01 07:42:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
private
|
2014-04-01 07:42:14 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def depend_on(filename)
|
|
|
|
if @context
|
|
|
|
@context.depend_on(filename)
|
|
|
|
@context.depend_on(globbed_file_parent(filename))
|
|
|
|
end
|
2014-04-09 09:47:19 -04:00
|
|
|
end
|
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def engine_from_path(name, dir, options)
|
|
|
|
full_filename, _ = ::Sass::Util.destructure(find_real_file(dir, name, options))
|
|
|
|
return unless full_filename && File.readable?(full_filename)
|
|
|
|
|
|
|
|
depend_on(full_filename)
|
|
|
|
::Sass::Engine.for_file(full_filename, options)
|
2014-04-09 09:47:19 -04:00
|
|
|
end
|
2016-04-17 22:47:52 -04:00
|
|
|
end
|
2014-04-09 09:47:19 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
include Sass
|
|
|
|
include ::Sass::Rails::SassImporter::Globbing
|
2014-04-01 07:42:14 -04:00
|
|
|
|
2016-04-17 22:47:52 -04:00
|
|
|
def self.special_imports
|
|
|
|
self.new('').special_imports
|
|
|
|
end
|
2014-04-01 07:42:14 -04:00
|
|
|
end
|