From 550f458759077ee77b0f3deefd087ea6797e195e Mon Sep 17 00:00:00 2001 From: nex3 Date: Fri, 22 Dec 2006 01:52:45 +0000 Subject: [PATCH] Adds constants for Sass. git-svn-id: svn://hamptoncatlin.com/haml/trunk@238 7063305b-7217-0410-af8c-cdc13e5119b9 --- TODO | 6 ++-- lib/sass/engine.rb | 44 ++++++++++++++++++++++++------ test/sass/plugin_test.rb | 2 +- test/sass/results/constants.css | 2 ++ test/sass/templates/constants.sass | 10 +++++++ 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 test/sass/results/constants.css create mode 100644 test/sass/templates/constants.sass diff --git a/TODO b/TODO index 8a569737..b7377b95 100644 --- a/TODO +++ b/TODO @@ -3,9 +3,9 @@ Testing: Add Sass support for various utilities (benchmark, profile) Documentation: - ! Add Sass info to README - ! Comment Sass code + ! Document Sass module Features: - Sassy variables + Arithmetic with Sassy variables + Exceptions thrown by Sass code should have their own class Filters for Haml diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb index fe59a3d6..91e170ed 100644 --- a/lib/sass/engine.rb +++ b/lib/sass/engine.rb @@ -16,6 +16,9 @@ module Sass # The character that begins a CSS attribute. ATTRIBUTE_CHAR = ':'[0] + # The attribute that begins a constant. + CONSTANT_CHAR = '@'[0] + # The string that begins one-line comments. COMMENT_STRING = '//' @@ -32,9 +35,10 @@ module Sass #++ # def initialize(template, options={}) + @options = options @template = template.split("\n") @lines = [] - @options = options + @constants = {} end # Processes the template and returns the result as a string. @@ -45,7 +49,7 @@ module Sass index = 0 while @lines[index] child, index = build_tree(index) - root << child + root << child if child end root.to_s end @@ -78,11 +82,12 @@ module Sass line, tabs = @lines[index] index += 1 node = parse_line(line) + return nil, index unless node has_children = has_children?(index, tabs) while has_children child, index = build_tree(index) - node << child + node << child if child has_children = has_children?(index, tabs) end @@ -95,13 +100,34 @@ module Sass end def parse_line(line) - if line[0] == ATTRIBUTE_CHAR - name, *value = line.split(' ') - name = name[1..-1] - Tree::AttrNode.new(name, value) - else - Tree::RuleNode.new(line) + case line[0] + when ATTRIBUTE_CHAR + parse_attribute(line) + when CONSTANT_CHAR + parse_constant(line) + else + Tree::RuleNode.new(line) 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 diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb index 8f012a88..42eaf9ab 100644 --- a/test/sass/plugin_test.rb +++ b/test/sass/plugin_test.rb @@ -9,7 +9,7 @@ RAILS_ENV = 'testing' require 'sass/plugin' class SassPluginTest < Test::Unit::TestCase - @@templates = %w{ complex } + @@templates = %w{ complex constants } def setup Sass::Plugin.options[:template_location] = File.dirname(__FILE__) + '/templates' diff --git a/test/sass/results/constants.css b/test/sass/results/constants.css new file mode 100644 index 00000000..d86b05df --- /dev/null +++ b/test/sass/results/constants.css @@ -0,0 +1,2 @@ +#main { width: 500px; background-color: #000; color: #fff; } +#main #sidebar { background-color: #00ff98; } diff --git a/test/sass/templates/constants.sass b/test/sass/templates/constants.sass new file mode 100644 index 00000000..2b5c4412 --- /dev/null +++ b/test/sass/templates/constants.sass @@ -0,0 +1,10 @@ +@width = 500px +@color = #00ff98 +@main-text = #fff + +#main + :width @width + :background-color #000 + :color @main-text + #sidebar + :background-color @color