Class: JadePug::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jade-pug/compiler.rb

Overview

Abstraction layer for engine compiler.

Direct Known Subclasses

ShippedCompiler, SystemCompiler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, version) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:

  • engine (Jade, Pug)
  • version (String)


34
35
36
37
# File 'lib/jade-pug/compiler.rb', line 34

def initialize(engine, version)
  @engine  = engine
  @version = version
end

Instance Attribute Details

#engineJade, Pug (readonly)

Returns the engine module.

Used in such cases:

  • used to compute the name for engine

  • used to refer to the error classes

Returns:



23
24
25
# File 'lib/jade-pug/compiler.rb', line 23

def engine
  @engine
end

#versionString (readonly)

Returns the version of engine compiler.

Returns:

  • (String)


29
30
31
# File 'lib/jade-pug/compiler.rb', line 29

def version
  @version
end

Instance Method Details

#compilation_snippet(args) ⇒ String (protected)

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

Parameters:

  • :method (String)

    The name of engine method to call.

  • :arguments (Array<Object>)

    The array of arguments to be passed to the method.

  • :locals (Hash)

    The hash of template local variables to be used to render template.

  • :options (Hash)

    The hash of options passed to #compile.

Returns:

  • (String)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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

#compile(source, options = {}) ⇒ String

This method is abstract.

Derived compilers must implement it.

Compiles template.

By default does nothing.

Parameters:

  • source (String, #read)

    The template source code or any object which responds to #read and returns string.

  • options (Hash) (defaults to: {})

Returns:

  • (String)


49
50
51
# File 'lib/jade-pug/compiler.rb', line 49

def compile(source, options = {})
  method_not_implemented
end

#npm_package_require_snippetString (protected)

This method is abstract.

Derived compilers must implement it.

Returns the JavaScript code used to access engine NPM module.

Returns:

  • (String)


147
148
149
# File 'lib/jade-pug/compiler.rb', line 147

def npm_package_require_snippet
  method_not_implemented
end

#prepare_options(options) ⇒ Hash (protected)

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

Parameters:

  • options (Hash)

Returns:

  • (Hash)


94
95
96
97
98
# 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

#prepare_source(source) ⇒ String (protected)

Reads the template source code. Responds for pre-processing source code. Actually, it just checks if source code responds to #read and if so

Parameters:

  • source (String, #read)

Returns:

  • (String)


80
81
82
# File 'lib/jade-pug/compiler.rb', line 80

def prepare_source(source)
  source.respond_to?(:read) ? source.read : source
end

#process_result(source, result, options) ⇒ String (protected)

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.

Parameters:

  • source (String)

    The source code of template.

  • result (String)

    The compiled code of template.

  • options (Hash)

    The compilation options.

Returns:

  • (String)


162
163
164
# File 'lib/jade-pug/compiler.rb', line 162

def process_result(source, result, options)
  result.strip
end

#shipped?true, false

Returns true if this compiler is a shipped compiler. Otherwise returns false.

Returns:

  • (true, false)


67
68
69
# File 'lib/jade-pug/compiler.rb', line 67

def shipped?
  ShippedCompiler === self
end

#system?true, false

Returns true if this compiler is a system compiler. Otherwise returns false.

Returns:

  • (true, false)


58
59
60
# File 'lib/jade-pug/compiler.rb', line 58

def system?
  SystemCompiler === self
end