2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
2019-10-02 08:54:31 +08:00
|
|
|
THEME_TARGETS ||= %w{embedded_theme mobile_theme desktop_theme}
|
2019-09-17 00:06:34 +08:00
|
|
|
|
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
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
# Contained in function so that it can be called repeatedly from test mode
|
|
|
|
def self.register_imports!
|
|
|
|
@special_imports = {}
|
|
|
|
|
|
|
|
register_import "theme_field" do
|
|
|
|
Import.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field)
|
|
|
|
end
|
2017-04-20 04:46:28 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
plugin_directory_name = plugin.directory_name
|
2019-08-21 00:39:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
["", "mobile", "desktop"].each do |type|
|
|
|
|
asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name
|
|
|
|
stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets
|
2019-08-21 00:39:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
if stylesheets[plugin_directory_name].present?
|
|
|
|
register_import asset_name do
|
|
|
|
import_files(stylesheets[plugin_directory_name])
|
|
|
|
end
|
2019-08-21 00:39:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
register_import "font" do
|
|
|
|
font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.base_font }
|
|
|
|
|
2020-09-23 17:00:07 +08:00
|
|
|
contents = if font.present?
|
|
|
|
<<~EOF
|
|
|
|
#{font_css(font)}
|
|
|
|
|
|
|
|
:root {
|
|
|
|
--font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2020-09-04 21:25:50 +08:00
|
|
|
|
|
|
|
Import.new("font.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "wizard_fonts" do
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
DiscourseFonts.fonts.each do |font|
|
2020-09-25 21:39:14 +08:00
|
|
|
if font[:key] == "system"
|
|
|
|
# Overwrite font definition because the preview canvases in the wizard require explicit @font-face definitions.
|
|
|
|
# uses same technique as https://github.com/jonathantneal/system-font-css
|
|
|
|
font[:variants] = [
|
|
|
|
{ src: 'local(".SFNS-Regular"), local(".SFNSText-Regular"), local(".HelveticaNeueDeskInterface-Regular"), local(".LucidaGrandeUI"), local("Segoe UI"), local("Ubuntu"), local("Roboto-Regular"), local("DroidSans"), local("Tahoma")', weight: 400 },
|
|
|
|
{ src: 'local(".SFNS-Bold"), local(".SFNSText-Bold"), local(".HelveticaNeueDeskInterface-Bold"), local(".LucidaGrandeUI"), local("Segoe UI Bold"), local("Ubuntu Bold"), local("Roboto-Bold"), local("DroidSans-Bold"), local("Tahoma Bold")', weight: 700 }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
contents << font_css(font)
|
|
|
|
contents << <<~EOF
|
|
|
|
.font-#{font[:key].tr("_", "-")} {
|
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
Import.new("wizard_fonts.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "plugins_variables" do
|
|
|
|
import_files(DiscoursePluginRegistry.sass_variables)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2019-02-19 23:55:59 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "theme_colors" do
|
|
|
|
contents = +""
|
2020-08-04 10:57:10 +08:00
|
|
|
if @color_scheme_id
|
|
|
|
colors = begin
|
|
|
|
ColorScheme.find(@color_scheme_id).resolved_colors
|
|
|
|
rescue
|
|
|
|
ColorScheme.base_colors
|
|
|
|
end
|
|
|
|
else
|
|
|
|
colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors
|
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
colors.each do |n, hex|
|
|
|
|
contents << "$#{n}: ##{hex} !default;\n"
|
|
|
|
end
|
2019-02-19 23:55:59 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
Import.new("theme_colors.scss", source: contents)
|
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "theme_variables" do
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
theme&.all_theme_variables&.each do |field|
|
|
|
|
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
|
|
|
|
contents << to_scss_variable(field.name, field.value)
|
2017-05-08 23:38:48 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
theme&.included_settings&.each do |name, value|
|
|
|
|
next if name == "theme_uploads"
|
|
|
|
contents << to_scss_variable(name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
Import.new("theme_variable.scss", source: contents)
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "category_backgrounds" do
|
|
|
|
contents = +""
|
|
|
|
Category.where('uploaded_background_id IS NOT NULL').each do |c|
|
|
|
|
contents << category_css(c) if c.uploaded_background&.url.present?
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
Import.new("category_background.scss", source: contents)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "embedded_theme" do
|
|
|
|
next unless @theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
theme_import(:common, :embedded_scss)
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "mobile_theme" do
|
|
|
|
next unless @theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
theme_import(:mobile, :scss)
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_import "desktop_theme" do
|
|
|
|
next unless @theme_id
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
theme_import(:desktop, :scss)
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2019-09-17 00:06:34 +08:00
|
|
|
register_imports!
|
|
|
|
|
2020-08-19 01:02:13 +08:00
|
|
|
def self.import_color_definitions(theme_id)
|
2020-08-06 21:46:17 +08:00
|
|
|
contents = +""
|
|
|
|
DiscoursePluginRegistry.color_definition_stylesheets.each do |name, path|
|
|
|
|
contents << "// Color definitions from #{name}\n\n"
|
|
|
|
contents << File.read(path.to_s)
|
|
|
|
contents << "\n\n"
|
|
|
|
end
|
2020-08-19 01:02:13 +08:00
|
|
|
|
2020-09-09 23:43:34 +08:00
|
|
|
theme_id ||= SiteSetting.default_theme_id
|
|
|
|
resolved_ids = Theme.transform_ids([theme_id])
|
|
|
|
|
|
|
|
if resolved_ids
|
|
|
|
contents << " @import \"theme_variables\";"
|
|
|
|
Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |row|
|
2020-08-19 01:02:13 +08:00
|
|
|
contents << "// Color definitions from #{Theme.find_by_id(theme_id)&.name}\n\n"
|
|
|
|
contents << row.value
|
|
|
|
end
|
|
|
|
end
|
2020-08-06 21:46:17 +08:00
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
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]
|
2020-08-04 10:57:10 +08:00
|
|
|
@color_scheme_id = options[:color_scheme_id]
|
|
|
|
|
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
|
2019-07-29 17:28:24 +08:00
|
|
|
@importable_theme_fields = {}
|
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-07-29 17:28:24 +08:00
|
|
|
def theme_dir(import_theme_id)
|
|
|
|
"theme_#{import_theme_id}"
|
2019-04-12 18:36:08 +08:00
|
|
|
end
|
|
|
|
|
2019-07-29 17:28:24 +08:00
|
|
|
def extract_theme_id(path)
|
|
|
|
path[/^theme_([0-9]+)\//, 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def importable_theme_fields(import_theme_id)
|
|
|
|
return {} unless theme && import_theme = Theme.find(import_theme_id)
|
|
|
|
@importable_theme_fields[import_theme_id] ||= begin
|
2019-04-12 18:36:08 +08:00
|
|
|
hash = {}
|
2019-07-29 17:28:24 +08:00
|
|
|
import_theme.theme_fields.where(target_id: Theme.targets[:extra_scss]).each do |field|
|
2019-04-12 18:36:08 +08:00
|
|
|
hash[field.name] = field.value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def match_theme_import(path, parent_path)
|
2019-07-29 17:28:24 +08:00
|
|
|
# Only allow importing theme stylesheets from within stylesheets in the same theme
|
|
|
|
return false unless theme && import_theme_id = extract_theme_id(parent_path) # Could be a child theme
|
2019-04-12 18:36:08 +08:00
|
|
|
parent_dir, _ = File.split(parent_path)
|
|
|
|
|
|
|
|
# Could be relative to the importing file, or relative to the root of the theme directory
|
2019-07-29 17:28:24 +08:00
|
|
|
search_paths = [parent_dir, theme_dir(import_theme_id)].uniq
|
2019-04-12 18:36:08 +08:00
|
|
|
search_paths.each do |search_path|
|
|
|
|
resolved = Pathname.new("#{search_path}/#{path}").cleanpath.to_s # Remove unnecessary ./ and ../
|
2019-07-29 17:28:24 +08:00
|
|
|
next unless resolved.start_with?("#{theme_dir(import_theme_id)}/")
|
|
|
|
resolved_within_theme = resolved.sub(/^theme_[0-9]+\//, "")
|
|
|
|
if importable_theme_fields(import_theme_id).keys.include?(resolved_within_theme)
|
|
|
|
return resolved, importable_theme_fields(import_theme_id)[resolved_within_theme]
|
2019-04-12 18:36:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def category_css(category)
|
2020-09-24 00:22:07 +08:00
|
|
|
full_slug = category.full_slug.split("-")[0..-2].join("-")
|
|
|
|
"body.category-#{category.slug}, body.category-#{full_slug} { background-image: url(#{upload_cdn_path(category.uploaded_background.url)}) }\n"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2020-09-04 21:25:50 +08:00
|
|
|
def font_css(font)
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
if font[:variants].present?
|
|
|
|
font[:variants].each do |variant|
|
2020-09-25 21:39:14 +08:00
|
|
|
src = variant[:src] ? variant[:src] : "asset-url(\"/fonts/#{variant[:filename]}?v=#{DiscourseFonts::VERSION}\") format(\"#{variant[:format]}\")"
|
2020-09-04 21:25:50 +08:00
|
|
|
contents << <<~EOF
|
|
|
|
@font-face {
|
|
|
|
font-family: #{font[:name]};
|
2020-09-25 21:39:14 +08:00
|
|
|
src: #{src};
|
2020-09-04 21:25:50 +08:00
|
|
|
font-weight: #{variant[:weight]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contents
|
|
|
|
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] == "*"
|
2019-10-02 12:01:53 +08:00
|
|
|
Dir["#{Stylesheet::Common::ASSET_ROOT}/#{asset}.scss"].map do |path|
|
2017-04-12 22:52:52 +08:00
|
|
|
Import.new(asset[0..-2] + File.basename(path, ".*"))
|
|
|
|
end
|
|
|
|
elsif callback = Importer.special_imports[asset]
|
|
|
|
instance_eval(&callback)
|
|
|
|
else
|
2020-02-16 17:37:54 +08:00
|
|
|
path, source = match_theme_import(asset, parent_path)
|
|
|
|
if path && source
|
|
|
|
Import.new(path, source: source)
|
|
|
|
else
|
|
|
|
Import.new(asset + ".scss")
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|