require "memoist"
require "method-not-implemented"
require "json"
module JadePug
class Compiler
extend Memoist
attr_reader :engine
attr_reader :version
def initialize(engine, version)
@engine = engine
@version = version
end
def compile(source, options = {})
method_not_implemented
end
def system?
SystemCompiler === self
end
def shipped?
ShippedCompiler === self
end
protected
def prepare_source(source)
source.respond_to?(:read) ? source.read : source
end
def prepare_options(options)
options = engine.config.to_hash.merge(options)
options.keys.each { |k| options[k.to_s.gsub(/_([a-z])/) { $1.upcase }.to_sym] = options[k] }
options.delete_if { |k, v| v.nil? }
end
def compilation_snippet(args)
method = args.fetch(:method)
arguments = args.fetch(:arguments)
locals = args.fetch(:locals)
options = args.fetch(:options)
" (function() {\n var engine = \#{ npm_package_require_snippet };\n var template = engine[\#{ JSON.dump(method) }].apply(engine, \#{ JSON.dump(arguments) });\n\n if (typeof template === 'function') {\n template = template(\#{ JSON.dump(locals) });\n }\n\n if (typeof console === 'object' && console !== null && typeof console.log === 'function') {\n console.log(template);\n }\n\n return template;\n })()\n JAVASCRIPT\n end\n\n #\n # Returns the JavaScript code used to access engine NPM module.\n #\n # @abstract Derived compilers must implement it.\n # @return [String]\n def npm_package_require_snippet\n method_not_implemented\n end\n\n #\n # Responds for post-processing compilation result.\n #\n # By default removes leading and trailing whitespace\n # by calling {String#strip} and returns the result.\n # Derived compilers may override it for it's own special behavior.\n #\n # @param source [String] The source code of template.\n # @param result [String] The compiled code of template.\n # @param options [Hash] The compilation options.\n # @return [String]\n def process_result(source, result, options)\n result.strip\n end\n end\nend\n"