class JadePug::Compiler
Abstraction layer for engine compiler.
Attributes
Returns the engine module.
Used in such cases:
-
used to compute the name for engine
-
used to refer to the error classes
@return [Jade, Pug]
Returns the version of engine compiler.
@return [String]
Public Class Methods
@param engine [Jade, Pug] @param version [String]
# File lib/jade-pug/compiler.rb, line 34 def initialize(engine, version) @engine = engine @version = version end
Public Instance Methods
Compiles template.
By default does nothing.
@abstract Derived compilers must implement it. @param source [String, read]
The template source code or any object which responds to #read and returns string.
@param options [Hash] @return [String]
# File lib/jade-pug/compiler.rb, line 49 def compile(source, options = {}) method_not_implemented end
Returns true if this compiler is a shipped compiler. Otherwise returns false.
@return [true, false]
# File lib/jade-pug/compiler.rb, line 67 def shipped? ShippedCompiler === self end
Returns true if this compiler is a system compiler. Otherwise returns false.
@return [true, false]
# File lib/jade-pug/compiler.rb, line 58 def system? SystemCompiler === self end
Protected Instance Methods
Generates the JavaScript code that is the bridge from the gem to the template engine compiler.
The code responds for:
-
invoking compiler
-
rendering template function with given locals
-
returning the result
@param :method [String]
The name of engine method to call.
@param :arguments [Array<Object>]
The array of arguments to be passed to the method.
@param :locals [Hash]
The hash of template local variables to be used to render template.
@param :options [Hash]
The hash of options passed to {#compile}.
@return [String]
# File lib/jade-pug/compiler.rb, line 118 def compilation_snippet(args) method = args.fetch(:method) arguments = args.fetch(:arguments) locals = args.fetch(:locals) options = args.fetch(:options) <<-JAVASCRIPT (function() { var engine = #{ npm_package_require_snippet }; var template = engine[#{ JSON.dump(method) }].apply(engine, #{ JSON.dump(arguments) }); if (typeof template === 'function') { template = template(#{ JSON.dump(locals) }); } if (typeof console === 'object' && console !== null && typeof console.log === 'function') { console.log(template); } return template; })() JAVASCRIPT end
Returns the JavaScript code used to access engine NPM module.
@abstract Derived compilers must implement it. @return [String]
# File lib/jade-pug/compiler.rb, line 147 def npm_package_require_snippet method_not_implemented end
Responds for preparing compilation options.
The next steps are executed:
-
is merges options into the engine config
-
it camelizes and symbolizes every option key
-
it removes nil values from the options
@param options [Hash] @return [Hash]
# File lib/jade-pug/compiler.rb, line 94 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
Reads the template source code. Responds for pre-processing source code. Actually, it just checks if source code responds to read and if so
@param source [String, read] @return [String]
# File lib/jade-pug/compiler.rb, line 80 def prepare_source(source) source.respond_to?(:read) ? source.read : source end
Responds for post-processing compilation result.
By default removes leading and trailing whitespace by calling {String#strip} and returns the result. Derived compilers may override it for it's own special behavior.
@param source [String] The source code of template. @param result [String] The compiled code of template. @param options [Hash] The compilation options. @return [String]
# File lib/jade-pug/compiler.rb, line 162 def process_result(source, result, options) result.strip end