Created
July 21, 2013 10:55
-
-
Save andreypopp/6048235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Convert SASS/SCSS to Stylus | |
require 'sass' | |
class ToStylus < Sass::Tree::Visitors::Base | |
def visit(node) | |
method = "visit_#{node_name node}" | |
if self.respond_to?(method, true) | |
self.send(method, node) {visit_children(node)} | |
else | |
raise "unhandled node: '#{node_name node}'" | |
end | |
end | |
def visit_children(node) | |
@indent += 1 | |
super(node) | |
@indent -= 1 | |
end | |
def render_arg(arg) | |
if arg.is_a? Array | |
var = arg[0] | |
default = arg[1] | |
if default | |
"#{arg[0].to_sass} = #{arg[1].to_sass}" | |
else | |
var.to_sass | |
end | |
else | |
arg.to_sass | |
end | |
end | |
def render_args(args) | |
args.map { |a| render_arg(a) }.join(', ') | |
end | |
def emit(line) | |
line = (' ' * @indent) + line | |
@lines.push line | |
end | |
def visit_if(node, isElse = false) | |
line = [] | |
line.push 'else' if isElse | |
line.push "if #{node.expr.to_sass}" if node.expr | |
emit line.join(' ') | |
visit_children(node) | |
visit_if(node.else, true) if node.else | |
end | |
def visit_return(node) | |
emit node.expr.to_sass | |
end | |
def visit_comment(node) | |
node.value.each { |s| emit s } | |
end | |
def visit_variable(node) | |
emit "$#{node.name} = #{node.expr.to_sass}" | |
end | |
def visit_mixindef(node) | |
emit "#{node.name}(#{render_args(node.args)})" | |
visit_children node | |
end | |
def visit_mixin(node) | |
emit "#{node.name}(#{render_args(node.args)})" | |
end | |
def visit_prop(node) | |
emit "#{node.name.join(' ')}: #{node.value.to_sass}" | |
end | |
def visit_function(node) | |
emit "#{node.name}(#{render_args(node.args)})" | |
visit_children node | |
end | |
def visit_rule(node) | |
emit node.parsed_rules.to_s | |
visit_children node | |
end | |
def visit_root(node) | |
@indent = -1 | |
@lines = [] | |
visit_children(node) | |
@lines.join("\n") | |
end | |
end | |
def main | |
options = Sass::Engine::DEFAULT_OPTIONS | |
engine = Sass::Engine.for_file(ARGV[0], options) | |
tree = engine.to_tree | |
stylus = ToStylus.visit(tree) | |
puts stylus | |
end | |
main() |
I'd also like to know if this works, and how ;)
This works with versions of Sass before 3.3.0, and only with the Sass syntax (not SCSS).
ruby converter.rb foo.sass
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work?