Adds constants for Sass.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@238 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-12-22 01:52:45 +00:00
parent afc413491c
commit 550f458759
5 changed files with 51 additions and 13 deletions

6
TODO
View File

@ -3,9 +3,9 @@ Testing:
Add Sass support for various utilities (benchmark, profile) Add Sass support for various utilities (benchmark, profile)
Documentation: Documentation:
! Add Sass info to README ! Document Sass module
! Comment Sass code
Features: Features:
Sassy variables Arithmetic with Sassy variables
Exceptions thrown by Sass code should have their own class
Filters for Haml Filters for Haml

View File

@ -16,6 +16,9 @@ module Sass
# The character that begins a CSS attribute. # The character that begins a CSS attribute.
ATTRIBUTE_CHAR = ':'[0] ATTRIBUTE_CHAR = ':'[0]
# The attribute that begins a constant.
CONSTANT_CHAR = '@'[0]
# The string that begins one-line comments. # The string that begins one-line comments.
COMMENT_STRING = '//' COMMENT_STRING = '//'
@ -32,9 +35,10 @@ module Sass
#++ #++
# #
def initialize(template, options={}) def initialize(template, options={})
@options = options
@template = template.split("\n") @template = template.split("\n")
@lines = [] @lines = []
@options = options @constants = {}
end end
# Processes the template and returns the result as a string. # Processes the template and returns the result as a string.
@ -45,7 +49,7 @@ module Sass
index = 0 index = 0
while @lines[index] while @lines[index]
child, index = build_tree(index) child, index = build_tree(index)
root << child root << child if child
end end
root.to_s root.to_s
end end
@ -78,11 +82,12 @@ module Sass
line, tabs = @lines[index] line, tabs = @lines[index]
index += 1 index += 1
node = parse_line(line) node = parse_line(line)
return nil, index unless node
has_children = has_children?(index, tabs) has_children = has_children?(index, tabs)
while has_children while has_children
child, index = build_tree(index) child, index = build_tree(index)
node << child node << child if child
has_children = has_children?(index, tabs) has_children = has_children?(index, tabs)
end end
@ -95,13 +100,34 @@ module Sass
end end
def parse_line(line) def parse_line(line)
if line[0] == ATTRIBUTE_CHAR case line[0]
name, *value = line.split(' ') when ATTRIBUTE_CHAR
name = name[1..-1] parse_attribute(line)
Tree::AttrNode.new(name, value) when CONSTANT_CHAR
else parse_constant(line)
Tree::RuleNode.new(line) else
Tree::RuleNode.new(line)
end end
end end
def parse_attribute(line)
name, *value = line.split(' ')
name = name[1..-1]
if value.size == 1 and value[0][0] == CONSTANT_CHAR
const_name = value[0][1..-1]
value = @constants[const_name]
raise "Constant \"#{const_name}\" is undefined." unless value
end
Tree::AttrNode.new(name, value)
end
def parse_constant(line)
name, value = line.scan(/^#{Regexp.escape(CONSTANT_CHAR.chr)}([^\s=]+)\s*=\s*(.+)/)[0]
raise "Invalid constant assignment:\n#{line}" unless name && value
@constants[name] = value
nil
end
end end
end end

View File

@ -9,7 +9,7 @@ RAILS_ENV = 'testing'
require 'sass/plugin' require 'sass/plugin'
class SassPluginTest < Test::Unit::TestCase class SassPluginTest < Test::Unit::TestCase
@@templates = %w{ complex } @@templates = %w{ complex constants }
def setup def setup
Sass::Plugin.options[:template_location] = File.dirname(__FILE__) + '/templates' Sass::Plugin.options[:template_location] = File.dirname(__FILE__) + '/templates'

View File

@ -0,0 +1,2 @@
#main { width: 500px; background-color: #000; color: #fff; }
#main #sidebar { background-color: #00ff98; }

View File

@ -0,0 +1,10 @@
@width = 500px
@color = #00ff98
@main-text = #fff
#main
:width @width
:background-color #000
:color @main-text
#sidebar
:background-color @color