Class: Less::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/less/parser.rb

Overview

Convert lesscss source into an abstract syntax Tree

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Construct and configure new Less::Parser

Parameters:

  • opts (Hash)

    configuration options



20
21
22
23
# File 'lib/less/parser.rb', line 20

def initialize(options = {})
  @options = Less.defaults.merge(options)
  @context = self.class.backend.compile(compiler_source)
end

Class Method Details

.backendObject



5
6
7
8
9
10
11
# File 'lib/less/parser.rb', line 5

def backend
  @backend ||= ExecJS::ExternalRuntime.new(
    name:        'Node.js (V8)',
    command:     %w(nodejs node),
    runner_path: File.expand_path('../runner.js', __FILE__)
  )
end

Instance Method Details

#parse(less, options = {}) ⇒ Less::Tree

Convert ‘less` source into a abstract syntaxt tree

Parameters:

  • less (String)

    the source to parse

  • opts (Hash)

    render options

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/less/parser.rb', line 29

def parse(less, options = {})
  old_node_env = ENV['NODE_PATH']
  opts = @options.merge(options)
  if opts[:custom_functions]
    ENV['NODE_PATH'] = "#{opts[:custom_functions].dirname}:#{old_node_env}"
    opts[:custom_functions] = opts[:custom_functions].to_s.split('/').last
  end
  Result.new @context.call('render', less, opts)
rescue ExecJS::ProgramError => e
  raise ParseError.new(e.message)
ensure
  ENV['NODE_PATH'] = old_node_env
end