Class: Less2Sass::Sass::ASTHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/less2sass/sass/ast_handler.rb

Overview

Wrapped Sass AST

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ ASTHandler

Creates an empty Sass AST.

Parameters:

  • tree (::Sass::Tree::Node)

    the Sass AST



8
9
10
# File 'lib/less2sass/sass/ast_handler.rb', line 8

def initialize(tree)
  @tree = tree
end

Instance Method Details

#<<(child) ⇒ Object

Appends child notes to the empty Sass AST.

Note, that this should be called only once.

Parameters:

  • child (::Sass::Tree::Node)

    the rest of the converted Less AST



18
19
20
21
22
# File 'lib/less2sass/sass/ast_handler.rb', line 18

def <<(child)
  # TODO: check if child.is_a?(::Sass::Tree::Node) and raise some error, if not
  @tree << child
  self
end

#code_gen(destination = nil, syntax) ⇒ Object

TODO:

method cannot handle import nodes, yet - single files only

Outputs the transformed and converted AST in the form of Sass code.

Parameters:

  • destination (String, IO) (defaults to: nil)

    the base directory or an IO Stream where the converted projects should be outputted.



36
37
38
39
40
41
42
43
44
# File 'lib/less2sass/sass/ast_handler.rb', line 36

def code_gen(destination = nil, syntax)
  code = syntax == :scss ? @tree.to_scss : @tree.to_sass
  return code unless destination
  if destination.is_a?(String)
    open_file(destination, 'w') { |file| file.write(code) }
  else
    destination.write(code)
  end
end

#open_file(filename, flag = 'r') {|File.open(filename, flag)| ... } ⇒ Object

Yields:

  • (File.open(filename, flag))


46
47
48
49
50
# File 'lib/less2sass/sass/ast_handler.rb', line 46

def open_file(filename, flag = 'r')
  return if filename.nil?
  return File.open(filename, flag) unless block_given?
  yield File.open(filename, flag)
end

#to_cssObject



24
25
26
# File 'lib/less2sass/sass/ast_handler.rb', line 24

def to_css
  @tree.render
end

#to_yamlObject



52
53
54
# File 'lib/less2sass/sass/ast_handler.rb', line 52

def to_yaml
  @tree.to_yaml
end