2006-12-04 03:40:23 -05:00
|
|
|
require 'sass/tree/node'
|
|
|
|
require 'sass/tree/value_node'
|
|
|
|
require 'sass/tree/rule_node'
|
2007-03-17 19:34:26 -04:00
|
|
|
require 'sass/tree/comment_node'
|
|
|
|
require 'sass/tree/attr_node'
|
2007-08-12 06:54:35 -04:00
|
|
|
require 'sass/tree/directive_node'
|
2007-01-01 01:10:20 -05:00
|
|
|
require 'sass/constant'
|
2007-01-28 05:14:15 -05:00
|
|
|
require 'sass/error'
|
2007-03-25 04:20:33 -04:00
|
|
|
require 'haml/util'
|
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 = ?:
|
2007-05-03 04:46:02 -04:00
|
|
|
|
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 = ?=
|
2007-03-17 16:48:29 -04:00
|
|
|
|
|
|
|
# The character that designates the beginning of a comment,
|
|
|
|
# either Sass or CSS.
|
|
|
|
COMMENT_CHAR = ?/
|
|
|
|
|
2007-03-17 19:34:26 -04:00
|
|
|
# The character that follows the general COMMENT_CHAR and designates a Sass comment,
|
2007-03-17 16:48:29 -04:00
|
|
|
# which is not output as a CSS comment.
|
|
|
|
SASS_COMMENT_CHAR = ?/
|
2007-02-28 11:35:37 -05:00
|
|
|
|
2007-03-17 19:34:26 -04:00
|
|
|
# The character that follows the general COMMENT_CHAR and designates a CSS comment,
|
|
|
|
# which is embedded in the CSS document.
|
|
|
|
CSS_COMMENT_CHAR = ?*
|
2007-03-18 17:11:32 -04:00
|
|
|
|
|
|
|
# The character used to denote a compiler directive.
|
|
|
|
DIRECTIVE_CHAR = ?@
|
2007-12-11 05:36:40 -05:00
|
|
|
|
|
|
|
# Designates a non-parsed rule.
|
|
|
|
ESCAPE_CHAR = ?\\
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-06-28 04:43:09 -04:00
|
|
|
# The regex that matches and extracts data from
|
|
|
|
# attributes of the form <tt>:name attr</tt>.
|
|
|
|
ATTRIBUTE = /^:([^\s=:]+)\s*(=?)(?:\s+|$)(.*)/
|
2007-05-03 04:46:02 -04:00
|
|
|
|
|
|
|
# The regex that matches attributes of the form <tt>name: attr</tt>.
|
2007-06-28 04:43:09 -04:00
|
|
|
ATTRIBUTE_ALTERNATE_MATCHER = /^[^\s:]+\s*[=:](\s|$)/
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-06-28 04:43:09 -04:00
|
|
|
# The regex that matches and extracts data from
|
|
|
|
# attributes of the form <tt>name: attr</tt>.
|
|
|
|
ATTRIBUTE_ALTERNATE = /^([^\s=:]+)(\s*=|:)(?:\s+|$)(.*)/
|
2007-05-03 04:46:02 -04:00
|
|
|
|
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
|
|
|
#
|
|
|
|
#--
|
|
|
|
#
|
2007-02-02 01:15:28 -05:00
|
|
|
# TODO: Add current options to REFRENCE. Remember :filename!
|
2006-12-17 11:45:07 -05:00
|
|
|
#
|
|
|
|
# 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={})
|
2007-02-05 20:58:23 -05:00
|
|
|
@options = {
|
2007-03-18 17:11:32 -04:00
|
|
|
:style => :nested,
|
|
|
|
:load_paths => ['.']
|
2007-02-05 20:58:23 -05:00
|
|
|
}.merge! options
|
2008-01-31 01:21:24 -05:00
|
|
|
@template = template.split(/\n?\r|\r?\n/)
|
2006-12-19 00:22:19 -05:00
|
|
|
@lines = []
|
2007-11-23 09:49:54 -05:00
|
|
|
@constants = {"important" => "!important"}
|
2006-11-28 15:33:22 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04: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
|
2007-01-31 01:38:23 -05:00
|
|
|
begin
|
2007-03-18 17:11:32 -04:00
|
|
|
render_to_tree.to_s
|
2007-01-31 01:38:23 -05:00
|
|
|
rescue SyntaxError => err
|
2007-03-25 00:57:03 -04:00
|
|
|
unless err.sass_filename
|
|
|
|
err.add_backtrace_entry(@options[:filename])
|
|
|
|
end
|
2007-01-31 01:38:23 -05:00
|
|
|
raise err
|
|
|
|
end
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-02-01 01:59:08 -05:00
|
|
|
|
|
|
|
alias_method :to_css, :render
|
2007-03-18 17:11:32 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def constants
|
|
|
|
@constants
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_to_tree
|
|
|
|
split_lines
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-03-18 17:11:32 -04:00
|
|
|
root = Tree::Node.new(@options[:style])
|
|
|
|
index = 0
|
|
|
|
while @lines[index]
|
|
|
|
child, index = build_tree(index)
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-03-18 17:11:32 -04:00
|
|
|
if child.is_a? Tree::Node
|
2007-05-03 04:46:02 -04:00
|
|
|
child.line = index
|
2007-03-18 17:11:32 -04:00
|
|
|
root << child
|
|
|
|
elsif child.is_a? Array
|
|
|
|
child.each do |c|
|
|
|
|
root << c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@line = nil
|
|
|
|
|
|
|
|
root
|
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-04 03:40:23 -05:00
|
|
|
private
|
2007-05-03 04:46:02 -04:00
|
|
|
|
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-03-18 17:11:32 -04:00
|
|
|
@line = 0
|
2007-01-30 23:30:30 -05:00
|
|
|
old_tabs = 0
|
2007-01-28 05:14:15 -05:00
|
|
|
@template.each_with_index do |line, index|
|
2007-03-18 17:11:32 -04:00
|
|
|
@line += 1
|
|
|
|
|
2007-03-17 16:48:29 -04:00
|
|
|
tabs = count_tabs(line)
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-03-18 17:11:32 -04:00
|
|
|
if line[0] == COMMENT_CHAR && line[1] == SASS_COMMENT_CHAR && tabs == 0
|
2007-03-17 16:48:29 -04:00
|
|
|
tabs = old_tabs
|
|
|
|
end
|
2007-01-30 23:30:30 -05:00
|
|
|
|
2007-03-17 16:48:29 -04:00
|
|
|
if tabs # if line isn't blank
|
|
|
|
if tabs - old_tabs > 1
|
2007-05-03 04:46:02 -04:00
|
|
|
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-03-17 16:48:29 -04:00
|
|
|
@lines << [line.strip, tabs]
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-03-17 16:48:29 -04:00
|
|
|
old_tabs = tabs
|
2007-03-17 19:13:04 -04:00
|
|
|
else
|
|
|
|
@lines << ['//', old_tabs]
|
2006-11-28 14:43:58 -05:00
|
|
|
end
|
|
|
|
end
|
2007-03-17 16:48:29 -04:00
|
|
|
|
2007-01-28 05:14:15 -05:00
|
|
|
@line = nil
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
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
|
2007-01-30 23:30:30 -05:00
|
|
|
if spaces % 2 == 1 || line[spaces] == ?\t
|
2007-03-30 02:49:03 -04:00
|
|
|
# Make sure a line with just tabs isn't an error
|
|
|
|
return nil if line.strip.empty?
|
|
|
|
|
2007-05-03 04:46:02 -04:00
|
|
|
raise SyntaxError.new("Illegal Indentation: Only two space characters are allowed as tabulation.", @line)
|
2007-01-28 05:14:15 -05:00
|
|
|
end
|
|
|
|
spaces / 2
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
def build_tree(index)
|
|
|
|
line, tabs = @lines[index]
|
|
|
|
index += 1
|
2007-02-02 01:15:28 -05:00
|
|
|
@line = index
|
2006-12-19 00:22:19 -05:00
|
|
|
node = parse_line(line)
|
2007-01-28 05:14:15 -05:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
has_children = has_children?(index, tabs)
|
2007-03-18 00:20:45 -04:00
|
|
|
|
|
|
|
# Node is a symbol if it's non-outputting, like a constant assignment
|
|
|
|
unless node.is_a? Tree::Node
|
2007-03-18 17:11:32 -04:00
|
|
|
if has_children
|
|
|
|
if node == :constant
|
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath constants.", @line)
|
|
|
|
elsif node.is_a? Array
|
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.", @line)
|
|
|
|
end
|
2007-03-18 00:20:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
return node, index
|
|
|
|
end
|
2007-03-18 00:52:16 -04:00
|
|
|
|
|
|
|
if node.is_a? Tree::CommentNode
|
|
|
|
while has_children
|
|
|
|
line, index = raw_next_line(index)
|
|
|
|
node << line
|
|
|
|
|
|
|
|
has_children = has_children?(index, tabs)
|
2007-02-04 19:41:05 -05:00
|
|
|
end
|
2007-03-18 00:52:16 -04:00
|
|
|
else
|
|
|
|
while has_children
|
|
|
|
child, index = build_tree(index)
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-03-18 00:52:16 -04:00
|
|
|
if child == :constant
|
|
|
|
raise SyntaxError.new("Constants may only be declared at the root of a document.", @line)
|
2007-03-18 17:11:32 -04:00
|
|
|
elsif child.is_a? Array
|
2007-03-25 04:20:33 -04:00
|
|
|
raise SyntaxError.new("Import directives may only be used at the root of a document.", @line)
|
2007-03-18 00:52:16 -04:00
|
|
|
elsif child.is_a? Tree::Node
|
|
|
|
child.line = @line
|
|
|
|
node << child
|
|
|
|
end
|
2007-02-04 19:41:05 -05:00
|
|
|
|
2007-03-18 00:52:16 -04:00
|
|
|
has_children = has_children?(index, tabs)
|
|
|
|
end
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
return node, index
|
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-19 00:22:19 -05:00
|
|
|
def has_children?(index, tabs)
|
2007-08-16 14:28:57 -04:00
|
|
|
next_line = ['//', 0]
|
|
|
|
while !next_line.nil? && next_line[0] == '//' && next_line[1] = 0
|
|
|
|
next_line = @lines[index]
|
|
|
|
index += 1
|
|
|
|
end
|
2006-12-19 00:22:19 -05:00
|
|
|
next_line && next_line[1] > tabs
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-03-18 00:52:16 -04:00
|
|
|
|
|
|
|
def raw_next_line(index)
|
|
|
|
[@lines[index][0], index + 1]
|
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-04 03:40:23 -05:00
|
|
|
def parse_line(line)
|
2007-06-28 04:43:09 -04:00
|
|
|
case line[0]
|
|
|
|
when ATTRIBUTE_CHAR
|
2007-05-03 04:46:02 -04:00
|
|
|
parse_attribute(line, ATTRIBUTE)
|
2007-06-28 04:43:09 -04:00
|
|
|
when Constant::CONSTANT_CHAR
|
|
|
|
parse_constant(line)
|
|
|
|
when COMMENT_CHAR
|
|
|
|
parse_comment(line)
|
|
|
|
when DIRECTIVE_CHAR
|
|
|
|
parse_directive(line)
|
2007-12-11 05:36:40 -05:00
|
|
|
when ESCAPE_CHAR
|
|
|
|
Tree::RuleNode.new(line[1..-1], @options[:style])
|
2007-03-17 16:48:29 -04:00
|
|
|
else
|
2007-06-28 04:43:09 -04:00
|
|
|
if line =~ ATTRIBUTE_ALTERNATE_MATCHER
|
|
|
|
parse_attribute(line, ATTRIBUTE_ALTERNATE)
|
2007-05-03 04:46:02 -04:00
|
|
|
else
|
|
|
|
Tree::RuleNode.new(line, @options[:style])
|
|
|
|
end
|
2006-12-21 20:52:45 -05:00
|
|
|
end
|
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
|
|
|
def parse_attribute(line, attribute_regx)
|
2007-08-11 19:13:43 -04:00
|
|
|
if @options[:attribute_syntax] == :normal &&
|
|
|
|
attribute_regx == ATTRIBUTE_ALTERNATE
|
|
|
|
raise SyntaxError.new("Illegal attribute syntax: can't use alternate syntax when :attribute_syntax => :normal is set.")
|
|
|
|
elsif @options[:attribute_syntax] == :alternate &&
|
|
|
|
attribute_regx == ATTRIBUTE
|
|
|
|
raise SyntaxError.new("Illegal attribute syntax: can't use normal syntax when :attribute_syntax => :alternate is set.")
|
|
|
|
end
|
|
|
|
|
2007-05-03 04:46:02 -04:00
|
|
|
name, eq, value = line.scan(attribute_regx)[0]
|
2007-01-28 05:14:15 -05:00
|
|
|
|
2007-02-28 11:35:37 -05:00
|
|
|
if name.nil? || value.nil?
|
2007-01-28 05:14:15 -05:00
|
|
|
raise SyntaxError.new("Invalid attribute: \"#{line}\"", @line)
|
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-06-28 04:43:09 -04:00
|
|
|
if eq.strip[0] == SCRIPT_CHAR
|
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
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2007-02-05 20:58:23 -05:00
|
|
|
Tree::AttrNode.new(name, value, @options[:style])
|
2006-12-21 20:52:45 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2006-12-21 20:52:45 -05:00
|
|
|
def parse_constant(line)
|
2007-01-28 05:14:15 -05:00
|
|
|
name, value = line.scan(Sass::Constant::MATCH)[0]
|
|
|
|
unless name && value
|
2007-01-30 23:30:30 -05:00
|
|
|
raise SyntaxError.new("Invalid constant: \"#{line}\"", @line)
|
2007-01-28 05:14:15 -05:00
|
|
|
end
|
2007-01-28 05:28:19 -05:00
|
|
|
@constants[name] = Sass::Constant.parse(value, @constants, @line)
|
2007-03-17 16:48:29 -04:00
|
|
|
:constant
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_comment(line)
|
|
|
|
if line[1] == SASS_COMMENT_CHAR
|
|
|
|
:comment
|
2007-03-17 19:34:26 -04:00
|
|
|
elsif line[1] == CSS_COMMENT_CHAR
|
|
|
|
Tree::CommentNode.new(line, @options[:style])
|
2007-03-17 16:48:29 -04:00
|
|
|
else
|
|
|
|
Tree::RuleNode.new(line, @options[:style])
|
|
|
|
end
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-03-18 17:11:32 -04:00
|
|
|
|
|
|
|
def parse_directive(line)
|
|
|
|
directive, value = line[1..-1].split(/\s+/, 2)
|
|
|
|
|
|
|
|
case directive
|
|
|
|
when "import"
|
|
|
|
import(value)
|
|
|
|
else
|
2007-08-12 06:54:35 -04:00
|
|
|
Tree::DirectiveNode.new(line, @options[:style])
|
2007-03-18 17:11:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import(files)
|
|
|
|
nodes = []
|
|
|
|
|
|
|
|
files.split(/,\s*/).each do |filename|
|
|
|
|
engine = nil
|
2007-09-01 21:48:25 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
filename = self.class.find_file_to_import(filename, @options[:load_paths])
|
|
|
|
rescue Exception => e
|
|
|
|
raise SyntaxError.new(e.message, @line)
|
|
|
|
end
|
|
|
|
|
2007-03-25 00:34:55 -04:00
|
|
|
if filename =~ /\.css$/
|
2007-11-17 19:36:39 -05:00
|
|
|
nodes << Tree::ValueNode.new("@import url(#{filename});", @options[:style])
|
2007-03-25 00:34:55 -04:00
|
|
|
else
|
|
|
|
File.open(filename) do |file|
|
2007-03-25 00:57:03 -04:00
|
|
|
new_options = @options.dup
|
|
|
|
new_options[:filename] = filename
|
2007-03-25 00:34:55 -04:00
|
|
|
engine = Sass::Engine.new(file.read, @options)
|
|
|
|
end
|
2007-03-18 17:11:32 -04:00
|
|
|
|
2007-03-25 04:20:33 -04:00
|
|
|
engine.constants.merge! @constants
|
|
|
|
|
2007-03-25 00:57:03 -04:00
|
|
|
begin
|
|
|
|
root = engine.render_to_tree
|
|
|
|
rescue Sass::SyntaxError => err
|
|
|
|
err.add_backtrace_entry(filename)
|
|
|
|
raise err
|
|
|
|
end
|
|
|
|
root.children.each do |child|
|
|
|
|
child.filename = filename
|
|
|
|
nodes << child
|
|
|
|
end
|
2007-03-25 04:20:33 -04:00
|
|
|
@constants = engine.constants
|
2007-03-25 00:34:55 -04:00
|
|
|
end
|
2007-03-18 17:11:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
nodes
|
|
|
|
end
|
|
|
|
|
2007-09-01 21:48:25 -04:00
|
|
|
def self.find_file_to_import(filename, load_paths)
|
2007-03-25 04:20:33 -04:00
|
|
|
was_sass = false
|
|
|
|
original_filename = filename
|
2007-03-18 17:11:32 -04:00
|
|
|
|
2007-03-25 04:20:33 -04:00
|
|
|
if filename[-5..-1] == ".sass"
|
|
|
|
filename = filename[0...-5]
|
|
|
|
was_sass = true
|
|
|
|
elsif filename[-4..-1] == ".css"
|
|
|
|
return filename
|
|
|
|
end
|
2007-03-18 17:11:32 -04:00
|
|
|
|
2007-09-01 21:48:25 -04:00
|
|
|
new_filename = find_full_path("#{filename}.sass", load_paths)
|
2007-03-18 17:11:32 -04:00
|
|
|
|
2007-03-25 04:20:33 -04:00
|
|
|
if new_filename.nil?
|
|
|
|
if was_sass
|
2007-09-01 21:48:25 -04:00
|
|
|
raise Exception.new("File to import not found or unreadable: #{original_filename}")
|
2007-03-25 04:20:33 -04:00
|
|
|
else
|
|
|
|
return filename + '.css'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
new_filename
|
2007-03-18 17:11:32 -04:00
|
|
|
end
|
|
|
|
end
|
2007-08-28 01:33:57 -04:00
|
|
|
|
2007-09-01 21:48:25 -04:00
|
|
|
def self.find_full_path(filename, load_paths)
|
|
|
|
load_paths.each do |path|
|
2007-08-28 01:33:57 -04:00
|
|
|
["_#{filename}", filename].each do |name|
|
|
|
|
full_path = File.join(path, name)
|
|
|
|
if File.readable?(full_path)
|
|
|
|
return full_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2006-11-28 14:43:58 -05:00
|
|
|
end
|
|
|
|
end
|