2017-04-12 22:52:52 +08:00
|
|
|
require_dependency 'stylesheet/common'
|
2017-05-08 23:38:48 +08:00
|
|
|
require_dependency 'global_path'
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
class Importer < SassC::Importer
|
2017-05-08 23:38:48 +08:00
|
|
|
include GlobalPath
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def self.special_imports
|
2018-01-20 05:24:19 +08:00
|
|
|
@special_imports ||= {}
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_import(name, &blk)
|
2018-01-20 05:24:19 +08:00
|
|
|
special_imports[name] = blk
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2017-04-20 04:46:28 +08:00
|
|
|
register_import "theme_field" do
|
2019-04-12 18:36:08 +08:00
|
|
|
Import.new("#{theme_dir}/theme_field.scss", source: @theme_field)
|
2017-04-20 04:46:28 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
register_import "plugins" do
|
|
|
|
import_files(DiscoursePluginRegistry.stylesheets)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "plugins_mobile" do
|
|
|
|
import_files(DiscoursePluginRegistry.mobile_stylesheets)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "plugins_desktop" do
|
|
|
|
import_files(DiscoursePluginRegistry.desktop_stylesheets)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "plugins_variables" do
|
|
|
|
import_files(DiscoursePluginRegistry.sass_variables)
|
|
|
|
end
|
|
|
|
|
2019-02-19 23:55:59 +08:00
|
|
|
register_import "theme_colors" do
|
2017-04-12 22:52:52 +08:00
|
|
|
contents = ""
|
|
|
|
colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
|
|
|
|
colors.each do |n, hex|
|
|
|
|
contents << "$#{n}: ##{hex} !default;\n"
|
|
|
|
end
|
2019-02-19 23:55:59 +08:00
|
|
|
contents
|
|
|
|
|
|
|
|
Import.new("theme_colors.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "theme_variables" do
|
|
|
|
contents = ""
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2017-11-14 12:22:59 +08:00
|
|
|
theme&.all_theme_variables&.each do |field|
|
2017-05-08 23:38:48 +08:00
|
|
|
if field.type_id == ThemeField.types[:theme_upload_var]
|
|
|
|
if upload = field.upload
|
|
|
|
url = upload_cdn_path(upload.url)
|
|
|
|
contents << "$#{field.name}: unquote(\"#{url}\");\n"
|
|
|
|
end
|
|
|
|
else
|
2018-03-05 08:04:23 +08:00
|
|
|
contents << to_scss_variable(field.name, field.value)
|
2017-05-08 23:38:48 +08:00
|
|
|
end
|
2017-05-03 04:01:01 +08:00
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
|
|
|
|
theme&.included_settings&.each do |name, value|
|
|
|
|
contents << to_scss_variable(name, value)
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
Import.new("theme_variable.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "category_backgrounds" do
|
|
|
|
contents = ""
|
|
|
|
Category.where('uploaded_background_id IS NOT NULL').each do |c|
|
2017-09-12 05:10:18 +08:00
|
|
|
contents << category_css(c) if c.uploaded_background&.url.present?
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2018-04-23 08:55:38 +08:00
|
|
|
Import.new("category_background.scss", source: contents)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
register_import "embedded_theme" do
|
|
|
|
next unless @theme_id
|
|
|
|
|
|
|
|
theme_import(:common, :embedded_scss)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "mobile_theme" do
|
|
|
|
next unless @theme_id
|
|
|
|
|
|
|
|
theme_import(:mobile, :scss)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "desktop_theme" do
|
|
|
|
next unless @theme_id
|
|
|
|
|
|
|
|
theme_import(:desktop, :scss)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options)
|
2017-05-10 05:20:28 +08:00
|
|
|
@theme = options[:theme]
|
2017-04-12 22:52:52 +08:00
|
|
|
@theme_id = options[:theme_id]
|
2017-04-20 04:46:28 +08:00
|
|
|
@theme_field = options[:theme_field]
|
2017-05-10 05:20:28 +08:00
|
|
|
if @theme && !@theme_id
|
|
|
|
# make up an id so other stuff does not bail out
|
|
|
|
@theme_id = @theme.id || -1
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_files(files)
|
|
|
|
files.map do |file|
|
|
|
|
# we never want inline css imports, they are a mess
|
|
|
|
# this tricks libsass so it imports inline instead
|
|
|
|
if file =~ /\.css$/
|
|
|
|
file = file[0..-5]
|
|
|
|
end
|
|
|
|
Import.new(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def theme_import(target, attr)
|
|
|
|
fields = theme.list_baked_fields(target, attr)
|
|
|
|
|
|
|
|
fields.map do |field|
|
|
|
|
value = field.value
|
|
|
|
if value.present?
|
2019-04-12 18:36:08 +08:00
|
|
|
filename = "theme_#{field.theme.id}/#{field.target_name}-#{field.name}-#{field.theme.name.parameterize}.scss"
|
|
|
|
with_comment = <<~COMMENT
|
|
|
|
// Theme: #{field.theme.name}
|
|
|
|
// Target: #{field.target_name} #{field.name}
|
|
|
|
// Last Edited: #{field.updated_at}
|
|
|
|
|
|
|
|
#{value}
|
|
|
|
COMMENT
|
2017-04-12 22:52:52 +08:00
|
|
|
Import.new(filename, source: with_comment)
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def theme
|
2017-05-03 04:01:01 +08:00
|
|
|
unless @theme
|
|
|
|
@theme = (@theme_id && Theme.find(@theme_id)) || :nil
|
|
|
|
end
|
|
|
|
@theme == :nil ? nil : @theme
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-04-12 18:36:08 +08:00
|
|
|
def theme_dir
|
|
|
|
"theme_#{theme.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def importable_theme_fields
|
|
|
|
return {} unless theme
|
|
|
|
@importable_theme_fields ||= begin
|
|
|
|
hash = {}
|
|
|
|
@theme.theme_fields.where(target_id: Theme.targets[:extra_scss]).each do |field|
|
|
|
|
hash[field.name] = field.value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_theme_import(path, parent_path)
|
|
|
|
# Only allow importing theme stylesheets from within other theme stylesheets
|
|
|
|
return false unless theme && parent_path.start_with?("#{theme_dir}/")
|
|
|
|
parent_dir, _ = File.split(parent_path)
|
|
|
|
|
|
|
|
# Could be relative to the importing file, or relative to the root of the theme directory
|
|
|
|
search_paths = [parent_dir, theme_dir].uniq
|
|
|
|
search_paths.each do |search_path|
|
|
|
|
resolved = Pathname.new("#{search_path}/#{path}").cleanpath.to_s # Remove unnecessary ./ and ../
|
|
|
|
next unless resolved.start_with?("#{theme_dir}/")
|
|
|
|
resolved.sub!("#{theme_dir}/", "")
|
|
|
|
if importable_theme_fields.keys.include?(resolved)
|
|
|
|
return resolved
|
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def category_css(category)
|
2017-05-23 00:37:01 +08:00
|
|
|
"body.category-#{category.full_slug} { background-image: url(#{upload_cdn_path(category.uploaded_background.url)}) }\n"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def to_scss_variable(name, value)
|
2019-03-08 16:58:06 +08:00
|
|
|
escaped = SassC::Script::Value::String.quote(value, sass: true)
|
|
|
|
"$#{name}: unquote(#{escaped});\n"
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def imports(asset, parent_path)
|
|
|
|
if asset[-1] == "*"
|
|
|
|
Dir["#{Stylesheet::ASSET_ROOT}/#{asset}.scss"].map do |path|
|
|
|
|
Import.new(asset[0..-2] + File.basename(path, ".*"))
|
|
|
|
end
|
|
|
|
elsif callback = Importer.special_imports[asset]
|
|
|
|
instance_eval(&callback)
|
2019-04-12 18:36:08 +08:00
|
|
|
elsif resolved = match_theme_import(asset, parent_path)
|
|
|
|
Import.new("#{theme_dir}/#{resolved}", source: importable_theme_fields[resolved])
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
|
|
|
Import.new(asset + ".scss")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|