Class: JadePug::ShippedCompiler

Inherits:
Compiler
  • Object
show all
Defined in:
lib/jade-pug/shipped-compiler.rb

Overview

Abstraction layer for shipped engine compiler.

Direct Known Subclasses

Jade::ShippedCompiler, Pug::ShippedCompiler

Instance Attribute Summary

Attributes inherited from Compiler

#engine, #version

Instance Method Summary collapse

Methods inherited from Compiler

#compilation_snippet, #prepare_options, #prepare_source, #process_result, #shipped?, #system?

Constructor Details

#initialize(engine, version) ⇒ ShippedCompiler

Returns a new instance of ShippedCompiler.

Parameters:

  • engine (Jade, Pug)

    The Jade or Pug module.

  • version (String)

14
15
16
17
# File 'lib/jade-pug/shipped-compiler.rb', line 14

def initialize(engine, version)
  super
  @execjs = compile_compiler_source(read_compiler_source(path_to_compiler_source))
end

Instance Method Details

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

Compiles template.

Parameters:

  • source (String, #read)
  • options (Hash) (defaults to: {})

Returns:

  • (String)

25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jade-pug/shipped-compiler.rb', line 25

def compile(source, options = {})
  source  = prepare_source(source)
  options = prepare_options(options)
  snippet = compilation_snippet \
    method:    "compile#{ "Client" if options[:client] }",
    arguments: [source, options],
    locals:    options.fetch(:locals, {}),
    options:   options
  result  = @execjs.eval(snippet)
  process_result(source, result, options)
rescue ExecJS::ProgramError => e
  raise engine::CompilationError, e.message
end

#compile_compiler_source(source) ⇒ ExecJS::Runtime (protected)

Compiles the compiler from source and returns it as ExecJS::Runtime.

Parameters:

  • source (String)

Returns:

  • (ExecJS::Runtime)

64
65
66
67
68
# File 'lib/jade-pug/shipped-compiler.rb', line 64

def compile_compiler_source(source)
  ExecJS.compile(source).tap do |compiler|
    raise engine::CompilerError, "Failed to compile #{ engine.name } compiler" unless compiler
  end
end

#npm_package_require_snippetString (protected)

Returns the JavaScript code used to access engine NPM package.

Returns:

  • (String)

74
75
76
# File 'lib/jade-pug/shipped-compiler.rb', line 74

def npm_package_require_snippet
  engine.name.downcase
end

#path_to_compiler_sourceString (protected)

Returns absolute path to the file with compiler source.

Returns:

  • (String)

45
46
47
# File 'lib/jade-pug/shipped-compiler.rb', line 45

def path_to_compiler_source
  File.expand_path("../../../vendor/#{ engine.name.downcase }-#{ version }.min.js", __FILE__)
end

#read_compiler_source(path) ⇒ String (protected)

Reads the compiler source from a file and returns it.

Parameters:

  • path (String)

Returns:

  • (String)

Raises:

  • (engine::CompilerError)

54
55
56
57
# File 'lib/jade-pug/shipped-compiler.rb', line 54

def read_compiler_source(path)
  raise engine::CompilerError, "Couldn't read compiler source: #{ path }" unless File.readable?(path)
  File.read(path)
end