2006-12-04 08:40:23 +00:00
|
|
|
require 'sass/tree/node'
|
|
|
|
require 'sass/tree/value_node'
|
|
|
|
require 'sass/tree/rule_node'
|
2007-01-01 06:10:20 +00:00
|
|
|
require 'sass/constant'
|
2007-01-28 10:14:15 +00:00
|
|
|
require 'sass/error'
|
2006-11-28 19:43:58 +00:00
|
|
|
|
|
|
|
module Sass
|
2006-12-17 16:45:07 +00: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 19:43:58 +00:00
|
|
|
class Engine
|
2006-12-04 08:40:23 +00:00
|
|
|
# The character that begins a CSS attribute.
|
2007-01-28 10:14:15 +00:00
|
|
|
ATTRIBUTE_CHAR = ?:
|
2006-12-22 06:22:15 +00:00
|
|
|
|
|
|
|
# The character that designates that
|
|
|
|
# an attribute should be assigned to the result of constant arithmetic.
|
2007-01-28 10:14:15 +00:00
|
|
|
SCRIPT_CHAR = ?=
|
2006-12-22 01:52:45 +00:00
|
|
|
|
2006-12-04 08:40:23 +00:00
|
|
|
# The string that begins one-line comments.
|
|
|
|
COMMENT_STRING = '//'
|
|
|
|
|
2006-12-17 16:45:07 +00:00
|
|
|
# Creates a new instace of Sass::Engine that will compile the given
|
|
|
|
# template string when <tt>render</tt> is called.
|
2006-12-18 01:31:11 +00:00
|
|
|
# See README for available options.
|
2006-12-17 16:45:07 +00:00
|
|
|
#
|
|
|
|
#--
|
|
|
|
#
|
2007-02-02 06:15:28 +00:00
|
|
|
# TODO: Add current options to REFRENCE. Remember :filename!
|
2006-12-17 16:45:07 +00:00
|
|
|
#
|
|
|
|
# When adding options, remember to add information about them
|
2006-12-18 01:31:11 +00:00
|
|
|
# to README!
|
2006-12-17 16:45:07 +00:00
|
|
|
#++
|
|
|
|
#
|
2006-12-04 02:47:37 +00:00
|
|
|
def initialize(template, options={})
|
2007-02-06 01:58:23 +00:00
|
|
|
@options = {
|
|
|
|
:style => :compact
|
|
|
|
}.merge! options
|
2006-12-04 08:40:23 +00:00
|
|
|
@template = template.split("\n")
|
2006-12-19 05:22:19 +00:00
|
|
|
@lines = []
|
2006-12-22 01:52:45 +00:00
|
|
|
@constants = {}
|
2006-11-28 20:33:22 +00:00
|
|
|
end
|
2006-12-04 02:47:37 +00:00
|
|
|
|
2006-12-17 16:45:07 +00:00
|
|
|
# Processes the template and returns the result as a string.
|
2006-12-04 02:47:37 +00:00
|
|
|
def render
|
2007-01-31 06:38:23 +00:00
|
|
|
begin
|
|
|
|
split_lines
|
2006-12-19 05:22:19 +00:00
|
|
|
|
2007-02-06 01:58:23 +00:00
|
|
|
root = Tree::Node.new(@options[:style])
|
2007-01-31 06:38:23 +00:00
|
|
|
index = 0
|
|
|
|
while @lines[index]
|
|
|
|
child, index = build_tree(index)
|
|
|
|
child.line = index if child
|
|
|
|
root << child if child
|
|
|
|
end
|
|
|
|
@line = nil
|
2007-01-28 10:14:15 +00:00
|
|
|
|
2007-01-31 06:38:23 +00:00
|
|
|
root.to_s
|
|
|
|
rescue SyntaxError => err
|
2007-02-02 06:15:28 +00:00
|
|
|
err.add_backtrace_entry(@options[:filename])
|
2007-01-31 06:38:23 +00:00
|
|
|
raise err
|
|
|
|
end
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2007-02-01 06:59:08 +00:00
|
|
|
|
|
|
|
alias_method :to_css, :render
|
2006-12-04 08:40:23 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2006-12-19 05:22:19 +00:00
|
|
|
# Readies each line in the template for parsing,
|
|
|
|
# and computes the tabulation of the line.
|
|
|
|
def split_lines
|
2007-01-31 04:30:30 +00:00
|
|
|
old_tabs = 0
|
2007-01-28 10:14:15 +00:00
|
|
|
@template.each_with_index do |line, index|
|
|
|
|
@line = index + 1
|
2006-12-04 08:40:23 +00:00
|
|
|
|
2007-01-28 10:14:15 +00:00
|
|
|
# TODO: Allow comments appended to the end of lines,
|
|
|
|
# find some way to make url(http://www.google.com/) work
|
2006-12-19 05:22:19 +00:00
|
|
|
unless line[0..1] == COMMENT_STRING # unless line is a comment
|
|
|
|
tabs = count_tabs(line)
|
2006-11-28 19:43:58 +00:00
|
|
|
|
2006-12-19 05:22:19 +00:00
|
|
|
if tabs # if line isn't blank
|
2007-01-31 04:30:30 +00:00
|
|
|
if tabs - old_tabs > 1
|
2007-01-31 06:38:23 +00:00
|
|
|
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
|
2007-01-31 04:30:30 +00:00
|
|
|
end
|
2006-12-19 05:22:19 +00:00
|
|
|
@lines << [line.strip, tabs]
|
2007-01-31 04:30:30 +00:00
|
|
|
|
|
|
|
old_tabs = tabs
|
2006-12-19 05:22:19 +00:00
|
|
|
end
|
2006-11-28 19:43:58 +00:00
|
|
|
end
|
|
|
|
end
|
2007-01-28 10:14:15 +00:00
|
|
|
@line = nil
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
|
|
|
|
2006-12-19 05:22:19 +00:00
|
|
|
# Counts the tabulation of a line.
|
|
|
|
def count_tabs(line)
|
|
|
|
spaces = line.index(/[^ ]/)
|
2007-01-28 10:14:15 +00:00
|
|
|
if spaces
|
2007-01-31 04:30:30 +00:00
|
|
|
if spaces % 2 == 1 || line[spaces] == ?\t
|
2007-01-31 06:38:23 +00:00
|
|
|
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
|
2007-01-28 10:14:15 +00:00
|
|
|
end
|
|
|
|
spaces / 2
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2006-12-19 05:22:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_tree(index)
|
|
|
|
line, tabs = @lines[index]
|
|
|
|
index += 1
|
2007-02-02 06:15:28 +00:00
|
|
|
@line = index
|
2006-12-19 05:22:19 +00:00
|
|
|
node = parse_line(line)
|
2007-01-28 10:14:15 +00:00
|
|
|
|
|
|
|
# Node is nil if it's non-outputting, like a constant assignment
|
2006-12-22 01:52:45 +00:00
|
|
|
return nil, index unless node
|
2007-01-28 10:14:15 +00:00
|
|
|
|
2006-12-19 05:22:19 +00:00
|
|
|
has_children = has_children?(index, tabs)
|
2006-12-04 08:40:23 +00:00
|
|
|
|
2006-12-19 05:22:19 +00:00
|
|
|
while has_children
|
|
|
|
child, index = build_tree(index)
|
2007-02-05 00:41:05 +00:00
|
|
|
|
|
|
|
if child.nil?
|
|
|
|
raise SyntaxError.new("Constants may only be declared at the root of a document.", @line)
|
|
|
|
end
|
|
|
|
|
2007-02-02 06:15:28 +00:00
|
|
|
child.line = @line
|
2006-12-22 01:52:45 +00:00
|
|
|
node << child if child
|
2006-12-19 05:22:19 +00: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 08:40:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_line(line)
|
2006-12-22 01:52:45 +00:00
|
|
|
case line[0]
|
|
|
|
when ATTRIBUTE_CHAR
|
|
|
|
parse_attribute(line)
|
2007-01-28 10:14:15 +00:00
|
|
|
when Constant::CONSTANT_CHAR
|
2006-12-22 01:52:45 +00:00
|
|
|
parse_constant(line)
|
|
|
|
else
|
2007-02-06 01:58:23 +00:00
|
|
|
Tree::RuleNode.new(line, @options[:style])
|
2006-12-22 01:52:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_attribute(line)
|
2006-12-22 06:26:46 +00:00
|
|
|
name, value = line.split(' ', 2)
|
2007-01-28 10:14:15 +00:00
|
|
|
value = value.to_s
|
|
|
|
|
|
|
|
if name.nil? || name == ':' || name == ':='
|
|
|
|
raise SyntaxError.new("Invalid attribute: \"#{line}\"", @line)
|
|
|
|
end
|
|
|
|
|
2006-12-22 01:52:45 +00:00
|
|
|
name = name[1..-1]
|
|
|
|
|
2006-12-22 06:22:15 +00:00
|
|
|
if name[-1] == SCRIPT_CHAR
|
|
|
|
name.slice!(-1)
|
2007-01-28 10:28:19 +00:00
|
|
|
value = Sass::Constant.parse(value, @constants, @line).to_s
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2006-12-22 01:52:45 +00:00
|
|
|
|
2007-02-06 01:58:23 +00:00
|
|
|
Tree::AttrNode.new(name, value, @options[:style])
|
2006-12-22 01:52:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_constant(line)
|
2007-01-28 10:14:15 +00:00
|
|
|
name, value = line.scan(Sass::Constant::MATCH)[0]
|
|
|
|
unless name && value
|
2007-01-31 04:30:30 +00:00
|
|
|
raise SyntaxError.new("Invalid constant: \"#{line}\"", @line)
|
2007-01-28 10:14:15 +00:00
|
|
|
end
|
2007-01-28 10:28:19 +00:00
|
|
|
@constants[name] = Sass::Constant.parse(value, @constants, @line)
|
2006-12-22 01:52:45 +00:00
|
|
|
nil
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2006-11-28 19:43:58 +00:00
|
|
|
end
|
|
|
|
end
|