2006-12-04 03:40:23 -05:00
|
|
|
require 'sass/tree/node'
|
|
|
|
require 'sass/tree/value_node'
|
|
|
|
require 'sass/tree/rule_node'
|
2007-01-01 01:10:20 -05:00
|
|
|
require 'sass/constant'
|
2007-01-28 05:14:15 -05:00
|
|
|
require 'sass/error'
|
2006-11-28 14:43:58 -05:00
|
|
|
|
|
|
|
module Sass
|
2006-12-17 11:45:07 -05:00
|
|
|
# This is the class where all the parsing and processing of the Sass
|
|
|
|
# template is done. It can be directly used by the user by creating a
|
|
|
|
# new instance and calling <tt>render</tt> to render the template. For example:
|
|
|
|
#
|
|
|
|
# template = File.load('stylesheets/sassy.sass')
|
|
|
|
# sass_engine = Sass::Engine.new(template)
|
|
|
|
# output = sass_engine.render
|
|
|
|
# puts output
|
2006-11-28 14:43:58 -05:00
|
|
|
class Engine
|
2006-12-04 03:40:23 -05:00
|
|
|
# The character that begins a CSS attribute.
|
2007-01-28 05:14:15 -05:00
|
|
|
ATTRIBUTE_CHAR = ?:
|
2006-12-22 01:22:15 -05:00
|
|
|
|
|
|
|
# The character that designates that
|
|
|
|
# an attribute should be assigned to the result of constant arithmetic.
|
2007-01-28 05:14:15 -05:00
|
|
|
SCRIPT_CHAR = ?=
|
2006-12-21 20:52:45 -05:00
|
|
|
|
2006-12-04 03:40:23 -05:00
|
|
|
# The string that begins one-line comments.
|
|
|
|
COMMENT_STRING = '//'
|
|
|
|
|
2006-12-17 11:45:07 -05:00
|
|
|
# Creates a new instace of Sass::Engine that will compile the given
|
|
|
|
# template string when <tt>render</tt> is called.
|
2006-12-17 20:31:11 -05:00
|
|
|
# See README for available options.
|
2006-12-17 11:45:07 -05:00
|
|
|
#
|
|
|
|
#--
|
|
|
|
#
|
|
|
|
# TODO: Add current options to REFRENCE.
|
|
|
|
#
|
|
|
|
# When adding options, remember to add information about them
|
2006-12-17 20:31:11 -05:00
|
|
|
# to README!
|
2006-12-17 11:45:07 -05:00
|
|
|
#++
|
|
|
|
#
|
2006-12-03 21:47:37 -05:00
|
|
|
def initialize(template, options={})
|
2006-12-21 20:52:45 -05:00
|
|
|
@options = options
|
2006-12-04 03:40:23 -05:00
|
|
|
@template = template.split("\n")
|
2006-12-19 00:22:19 -05:00
|
|
|
@lines = []
|
2006-12-21 20:52:45 -05:00
|
|
|
@constants = {}
|
2006-11-28 15:33:22 -05:00
|
|
|
end
|
2006-12-03 21:47:37 -05:00
|
|
|
|
2006-12-17 11:45:07 -05:00
|
|
|
# Processes the template and returns the result as a string.
|
2006-12-03 21:47:37 -05:00
|
|
|
def render
|
2006-12-19 00:22:19 -05:00
|
|
|
split_lines
|
|
|
|
|
2006-12-04 11:49:23 -05:00
|
|
|
root = Tree::Node.new
|
2006-12-19 00:22:19 -05:00
|
|
|
index = 0
|
|
|
|
while @lines[index]
|
|
|
|
child, index = build_tree(index)
|
2007-01-28 05:14:15 -05:00
|
|
|
child.line = index if child
|
2006-12-21 20:52:45 -05:00
|
|
|
root << child if child
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-01-28 05:14:15 -05:00
|
|
|
@line = nil
|
|
|
|
|
2006-12-04 11:49:23 -05:00
|
|
|
root.to_s
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
# Readies each line in the template for parsing,
|
|
|
|
# and computes the tabulation of the line.
|
|
|
|
def split_lines
|
2007-01-28 05:14:15 -05:00
|
|
|
@template.each_with_index do |line, index|
|
|
|
|
@line = index + 1
|
2006-12-04 03:40:23 -05:00
|
|
|
|
2007-01-28 05:14:15 -05:00
|
|
|
# TODO: Allow comments appended to the end of lines,
|
|
|
|
# find some way to make url(http://www.google.com/) work
|
2006-12-19 00:22:19 -05:00
|
|
|
unless line[0..1] == COMMENT_STRING # unless line is a comment
|
|
|
|
tabs = count_tabs(line)
|
2006-11-28 14:43:58 -05:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
if tabs # if line isn't blank
|
|
|
|
@lines << [line.strip, tabs]
|
|
|
|
end
|
2006-11-28 14:43:58 -05:00
|
|
|
end
|
|
|
|
end
|
2007-01-28 05:14:15 -05:00
|
|
|
@line = nil
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
# Counts the tabulation of a line.
|
|
|
|
def count_tabs(line)
|
|
|
|
spaces = line.index(/[^ ]/)
|
2007-01-28 05:14:15 -05:00
|
|
|
if spaces
|
|
|
|
if line[spaces] == ?\t
|
|
|
|
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.")
|
|
|
|
end
|
|
|
|
spaces / 2
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_tree(index)
|
|
|
|
line, tabs = @lines[index]
|
|
|
|
index += 1
|
2007-01-28 05:14:15 -05:00
|
|
|
@line = index + 1
|
2006-12-19 00:22:19 -05:00
|
|
|
node = parse_line(line)
|
2007-01-28 05:14:15 -05:00
|
|
|
|
|
|
|
# Node is nil if it's non-outputting, like a constant assignment
|
2006-12-21 20:52:45 -05:00
|
|
|
return nil, index unless node
|
2007-01-28 05:14:15 -05:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
has_children = has_children?(index, tabs)
|
2006-12-04 03:40:23 -05:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
while has_children
|
|
|
|
child, index = build_tree(index)
|
2007-01-28 05:14:15 -05:00
|
|
|
child.line = index
|
2006-12-21 20:52:45 -05:00
|
|
|
node << child if child
|
2006-12-19 00:22:19 -05:00
|
|
|
has_children = has_children?(index, tabs)
|
|
|
|
end
|
|
|
|
|
|
|
|
return node, index
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_children?(index, tabs)
|
|
|
|
next_line = @lines[index]
|
|
|
|
next_line && next_line[1] > tabs
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_line(line)
|
2006-12-21 20:52:45 -05:00
|
|
|
case line[0]
|
|
|
|
when ATTRIBUTE_CHAR
|
|
|
|
parse_attribute(line)
|
2007-01-28 05:14:15 -05:00
|
|
|
when Constant::CONSTANT_CHAR
|
2006-12-21 20:52:45 -05:00
|
|
|
parse_constant(line)
|
|
|
|
else
|
|
|
|
Tree::RuleNode.new(line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_attribute(line)
|
2006-12-22 01:26:46 -05:00
|
|
|
name, value = line.split(' ', 2)
|
2007-01-28 05:14:15 -05:00
|
|
|
value = value.to_s
|
|
|
|
|
|
|
|
if name.nil? || name == ':' || name == ':='
|
|
|
|
raise SyntaxError.new("Invalid attribute: \"#{line}\"", @line)
|
|
|
|
end
|
|
|
|
|
2006-12-21 20:52:45 -05:00
|
|
|
name = name[1..-1]
|
|
|
|
|
2006-12-22 01:22:15 -05:00
|
|
|
if name[-1] == SCRIPT_CHAR
|
|
|
|
name.slice!(-1)
|
2007-01-28 05:28:19 -05:00
|
|
|
value = Sass::Constant.parse(value, @constants, @line).to_s
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2006-12-21 20:52:45 -05:00
|
|
|
|
|
|
|
Tree::AttrNode.new(name, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_constant(line)
|
2007-01-28 05:14:15 -05:00
|
|
|
name, value = line.scan(Sass::Constant::MATCH)[0]
|
|
|
|
unless name && value
|
|
|
|
raise SyntaxError.new("Invalid constant: #{line}", @line)
|
|
|
|
end
|
2007-01-28 05:28:19 -05:00
|
|
|
@constants[name] = Sass::Constant.parse(value, @constants, @line)
|
2006-12-21 20:52:45 -05:00
|
|
|
nil
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2006-11-28 14:43:58 -05:00
|
|
|
end
|
|
|
|
end
|