2010-01-22 19:41:08 +00:00
|
|
|
require 'sass'
|
2010-01-21 19:54:47 +00:00
|
|
|
module Sass
|
|
|
|
module Tree
|
2010-01-23 09:06:54 +00:00
|
|
|
class Node
|
|
|
|
private
|
|
|
|
def tab(s, indent_level = 1)
|
|
|
|
s.split("\n").join("\n" + " " * indent_level)
|
|
|
|
end
|
|
|
|
def children_to_sass
|
|
|
|
clean_children = children.inject([[],true]) do |m,c|
|
|
|
|
if c.respond_to?(:doc) && !c.doc.nil?
|
|
|
|
[m.first, c.doc]
|
|
|
|
else
|
|
|
|
if m.last
|
|
|
|
[m.first + [c], true]
|
|
|
|
else
|
|
|
|
m
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
raise clean_children.first.inspect if clean_children.first.detect{|c| TrueClass === c}
|
|
|
|
tab(clean_children.first.
|
|
|
|
map{|c| c.to_sass}.join("\n"))
|
|
|
|
end
|
|
|
|
end
|
2010-01-21 19:54:47 +00:00
|
|
|
class VariableNode < Node
|
|
|
|
attr_accessor :name unless method_defined? :name
|
|
|
|
end
|
2010-01-23 09:06:54 +00:00
|
|
|
class IfNode < Node
|
|
|
|
def to_sass
|
|
|
|
sass_str = %Q{@if #{@expr.inspect unless @expr.nil?}
|
|
|
|
- #{tab children_to_sass}
|
|
|
|
-}.gsub(/^\s+-/,'')
|
|
|
|
if @else
|
|
|
|
sass_str << %Q{@else
|
|
|
|
- #{tab @else.to_sass}
|
|
|
|
-}.gsub(/^\s+-/,'')
|
|
|
|
end
|
|
|
|
sass_str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class DebugNode < Node
|
|
|
|
def to_sass
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
class MixinNode < Node
|
|
|
|
attr_accessor :name unless method_defined? :name
|
|
|
|
attr_accessor :args unless method_defined? :args
|
|
|
|
def to_sass
|
|
|
|
sass_str = "+#{name}"
|
|
|
|
if args.any?
|
|
|
|
sass_str << "(#{args.map{|a| a.inspect}.join(", ")})"
|
|
|
|
end
|
|
|
|
sass_str
|
|
|
|
end
|
|
|
|
end
|
2010-01-21 19:54:47 +00:00
|
|
|
class MixinDefNode < Node
|
|
|
|
attr_accessor :name unless method_defined? :name
|
|
|
|
attr_accessor :args unless method_defined? :args
|
2010-01-22 19:40:56 +00:00
|
|
|
attr_accessor :comment unless method_defined? :comment
|
2010-01-23 09:06:54 +00:00
|
|
|
|
|
|
|
def to_sass
|
|
|
|
"#{sass_signature}\n #{children_to_sass}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def sass_signature(mode = :definition)
|
|
|
|
prefix = case mode
|
|
|
|
when :definition
|
|
|
|
"="
|
|
|
|
when :include
|
|
|
|
"+"
|
|
|
|
end
|
|
|
|
"#{prefix}#{name}#{arglist_to_sass}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def arglist_to_sass
|
|
|
|
if args.any?
|
|
|
|
"(#{args.map{|a| arg_to_sass(a)}.join(", ")})"
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def arg_to_sass(arg)
|
|
|
|
name, default_value = arg
|
|
|
|
sass_str = "#{name.inspect}"
|
|
|
|
if default_value
|
|
|
|
sass_str << " = "
|
|
|
|
sass_str << default_value.inspect
|
|
|
|
end
|
|
|
|
sass_str
|
|
|
|
end
|
2010-01-21 19:54:47 +00:00
|
|
|
end
|
2010-01-22 19:40:56 +00:00
|
|
|
class ImportNode < RootNode
|
2010-01-21 19:54:47 +00:00
|
|
|
attr_accessor :imported_filename unless method_defined? :imported_filename
|
|
|
|
end
|
2010-01-22 19:40:56 +00:00
|
|
|
class CommentNode < Node
|
|
|
|
def docstring
|
|
|
|
([value] + lines).join("\n")
|
|
|
|
end
|
2010-01-23 09:06:54 +00:00
|
|
|
def doc
|
|
|
|
if value == "@doc off"
|
|
|
|
false
|
|
|
|
elsif value == "@doc on"
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2010-01-22 19:40:56 +00:00
|
|
|
end
|
2010-01-21 19:54:47 +00:00
|
|
|
end
|
|
|
|
end
|