2018-03-05 08:04:23 +08:00
|
|
|
require_dependency 'theme_settings_parser'
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
class ThemeField < ActiveRecord::Base
|
|
|
|
|
2017-05-08 23:38:48 +08:00
|
|
|
belongs_to :upload
|
2018-10-15 12:55:23 +08:00
|
|
|
has_one :javascript_cache, dependent: :destroy
|
2017-05-08 23:38:48 +08:00
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
scope :find_by_theme_ids, ->(theme_ids) {
|
|
|
|
return none unless theme_ids.present?
|
|
|
|
|
|
|
|
where(theme_id: theme_ids)
|
|
|
|
.joins(
|
|
|
|
"JOIN (
|
|
|
|
SELECT #{theme_ids.map.with_index { |id, idx| "#{id.to_i} AS theme_id, #{idx} AS sort_column" }.join(" UNION ALL SELECT ")}
|
|
|
|
) as X ON X.theme_id = theme_fields.theme_id")
|
|
|
|
.order("sort_column")
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:01:01 +08:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(html: 0,
|
|
|
|
scss: 1,
|
|
|
|
theme_upload_var: 2,
|
|
|
|
theme_color_var: 3,
|
2018-03-05 08:04:23 +08:00
|
|
|
theme_var: 4,
|
|
|
|
yaml: 5)
|
2017-05-03 04:01:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.theme_var_type_ids
|
2017-07-28 09:20:09 +08:00
|
|
|
@theme_var_type_ids ||= [2, 3, 4]
|
2017-05-03 04:01:01 +08:00
|
|
|
end
|
|
|
|
|
2019-01-10 18:06:01 +08:00
|
|
|
def self.force_recompilation!
|
|
|
|
find_each do |field|
|
|
|
|
field.compiler_version = 0
|
|
|
|
field.ensure_baked!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-19 23:10:44 +08:00
|
|
|
validates :name, format: { with: /\A[a-z_][a-z0-9_-]*\z/i },
|
|
|
|
if: Proc.new { |field| ThemeField.theme_var_type_ids.include?(field.type_id) }
|
|
|
|
|
2019-01-10 19:36:13 +08:00
|
|
|
COMPILER_VERSION = 6
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
belongs_to :theme
|
|
|
|
|
2018-03-05 12:35:41 +08:00
|
|
|
def settings(source)
|
|
|
|
|
|
|
|
settings = {}
|
|
|
|
|
|
|
|
theme.cached_settings.each do |k, v|
|
|
|
|
if source.include?("settings.#{k}")
|
|
|
|
settings[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if settings.length > 0
|
|
|
|
"let settings = #{settings.to_json};"
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def transpile(es6_source, version)
|
2017-07-28 09:20:09 +08:00
|
|
|
template = Tilt::ES6ModuleTranspilerTemplate.new {}
|
2017-04-12 22:52:52 +08:00
|
|
|
wrapped = <<PLUGIN_API_JS
|
2019-01-15 18:25:32 +08:00
|
|
|
if ('Discourse' in window && typeof Discourse._registerPluginCode === 'function') {
|
2017-04-12 22:52:52 +08:00
|
|
|
Discourse._registerPluginCode('#{version}', api => {
|
2018-03-05 12:35:41 +08:00
|
|
|
#{settings(es6_source)}
|
2017-04-12 22:52:52 +08:00
|
|
|
#{es6_source}
|
|
|
|
});
|
2018-07-18 13:13:47 +08:00
|
|
|
}
|
2017-04-12 22:52:52 +08:00
|
|
|
PLUGIN_API_JS
|
|
|
|
|
|
|
|
template.babel_transpile(wrapped)
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_html(html)
|
2017-04-20 04:46:28 +08:00
|
|
|
errors = nil
|
2018-10-15 12:55:23 +08:00
|
|
|
javascript_cache || build_javascript_cache
|
|
|
|
javascript_cache.content = ''
|
2017-04-20 04:46:28 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
doc.css('script[type="text/x-handlebars"]').each do |node|
|
|
|
|
name = node["name"] || node["data-template-name"] || "broken"
|
2018-04-03 04:16:16 +08:00
|
|
|
|
2018-04-13 02:30:36 +08:00
|
|
|
is_raw = name =~ /\.raw$/
|
2018-04-03 04:16:16 +08:00
|
|
|
setting_helpers = ''
|
|
|
|
theme.cached_settings.each do |k, v|
|
|
|
|
val = v.is_a?(String) ? "\"#{v.gsub('"', "\\u0022")}\"" : v
|
2018-04-13 02:30:36 +08:00
|
|
|
setting_helpers += "{{theme-setting-injector #{is_raw ? "" : "context=this"} key=\"#{k}\" value=#{val}}}\n"
|
2018-04-03 04:16:16 +08:00
|
|
|
end
|
|
|
|
hbs_template = setting_helpers + node.inner_html
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
if is_raw
|
2018-04-13 02:30:36 +08:00
|
|
|
template = "requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(hbs_template)})"
|
2018-10-15 12:55:23 +08:00
|
|
|
javascript_cache.content << <<COMPILED
|
|
|
|
(function() {
|
|
|
|
if ('Discourse' in window) {
|
2017-04-12 22:52:52 +08:00
|
|
|
Discourse.RAW_TEMPLATES[#{name.sub(/\.raw$/, '').inspect}] = #{template};
|
2018-10-15 12:55:23 +08:00
|
|
|
}
|
|
|
|
})();
|
2017-04-12 22:52:52 +08:00
|
|
|
COMPILED
|
|
|
|
else
|
2018-04-03 04:16:16 +08:00
|
|
|
template = "Ember.HTMLBars.template(#{Barber::Ember::Precompiler.compile(hbs_template)})"
|
2018-10-15 12:55:23 +08:00
|
|
|
javascript_cache.content << <<COMPILED
|
|
|
|
(function() {
|
|
|
|
if ('Em' in window) {
|
2017-04-12 22:52:52 +08:00
|
|
|
Ember.TEMPLATES[#{name.inspect}] = #{template};
|
2018-10-15 12:55:23 +08:00
|
|
|
}
|
|
|
|
})();
|
2017-04-12 22:52:52 +08:00
|
|
|
COMPILED
|
|
|
|
end
|
|
|
|
|
2018-10-15 12:55:23 +08:00
|
|
|
node.remove
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
doc.css('script[type="text/discourse-plugin"]').each do |node|
|
|
|
|
if node['version'].present?
|
|
|
|
begin
|
2018-10-15 12:55:23 +08:00
|
|
|
javascript_cache.content << transpile(node.inner_html, node['version'])
|
2017-04-12 22:52:52 +08:00
|
|
|
rescue MiniRacer::RuntimeError => ex
|
2018-10-15 12:55:23 +08:00
|
|
|
javascript_cache.content << "console.error('Theme Transpilation Error:', #{ex.message.inspect});"
|
|
|
|
|
2017-04-20 04:46:28 +08:00
|
|
|
errors ||= []
|
|
|
|
errors << ex.message
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2018-10-15 12:55:23 +08:00
|
|
|
|
|
|
|
node.remove
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-15 12:55:23 +08:00
|
|
|
doc.css('script').each do |node|
|
2018-11-02 04:01:46 +08:00
|
|
|
next unless inline_javascript?(node)
|
2018-10-15 12:55:23 +08:00
|
|
|
|
2018-11-02 04:01:46 +08:00
|
|
|
javascript_cache.content << node.inner_html
|
|
|
|
javascript_cache.content << "\n"
|
2018-10-15 12:55:23 +08:00
|
|
|
node.remove
|
|
|
|
end
|
|
|
|
|
|
|
|
javascript_cache.save!
|
|
|
|
|
|
|
|
doc.add_child("<script src='#{javascript_cache.url}'></script>") if javascript_cache.content.present?
|
2017-04-20 04:46:28 +08:00
|
|
|
[doc.to_s, errors&.join("\n")]
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def validate_yaml!
|
|
|
|
return unless self.name == "yaml"
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
begin
|
|
|
|
ThemeSettingsParser.new(self).load do |name, default, type, opts|
|
|
|
|
setting = ThemeSetting.new(name: name, data_type: type, theme: theme)
|
|
|
|
translation_key = "themes.settings_errors"
|
|
|
|
|
|
|
|
if setting.invalid?
|
|
|
|
setting.errors.details.each_pair do |attribute, _errors|
|
|
|
|
_errors.each do |hash|
|
|
|
|
errors << I18n.t("#{translation_key}.#{attribute}_#{hash[:error]}", name: name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if default.nil?
|
|
|
|
errors << I18n.t("#{translation_key}.default_value_missing", name: name)
|
|
|
|
end
|
|
|
|
|
|
|
|
if (min = opts[:min]) && (max = opts[:max])
|
|
|
|
unless ThemeSetting.value_in_range?(default, (min..max), type)
|
|
|
|
errors << I18n.t("#{translation_key}.default_out_range", name: name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless ThemeSetting.acceptable_value_for_type?(default, type)
|
|
|
|
errors << I18n.t("#{translation_key}.default_not_match_type", name: name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ThemeSettingsParser::InvalidYaml => e
|
|
|
|
errors << e.message
|
|
|
|
end
|
|
|
|
|
2018-12-19 23:36:31 +08:00
|
|
|
self.error = errors.join("\n").presence
|
2018-03-05 08:04:23 +08:00
|
|
|
end
|
|
|
|
|
2017-05-03 04:01:01 +08:00
|
|
|
def self.guess_type(name)
|
|
|
|
if html_fields.include?(name.to_s)
|
|
|
|
types[:html]
|
|
|
|
elsif scss_fields.include?(name.to_s)
|
|
|
|
types[:scss]
|
2018-03-05 08:04:23 +08:00
|
|
|
elsif name.to_s === "yaml"
|
|
|
|
types[:yaml]
|
2017-05-03 04:01:01 +08:00
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def self.html_fields
|
2017-05-03 04:01:01 +08:00
|
|
|
@html_fields ||= %w(body_tag head_tag header footer after_header)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2017-04-20 04:46:28 +08:00
|
|
|
def self.scss_fields
|
2017-05-03 04:01:01 +08:00
|
|
|
@scss_fields ||= %w(scss embedded_scss)
|
2017-04-20 04:46:28 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def ensure_baked!
|
|
|
|
if ThemeField.html_fields.include?(self.name)
|
|
|
|
if !self.value_baked || compiler_version != COMPILER_VERSION
|
2017-04-20 04:46:28 +08:00
|
|
|
self.value_baked, self.error = process_html(self.value)
|
2017-04-12 22:52:52 +08:00
|
|
|
self.compiler_version = COMPILER_VERSION
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
if self.will_save_change_to_value_baked? ||
|
|
|
|
self.will_save_change_to_compiler_version? ||
|
|
|
|
self.will_save_change_to_error?
|
|
|
|
|
2017-04-20 04:46:28 +08:00
|
|
|
self.update_columns(value_baked: value_baked,
|
|
|
|
compiler_version: compiler_version,
|
|
|
|
error: error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_scss_compiles!
|
|
|
|
if ThemeField.scss_fields.include?(self.name)
|
|
|
|
begin
|
|
|
|
Stylesheet::Compiler.compile("@import \"theme_variables\"; @import \"theme_field\";",
|
|
|
|
"theme.scss",
|
2017-05-10 05:20:28 +08:00
|
|
|
theme_field: self.value.dup,
|
|
|
|
theme: self.theme
|
|
|
|
)
|
2017-04-20 04:46:28 +08:00
|
|
|
self.error = nil unless error.nil?
|
|
|
|
rescue SassC::SyntaxError => e
|
2018-03-05 08:04:23 +08:00
|
|
|
self.error = e.message unless self.destroyed?
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2017-04-21 04:55:09 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
if will_save_change_to_error?
|
2017-04-21 04:55:09 +08:00
|
|
|
update_columns(error: self.error)
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_name
|
2017-05-03 04:01:01 +08:00
|
|
|
Theme.targets.invert[target_id].to_s
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
before_save do
|
2018-12-19 23:36:31 +08:00
|
|
|
validate_yaml!
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
if will_save_change_to_value? && !will_save_change_to_value_baked?
|
2017-04-12 22:52:52 +08:00
|
|
|
self.value_baked = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after_commit do
|
|
|
|
ensure_baked!
|
2017-04-20 04:46:28 +08:00
|
|
|
ensure_scss_compiles!
|
2018-03-05 08:04:23 +08:00
|
|
|
theme.clear_cached_settings!
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
Stylesheet::Manager.clear_theme_cache! if self.name.include?("scss")
|
2018-11-30 22:51:45 +08:00
|
|
|
CSP::Extension.clear_theme_extensions_cache! if name == 'yaml'
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
# TODO message for mobile vs desktop
|
2018-07-12 12:18:21 +08:00
|
|
|
MessageBus.publish "/header-change/#{theme.id}", self.value if theme && self.name == "header"
|
|
|
|
MessageBus.publish "/footer-change/#{theme.id}", self.value if theme && self.name == "footer"
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2018-11-02 04:01:46 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
JAVASCRIPT_TYPES = %w(
|
|
|
|
text/javascript
|
|
|
|
application/javascript
|
|
|
|
application/ecmascript
|
|
|
|
)
|
|
|
|
|
|
|
|
def inline_javascript?(node)
|
|
|
|
if node['src'].present?
|
|
|
|
false
|
|
|
|
elsif node['type'].present?
|
|
|
|
JAVASCRIPT_TYPES.include?(node['type'].downcase)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: theme_fields
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# theme_id :integer not null
|
2017-05-03 04:01:01 +08:00
|
|
|
# target_id :integer not null
|
|
|
|
# name :string(30) not null
|
2017-04-12 22:52:52 +08:00
|
|
|
# value :text not null
|
|
|
|
# value_baked :text
|
2019-01-12 03:29:56 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2017-04-12 22:52:52 +08:00
|
|
|
# compiler_version :integer default(0), not null
|
2017-04-21 03:47:25 +08:00
|
|
|
# error :string
|
2017-05-03 04:01:01 +08:00
|
|
|
# upload_id :integer
|
|
|
|
# type_id :integer default(0), not null
|
2017-04-12 22:52:52 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2017-05-03 04:01:01 +08:00
|
|
|
# theme_field_unique_index (theme_id,target_id,type_id,name) UNIQUE
|
2017-04-12 22:52:52 +08:00
|
|
|
#
|