class Sass::Engine

puts output
output = sass_engine.render
sass_engine = Sass::Engine.new(template)
template = File.load(‘stylesheets/sassy.sass’)
Example usage:
This class handles the parsing and compilation of the Sass template.

def append_children(parent, children, root)

def append_children(parent, children, root)
  continued_rule = nil
  children.each do |line|
    child = build_tree(parent, line, root)
    if child.is_a?(Tree::RuleNode) && child.continued?
      raise SyntaxError.new("Rules can't end in commas.", child.line) unless child.children.empty?
      if continued_rule
        continued_rule.add_rules child
      else
        continued_rule = child
      end
      next
    end
    if continued_rule
      raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) unless child.is_a?(Tree::RuleNode)
      continued_rule.add_rules child
      continued_rule.children = child.children
      continued_rule, child = nil, continued_rule
    end
    check_for_no_children(child)
    validate_and_append_child(parent, child, line, root)
  end
  raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) if continued_rule
  parent
end

def build_tree(parent, line, root = false)

def build_tree(parent, line, root = false)
  @line = line.index
  node_or_nodes = parse_line(parent, line, root)
  Array(node_or_nodes).each do |node|
    # Node is a symbol if it's non-outputting, like a variable assignment
    next unless node.is_a? Tree::Node
    node.line = line.index
    node.filename = line.filename
    if node.is_a?(Tree::CommentNode)
      node.lines = line.children
    else
      append_children(node, line.children, false)
    end
  end
  node_or_nodes
end

def check_for_no_children(node)

def check_for_no_children(node)
  return unless node.is_a?(Tree::RuleNode) && node.children.empty?
  warning = (node.rules.size == 1) ? <<SHORT : <<LONG
ING on line #{node.line}:
ctor #{node.rules.first.inspect} doesn't have any properties and will not be rendered.
T
ING on line #{node.line}:
ctor
node.rules.join("\n  ")}
n't have any properties and will not be rendered.

  warn(warning.strip)
end

def initialize(template, options={})

Parameters:
  • options (Hash) -- An options hash;
  • template (String) -- The Sass template.
def initialize(template, options={})
  @options = DEFAULT_OPTIONS.merge(options)
  @template = template
  # Backwards compatibility
  @options[:property_syntax] ||= @options[:attribute_syntax]
  case @options[:property_syntax]
  when :alternate; @options[:property_syntax] = :new
  when :normal; @options[:property_syntax] = :old
  end
end

def parse_comment(line)

def parse_comment(line)
  if line[1] == CSS_COMMENT_CHAR || line[1] == SASS_COMMENT_CHAR
    Tree::CommentNode.new(line, line[1] == SASS_COMMENT_CHAR)
  else
    Tree::RuleNode.new(line)
  end
end

def parse_directive(parent, line, root)

def parse_directive(parent, line, root)
  directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2)
  offset = directive.size + whitespace.size + 1 if whitespace
  # If value begins with url( or ",
  # it's a CSS @import rule and we don't want to touch it.
  if directive == "import" && value !~ /^(url\(|")/
    raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.", @line + 1) unless line.children.empty?
    value.split(/,\s*/).map {|f| Tree::ImportNode.new(f)}
  elsif directive == "for"
    parse_for(line, root, value)
  elsif directive == "else"
    parse_else(parent, line, value)
  elsif directive == "while"
    raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value
    Tree::WhileNode.new(parse_script(value, :offset => offset))
  elsif directive == "if"
    raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value
    Tree::IfNode.new(parse_script(value, :offset => offset))
  elsif directive == "debug"
    raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value
    raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.", @line + 1) unless line.children.empty?
    offset = line.offset + line.text.index(value).to_i
    Tree::DebugNode.new(parse_script(value, :offset => offset))
  else
    Tree::DirectiveNode.new(line.text)
  end
end

def parse_else(parent, line, text)

def parse_else(parent, line, text)
  previous = parent.last
  raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode)
  if text
    if text !~ /^if\s+(.+)/
      raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.", @line)
    end
    expr = parse_script($1, :offset => line.offset + line.text.index($1))
  end
  node = Tree::IfNode.new(expr)
  append_children(node, line.children, false)
  previous.add_else node
  nil
end

def parse_for(line, root, text)

def parse_for(line, root, text)
  var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
  if var.nil? # scan failed, try to figure out why for error message
    if text !~ /^[^\s]+/
      expected = "variable name"
    elsif text !~ /^[^\s]+\s+from\s+.+/
      expected = "'from <expr>'"
    else
      expected = "'to <expr>' or 'through <expr>'"
    end
    raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.", @line)
  end
  raise SyntaxError.new("Invalid variable \"#{var}\".", @line) unless var =~ Script::VALIDATE
  parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr))
  parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr))
  Tree::ForNode.new(var[1..-1], parsed_from, parsed_to, to_name == 'to')
end

def parse_line(parent, line, root)

def parse_line(parent, line, root)
  case line.text[0]
  when PROPERTY_CHAR
    if line.text[1] != PROPERTY_CHAR
      parse_property(line, PROPERTY_OLD)
    else
      # Support CSS3-style pseudo-elements,
      # which begin with ::
      Tree::RuleNode.new(line.text)
    end
  when Script::VARIABLE_CHAR
    parse_variable(line)
  when COMMENT_CHAR
    parse_comment(line.text)
  when DIRECTIVE_CHAR
    parse_directive(parent, line, root)
  when ESCAPE_CHAR
    Tree::RuleNode.new(line.text[1..-1])
  when MIXIN_DEFINITION_CHAR
    parse_mixin_definition(line)
  when MIXIN_INCLUDE_CHAR
    if line.text[1].nil? || line.text[1] == ?\s
      Tree::RuleNode.new(line.text)
    else
      parse_mixin_include(line, root)
    end
  else
    if line.text =~ PROPERTY_NEW_MATCHER
      parse_property(line, PROPERTY_NEW)
    else
      Tree::RuleNode.new(line.text)
    end
  end
end

def parse_mixin_definition(line)

def parse_mixin_definition(line)
  name, arg_string = line.text.scan(/^=\s*([^(]+)(.*)$/).first
  raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".", @line) if name.nil?
  offset = line.offset + line.text.size - arg_string.size
  args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_definition_arglist
  default_arg_found = false
  Tree::MixinDefNode.new(name, args)
end

def parse_mixin_include(line, root)

def parse_mixin_include(line, root)
  name, arg_string = line.text.scan(/^\+\s*([^(]+)(.*)$/).first
  raise SyntaxError.new("Invalid mixin include \"#{line.text}\".", @line) if name.nil?
  offset = line.offset + line.text.size - arg_string.size
  args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_include_arglist
  raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.", @line + 1) unless line.children.empty?
  Tree::MixinNode.new(name, args)
end

def parse_property(line, property_regx)

def parse_property(line, property_regx)
  name, eq, value = line.text.scan(property_regx)[0]
  if name.nil? || value.nil?
    raise SyntaxError.new("Invalid property: \"#{line.text}\".", @line)
  end
  expr = if (eq.strip[0] == SCRIPT_CHAR)
    parse_script(value, :offset => line.offset + line.text.index(value))
  else
    value
  end
  Tree::PropNode.new(name, expr, property_regx == PROPERTY_OLD ? :old : :new)
end

def parse_script(script, options = {})

def parse_script(script, options = {})
  line = options[:line] || @line
  offset = options[:offset] || 0
  Script.parse(script, line, offset, @options[:filename])
end

def parse_variable(line)

def parse_variable(line)
  name, op, value = line.text.scan(Script::MATCH)[0]
  raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.", @line + 1) unless line.children.empty?
  raise SyntaxError.new("Invalid variable: \"#{line.text}\".", @line) unless name && value
  Tree::VariableNode.new(name, parse_script(value, :offset => line.offset + line.text.index(value)), op == '||=')
end

def render

Raises:
  • (Sass::SyntaxError) - if there's an error in the document

Returns:
  • (String) - The CSS
def render
  to_tree.render
end

def tabulate(string)

def tabulate(string)
  tab_str = nil
  first = true
  lines = []
  string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/).each_with_index do |line, index|
    index += (@options[:line] || 1)
    if line.strip.empty?
      lines.last.text << "\n" if lines.last && lines.last.comment?
      next
    end
    line_tab_str = line[/^\s*/]
    unless line_tab_str.empty?
      tab_str ||= line_tab_str
      raise SyntaxError.new("Indenting at the beginning of the document is illegal.", index) if first
      if tab_str.include?(?\s) && tab_str.include?(?\t)
        raise SyntaxError.new("Indentation can't use both tabs and spaces.", index)
      end
    end
    first &&= !tab_str.nil?
    if tab_str.nil?
      lines << Line.new(line.strip, 0, index, 0, @options[:filename], [])
      next
    end
    if lines.last && lines.last.comment? && line =~ /^(?:#{tab_str}){#{lines.last.tabs + 1}}(.*)$/
      lines.last.text << "\n" << $1
      next
    end
    line_tabs = line_tab_str.scan(tab_str).size
    raise SyntaxError.new(<<END.strip.gsub("\n", ' '), index) if tab_str * line_tabs != line_tab_str
nsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation,
the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}.
    lines << Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
  end
  lines
end

def to_tree

Raises:
  • (Sass::SyntaxError) - if there's an error in the document

Returns:
  • (Sass::Tree::Node) - The root of the parse tree.
def to_tree
  root = Tree::Node.new
  append_children(root, tree(tabulate(@template)).first, true)
  root.options = @options
  root
rescue SyntaxError => e; e.add_metadata(@options[:filename], @line)
end

def tree(arr, i = 0)

def tree(arr, i = 0)
  return [], i if arr[i].nil?
  base = arr[i].tabs
  nodes = []
  while (line = arr[i]) && line.tabs >= base
    if line.tabs > base
      if line.tabs > base + 1
        raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.", line.index)
      end
      nodes.last.children, i = tree(arr, i)
    else
      nodes << line
      i += 1
    end
  end
  return nodes, i
end

def validate_and_append_child(parent, child, line, root)

def validate_and_append_child(parent, child, line, root)
  unless root
    case child
    when Tree::MixinDefNode
      raise SyntaxError.new("Mixins may only be defined at the root of a document.", line.index)
    when Tree::ImportNode
      raise SyntaxError.new("Import directives may only be used at the root of a document.", line.index)
    end
  end
  case child
  when Array
    child.each {|c| validate_and_append_child(parent, c, line, root)}
  when Tree::Node
    parent << child
  end
end