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'
|
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 = ?@
|
2008-04-08 02:09:17 -04:00
|
|
|
|
2007-12-11 05:36:40 -05:00
|
|
|
# Designates a non-parsed rule.
|
|
|
|
ESCAPE_CHAR = ?\\
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
# Designates block as mixin definition rather than CSS rules to output
|
2008-04-16 10:13:55 -04:00
|
|
|
MIXIN_DEFINITION_CHAR = ?=
|
2008-04-09 05:21:49 -04:00
|
|
|
|
|
|
|
# Includes named mixin declared using MIXIN_DEFINITION_CHAR
|
|
|
|
MIXIN_INCLUDE_CHAR = ?+
|
|
|
|
|
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.
|
2008-03-13 04:07:07 -04:00
|
|
|
# See README.rdoc 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
|
2008-03-13 04:07:07 -04:00
|
|
|
# to README.rdoc!
|
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-05-19 10:54:34 -04:00
|
|
|
@template = template.split(/\r\n|\r|\n/)
|
2006-12-19 00:22:19 -05:00
|
|
|
@lines = []
|
2007-11-23 09:49:54 -05:00
|
|
|
@constants = {"important" => "!important"}
|
2008-04-09 05:21:49 -04:00
|
|
|
@mixins = {}
|
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
|
|
|
|
|
2008-04-16 10:17:01 -04:00
|
|
|
def mixins
|
|
|
|
@mixins
|
|
|
|
end
|
|
|
|
|
2007-03-18 17:11:32 -04:00
|
|
|
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
|
2008-03-27 09:29:48 -04:00
|
|
|
@lines.clear
|
2007-03-18 17:11:32 -04:00
|
|
|
|
|
|
|
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
|
2008-06-01 00:37:44 -04:00
|
|
|
old_tabs = nil
|
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
|
2008-06-01 00:37:44 -04:00
|
|
|
raise SyntaxError.new("Indenting at the beginning of the document is illegal.", @line) if old_tabs.nil? && tabs > 0
|
|
|
|
|
|
|
|
if old_tabs && tabs - old_tabs > 1
|
2008-04-19 12:58:45 -04:00
|
|
|
raise SyntaxError.new("#{tabs * 2} spaces were used for indentation. Sass must be indented using two spaces.", @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
|
2008-06-01 00:37:44 -04:00
|
|
|
@lines << ['//', old_tabs || 0]
|
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)
|
2008-04-19 12:58:45 -04:00
|
|
|
return nil if line.strip.empty?
|
|
|
|
return nil unless spaces = line.index(/[^ ]/)
|
|
|
|
|
|
|
|
if spaces % 2 == 1
|
|
|
|
raise SyntaxError.new(<<END.strip, @line)
|
|
|
|
#{spaces} space#{spaces == 1 ? ' was' : 's were'} used for indentation. Sass must be indented using two spaces.
|
|
|
|
END
|
|
|
|
elsif line[spaces] == ?\t
|
|
|
|
raise SyntaxError.new(<<END.strip, @line)
|
|
|
|
A tab character was used for indentation. Sass must be indented using two spaces.
|
|
|
|
Are you sure you have soft tabs enabled in your editor?
|
|
|
|
END
|
2007-01-28 05:14:15 -05:00
|
|
|
end
|
2008-04-19 12:58:45 -04:00
|
|
|
spaces / 2
|
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
|
2008-04-24 16:41:23 -04:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath constants.", @line + 1)
|
2007-03-18 17:11:32 -04:00
|
|
|
elsif node.is_a? Array
|
2008-04-09 05:21:49 -04:00
|
|
|
# arrays can either be full of import statements
|
|
|
|
# or attributes from mixin includes
|
|
|
|
# in either case they shouldn't have children.
|
|
|
|
# Need to peek into the array in order to give meaningful errors
|
|
|
|
directive_type = (node.first.is_a?(Tree::DirectiveNode) ? "import" : "mixin")
|
2008-04-24 16:41:23 -04:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath #{directive_type} directives.", @line + 1)
|
2007-03-18 17:11:32 -04:00
|
|
|
end
|
2007-03-18 00:20:45 -04:00
|
|
|
end
|
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
index = @line if node == :mixin
|
2007-03-18 00:20:45 -04:00
|
|
|
return node, index
|
|
|
|
end
|
2007-03-18 00:52:16 -04:00
|
|
|
|
2008-04-24 16:28:15 -04:00
|
|
|
node.line = @line
|
|
|
|
|
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
|
2008-03-30 14:15:37 -04:00
|
|
|
|
|
|
|
return node, index
|
|
|
|
end
|
|
|
|
|
|
|
|
# Resolve multiline rules
|
|
|
|
if node.is_a?(Tree::RuleNode)
|
2008-04-28 15:13:31 -04:00
|
|
|
if node.continued?
|
2008-03-30 14:15:37 -04:00
|
|
|
child, index = build_tree(index) if @lines[old_index = index]
|
|
|
|
if @lines[old_index].nil? || has_children?(old_index, tabs) || !child.is_a?(Tree::RuleNode)
|
|
|
|
raise SyntaxError.new("Rules can't end in commas.", @line)
|
2007-03-18 00:52:16 -04:00
|
|
|
end
|
2007-02-04 19:41:05 -05:00
|
|
|
|
2008-04-28 15:13:31 -04:00
|
|
|
node.add_rules child
|
2007-03-18 00:52:16 -04:00
|
|
|
end
|
2008-03-30 14:15:37 -04:00
|
|
|
node.children = child.children if child
|
|
|
|
end
|
|
|
|
|
|
|
|
while has_children
|
|
|
|
child, index = build_tree(index)
|
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
validate_and_append_child(node, child)
|
2008-03-30 14:15:37 -04:00
|
|
|
|
|
|
|
has_children = has_children?(index, tabs)
|
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
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
def validate_and_append_child(parent, child)
|
|
|
|
case child
|
|
|
|
when :constant
|
|
|
|
raise SyntaxError.new("Constants may only be declared at the root of a document.", @line)
|
|
|
|
when :mixin
|
|
|
|
raise SyntaxError.new("Mixins may only be defined at the root of a document.", @line)
|
|
|
|
when Array
|
|
|
|
child.each do |c|
|
|
|
|
if c.is_a?(Tree::DirectiveNode)
|
|
|
|
raise SyntaxError.new("Import directives may only be used at the root of a document.", @line)
|
|
|
|
end
|
|
|
|
parent << c
|
|
|
|
end
|
|
|
|
when Tree::Node
|
|
|
|
parent << child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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])
|
2008-04-09 05:21:49 -04:00
|
|
|
when MIXIN_DEFINITION_CHAR
|
|
|
|
parse_mixin_definition(line)
|
|
|
|
when MIXIN_INCLUDE_CHAR
|
2008-05-29 15:05:04 -04:00
|
|
|
if line[1].nil? || line[1] == ?\s
|
|
|
|
Tree::RuleNode.new(line, @options[:style])
|
|
|
|
else
|
|
|
|
parse_mixin_include(line)
|
|
|
|
end
|
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?
|
2008-04-19 13:16:10 -04:00
|
|
|
raise SyntaxError.new("Invalid attribute: \"#{line}\".", @line)
|
2007-01-28 05:14:15 -05:00
|
|
|
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)
|
2008-02-29 16:18:52 -05:00
|
|
|
name, op, value = line.scan(Sass::Constant::MATCH)[0]
|
2007-01-28 05:14:15 -05:00
|
|
|
unless name && value
|
2008-04-19 13:16:10 -04:00
|
|
|
raise SyntaxError.new("Invalid constant: \"#{line}\".", @line)
|
2007-01-28 05:14:15 -05:00
|
|
|
end
|
2008-02-29 16:18:52 -05:00
|
|
|
|
|
|
|
constant = Sass::Constant.parse(value, @constants, @line)
|
|
|
|
if op == '||='
|
|
|
|
@constants[name] ||= constant
|
|
|
|
else
|
|
|
|
@constants[name] = constant
|
|
|
|
end
|
|
|
|
|
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)
|
|
|
|
|
2008-03-01 01:38:01 -05:00
|
|
|
# If value begins with url( or ",
|
|
|
|
# it's a CSS @import rule and we don't want to touch it.
|
|
|
|
if directive == "import" && value !~ /^(url\(|")/
|
2007-03-18 17:11:32 -04:00
|
|
|
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
|
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
def parse_mixin_definition(line)
|
|
|
|
mixin_name = line[1..-1]
|
|
|
|
@mixins[mixin_name] = []
|
|
|
|
index = @line
|
|
|
|
line, tabs = @lines[index]
|
|
|
|
while !line.nil? && tabs > 0
|
|
|
|
child, index = build_tree(index)
|
|
|
|
validate_and_append_child(@mixins[mixin_name], child)
|
|
|
|
line, tabs = @lines[index]
|
|
|
|
end
|
|
|
|
:mixin
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_mixin_include(line)
|
|
|
|
mixin_name = line[1..-1]
|
|
|
|
unless @mixins.has_key?(mixin_name)
|
2008-04-19 13:16:10 -04:00
|
|
|
raise SyntaxError.new("Undefined mixin '#{mixin_name}'.", @line)
|
2008-04-09 05:21:49 -04:00
|
|
|
end
|
|
|
|
@mixins[mixin_name]
|
|
|
|
end
|
|
|
|
|
2007-03-18 17:11:32 -04:00
|
|
|
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$/
|
2008-04-09 05:21:49 -04:00
|
|
|
nodes << Tree::DirectiveNode.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
|
2008-04-16 10:17:01 -04:00
|
|
|
engine.mixins.merge! @mixins
|
2007-03-25 04:20:33 -04:00
|
|
|
|
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
|
2008-04-16 10:17:01 -04:00
|
|
|
@mixins = engine.mixins
|
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
|
2008-04-19 13:16:10 -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
|