1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Created css2sass. Still lacking a lot of functionality, like figuring out nesting and dealing with comments and @import. The basics are there, though.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@476 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2007-04-01 10:03:12 +00:00
parent 2a5337bff0
commit a25e1fbfdc
3 changed files with 129 additions and 0 deletions

7
bin/css2sass Executable file
View file

@ -0,0 +1,7 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/haml'
require 'haml/exec'
opts = Haml::Exec::CSS2Sass.new(ARGV)
opts.parse!

View file

@ -253,5 +253,36 @@ END
output.write(::Haml::HTML.new(input).render) output.write(::Haml::HTML.new(input).render)
end end
end end
# A class encapsulating executable functionality
# specific to the css2sass executable.
class CSS2Sass < Generic # :nodoc:
def initialize(args)
super
require 'sass/css'
end
def set_opts(opts)
opts.banner = <<END
Usage: css2sass [options] (css file) (output file)
Description: Transforms a CSS file into corresponding Sass code.
Options:
END
super
end
def process_result
super
input = @options[:input]
output = @options[:output]
output.write(::Sass::CSS.new(input).render)
end
end
end end
end end

91
lib/sass/css.rb Normal file
View file

@ -0,0 +1,91 @@
require File.dirname(__FILE__) + '/../sass'
require 'sass/tree/node'
require 'strscan'
module Sass
# :stopdoc:
module Tree
class Node
def to_sass
result = ''
children.each do |child|
result << "#{child.to_sass(0)}\n"
end
result
end
end
class RuleNode
def to_sass(tabs)
str = "#{' ' * tabs}#{rule}\n"
children.each do |child|
str << "#{child.to_sass(tabs + 1)}\n"
end
str
end
end
class AttrNode
def to_sass(tabs)
"#{' ' * tabs}:#{name} #{value}"
end
end
end
# :startdoc:
# This class contains the functionality used in the +css2sass+ utility,
# namely converting CSS documents to Sass templates.
class CSS
# :stopdoc:
# The Regexp matching a CSS rule
RULE_RE = /\s*([^\{]+)\s*\{/
# The Regexp matching a CSS attribute
ATTR_RE = /\s*[^::\{\}]+\s*:\s*[^:;\{\}]+\s*;/
# :startdoc:
# Creates a new instance of Sass::CSS that will compile the given document
# to a Sass string when +render+ is called.
def initialize(template)
if template.is_a? IO
template = template.read
end
@template = StringScanner.new(template)
end
# Processes the document and returns the result as a string
# containing the CSS template.
def render
build_tree.to_sass
end
private
def build_tree
root = Tree::Node.new(nil)
while @template.scan(RULE_RE)
rule = Tree::RuleNode.new(@template[0][0...-1].strip, nil)
root << rule
while @template.scan(ATTR_RE)
attrs = @template[0][0...-1].split(':').map {|s| s.strip}
rule << Tree::AttrNode.new(attrs[0], attrs[1], nil)
end
if @template.scan(/\s*\}/).nil?
raise "Invalid CSS!"
end
end
root
end
end
end