discourse/lib/freedom_patches/raw_handlebars.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# barber patches to re-route raw compilation via ember compat handlebars
class Barber::Precompiler
def sources
2016-07-01 05:10:08 +08:00
[File.open("#{Rails.root}/vendor/assets/javascripts/handlebars.js"),
precompiler]
end
def precompiler
2016-07-01 05:10:08 +08:00
if !@precompiler
source = File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js")
transpiler = DiscourseJsProcessor::Transpiler.new(skip_module: true)
transpiled = transpiler.perform(source)
2016-07-01 05:10:08 +08:00
# very hacky but lets us use ES6. I'm ashamed of this code -RW
transpiled = transpiled[transpiled.index('var RawHandlebars = ')...transpiled.index('export ')]
2016-07-01 05:10:08 +08:00
@precompiler = StringIO.new <<~END
var __RawHandlebars;
(function() {
#{transpiled};
__RawHandlebars = RawHandlebars;
})();
Barber = {
precompile: function(string) {
return __RawHandlebars.precompile(string, false).toString();
}
};
END
2016-07-01 05:10:08 +08:00
end
@precompiler
end
end
module Discourse
module Ember
module Handlebars
module Helper
def precompile_handlebars(string)
2017-07-06 02:14:30 +08:00
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string)
2017-07-06 02:14:30 +08:00
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
end
end
end
end
end
class Ember::Handlebars::Template
include Discourse::Ember::Handlebars::Helper
def precompile_handlebars(string, input = nil)
2017-07-06 02:14:30 +08:00
"requirejs('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string, input = nil)
2017-07-06 02:14:30 +08:00
"requirejs('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
end
def global_template_target(namespace, module_name, config)
# We need this for backward-compatibility reasons.
# Plugins may not have an app subdirectory.
path = template_path(module_name, config).inspect.gsub('discourse/templates/', '')
"#{namespace}[#{path}]"
end
# FIXME: Previously, ember-handlebars-templates uses the logical path which incorrectly
# returned paths with the `.raw` extension and our code is depending on the `.raw`
# to find the right template to use.
def actual_name(input)
actual_name = input[:name]
input[:filename].include?('.raw') ? "#{actual_name}.raw" : actual_name
end
private
def handlebars?(filename)
filename.to_s =~ /\.raw\.(handlebars|hjs|hbs)/ || filename.to_s.ends_with?(".hbr") || filename.to_s.ends_with?(".hbr.erb")
end
end