2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
require_dependency 'stylesheet/common'
|
2017-05-08 10:38:48 -05:00
|
|
|
require_dependency 'global_path'
|
2017-04-12 09:52:52 -05:00
|
|
|
|
|
|
|
module Stylesheet
|
|
|
|
class Importer < SassC::Importer
|
2017-05-08 10:38:48 -05:00
|
|
|
include GlobalPath
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2019-10-01 19:54:31 -05:00
|
|
|
THEME_TARGETS ||= %w{embedded_theme mobile_theme desktop_theme}
|
2019-09-16 11:06:34 -05:00
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def self.special_imports
|
2018-01-19 15:24:19 -06:00
|
|
|
@special_imports ||= {}
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_import(name, &blk)
|
2018-01-19 15:24:19 -06:00
|
|
|
special_imports[name] = blk
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
# Contained in function so that it can be called repeatedly from test mode
|
|
|
|
def self.register_imports!
|
|
|
|
@special_imports = {}
|
|
|
|
|
|
|
|
Discourse.plugins.each do |plugin|
|
|
|
|
plugin_directory_name = plugin.directory_name
|
2019-08-20 11:39:52 -05:00
|
|
|
|
2019-09-16 11:06:34 -05: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-20 11:39:52 -05:00
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
if stylesheets[plugin_directory_name].present?
|
|
|
|
register_import asset_name do
|
|
|
|
import_files(stylesheets[plugin_directory_name])
|
|
|
|
end
|
2019-08-20 11:39:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2020-09-04 08:25:50 -05:00
|
|
|
register_import "font" do
|
2020-10-05 12:40:41 -05:00
|
|
|
body_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.base_font }
|
|
|
|
heading_font = DiscourseFonts.fonts.find { |f| f[:key] == SiteSetting.heading_font }
|
|
|
|
contents = +""
|
2020-09-04 08:25:50 -05:00
|
|
|
|
2020-10-05 12:40:41 -05:00
|
|
|
if body_font.present?
|
|
|
|
contents << <<~EOF
|
|
|
|
#{font_css(body_font)}
|
2020-09-23 04:00:07 -05:00
|
|
|
|
|
|
|
:root {
|
2020-10-05 12:40:41 -05:00
|
|
|
--font-family: #{body_font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
if heading_font.present?
|
|
|
|
contents << <<~EOF
|
|
|
|
#{font_css(heading_font)}
|
|
|
|
|
|
|
|
:root {
|
|
|
|
--heading-font-family: #{heading_font[:stack]};
|
2020-09-23 04:00:07 -05:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
2020-09-04 08:25:50 -05:00
|
|
|
|
|
|
|
Import.new("font.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
register_import "wizard_fonts" do
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
DiscourseFonts.fonts.each do |font|
|
2020-09-25 08:39:14 -05: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 08:25:50 -05:00
|
|
|
contents << font_css(font)
|
|
|
|
contents << <<~EOF
|
2020-10-05 12:40:41 -05:00
|
|
|
.body-font-#{font[:key].tr("_", "-")} {
|
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
.heading-font-#{font[:key].tr("_", "-")} h2 {
|
2020-09-04 08:25:50 -05:00
|
|
|
font-family: #{font[:stack]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
Import.new("wizard_fonts.scss", source: contents)
|
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
register_import "plugins_variables" do
|
|
|
|
import_files(DiscoursePluginRegistry.sass_variables)
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2019-02-19 09:55:59 -06:00
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
register_import "theme_colors" do
|
|
|
|
contents = +""
|
2020-08-03 21:57:10 -05: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-16 11:06:34 -05:00
|
|
|
colors.each do |n, hex|
|
|
|
|
contents << "$#{n}: ##{hex} !default;\n"
|
|
|
|
end
|
2019-02-19 09:55:59 -06:00
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
Import.new("theme_colors.scss", source: contents)
|
|
|
|
end
|
2018-03-04 18:04:23 -06:00
|
|
|
|
2019-09-16 11:06:34 -05: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 09:52:52 -05:00
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
Import.new("category_background.scss", source: contents)
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-09-16 11:06:34 -05:00
|
|
|
register_imports!
|
|
|
|
|
2020-08-18 12:02:13 -05:00
|
|
|
def self.import_color_definitions(theme_id)
|
2020-08-06 08:46:17 -05: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-18 12:02:13 -05:00
|
|
|
|
2020-09-09 10:43:34 -05:00
|
|
|
theme_id ||= SiteSetting.default_theme_id
|
|
|
|
resolved_ids = Theme.transform_ids([theme_id])
|
|
|
|
|
|
|
|
if resolved_ids
|
2021-02-02 12:09:41 -06:00
|
|
|
theme = Theme.find_by_id(theme_id)
|
|
|
|
contents << theme&.scss_variables.to_s
|
2020-09-09 10:43:34 -05:00
|
|
|
Theme.list_baked_fields(resolved_ids, :common, :color_definitions).each do |row|
|
2021-02-02 12:09:41 -06:00
|
|
|
contents << "// Color definitions from #{theme.name}\n\n"
|
2020-08-18 12:02:13 -05:00
|
|
|
contents << row.value
|
|
|
|
end
|
|
|
|
end
|
2020-08-06 08:46:17 -05:00
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2020-10-15 13:05:48 -05:00
|
|
|
def self.import_wcag_overrides(color_scheme_id)
|
|
|
|
if color_scheme_id && ColorScheme.find_by_id(color_scheme_id)&.is_wcag?
|
|
|
|
return "@import \"wcag\";"
|
|
|
|
end
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def initialize(options)
|
2017-05-09 16:20:28 -05:00
|
|
|
@theme = options[:theme]
|
2017-04-12 09:52:52 -05:00
|
|
|
@theme_id = options[:theme_id]
|
2020-08-03 21:57:10 -05:00
|
|
|
@color_scheme_id = options[:color_scheme_id]
|
|
|
|
|
2017-05-09 16:20:28 -05: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 09:52:52 -05: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
|
|
|
|
|
2021-02-02 12:09:41 -06:00
|
|
|
def theme_import(target)
|
|
|
|
attr = target == :embedded_theme ? :embedded_scss : :scss
|
|
|
|
target = target.to_s.gsub("_theme", "").to_sym
|
2017-04-12 09:52:52 -05:00
|
|
|
|
2021-02-02 12:09:41 -06:00
|
|
|
contents = +""
|
|
|
|
|
|
|
|
fields = theme.list_baked_fields(target, attr)
|
2017-04-12 09:52:52 -05:00
|
|
|
fields.map do |field|
|
|
|
|
value = field.value
|
|
|
|
if value.present?
|
2019-04-12 05:36:08 -05:00
|
|
|
filename = "theme_#{field.theme.id}/#{field.target_name}-#{field.name}-#{field.theme.name.parameterize}.scss"
|
2021-02-02 12:09:41 -06:00
|
|
|
contents << <<~COMMENT
|
2019-04-12 05:36:08 -05:00
|
|
|
// Theme: #{field.theme.name}
|
|
|
|
// Target: #{field.target_name} #{field.name}
|
|
|
|
// Last Edited: #{field.updated_at}
|
|
|
|
COMMENT
|
2021-02-03 10:02:53 -06:00
|
|
|
|
|
|
|
if field.theme_id == theme.id
|
|
|
|
contents << value
|
|
|
|
else
|
|
|
|
css, source_map = begin
|
|
|
|
field.compile_scss
|
|
|
|
rescue SassC::SyntaxError => e
|
|
|
|
raise Discourse::ScssError, e.message
|
|
|
|
end
|
|
|
|
|
|
|
|
contents << css
|
|
|
|
end
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
2021-02-03 10:02:53 -06:00
|
|
|
|
2021-02-02 12:09:41 -06:00
|
|
|
end
|
|
|
|
contents
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def theme
|
2017-05-02 15:01:01 -05:00
|
|
|
unless @theme
|
|
|
|
@theme = (@theme_id && Theme.find(@theme_id)) || :nil
|
|
|
|
end
|
|
|
|
@theme == :nil ? nil : @theme
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def category_css(category)
|
2020-09-23 11:22:07 -05:00
|
|
|
full_slug = category.full_slug.split("-")[0..-2].join("-")
|
2020-11-25 12:40:16 -06:00
|
|
|
"body.category-#{full_slug} { background-image: url(#{upload_cdn_path(category.uploaded_background.url)}) }\n"
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
|
2020-09-04 08:25:50 -05:00
|
|
|
def font_css(font)
|
|
|
|
contents = +""
|
|
|
|
|
|
|
|
if font[:variants].present?
|
|
|
|
font[:variants].each do |variant|
|
2020-09-25 08:39:14 -05:00
|
|
|
src = variant[:src] ? variant[:src] : "asset-url(\"/fonts/#{variant[:filename]}?v=#{DiscourseFonts::VERSION}\") format(\"#{variant[:format]}\")"
|
2020-09-04 08:25:50 -05:00
|
|
|
contents << <<~EOF
|
|
|
|
@font-face {
|
|
|
|
font-family: #{font[:name]};
|
2020-09-25 08:39:14 -05:00
|
|
|
src: #{src};
|
2020-09-04 08:25:50 -05:00
|
|
|
font-weight: #{variant[:weight]};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
contents
|
|
|
|
end
|
|
|
|
|
2017-04-12 09:52:52 -05:00
|
|
|
def imports(asset, parent_path)
|
2020-12-30 14:29:10 -06:00
|
|
|
if callback = Importer.special_imports[asset]
|
2017-04-12 09:52:52 -05:00
|
|
|
instance_eval(&callback)
|
|
|
|
else
|
2021-02-02 12:09:41 -06:00
|
|
|
Import.new(asset + ".scss")
|
2017-04-12 09:52:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|