2008-08-04 01:00:32 -04:00
|
|
|
require 'enumerator'
|
2008-08-08 23:00:48 -04:00
|
|
|
require 'strscan'
|
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'
|
2008-06-01 00:17:43 -04:00
|
|
|
require 'haml/shared'
|
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
|
2008-08-08 21:20:14 -04:00
|
|
|
Line = Struct.new(:text, :tabs, :index, :filename, :children)
|
|
|
|
Mixin = Struct.new(:args, :tree)
|
2008-08-04 01:00:32 -04:00
|
|
|
|
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-08-04 01:00:32 -04:00
|
|
|
@template = template
|
2008-10-11 17:55:45 -04:00
|
|
|
@constants = {"important" => Constant::String.new("!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
|
2008-08-10 14:10:01 -04:00
|
|
|
err.sass_line = @line unless err.sass_line
|
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
|
2008-07-19 23:27:17 -04:00
|
|
|
root = Tree::Node.new(@options)
|
2008-08-04 01:00:32 -04:00
|
|
|
append_children(root, tree(tabulate(@template)).first, true)
|
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
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def tabulate(string)
|
|
|
|
tab_str = nil
|
|
|
|
first = true
|
|
|
|
string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/).enum_with_index.map do |line, index|
|
|
|
|
index += 1
|
|
|
|
next if line.strip.empty? || line =~ /^\/\//
|
2007-01-30 23:30:30 -05:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
line_tab_str = line[/^\s*/]
|
|
|
|
unless line_tab_str.empty?
|
|
|
|
tab_str ||= line_tab_str
|
2008-06-01 00:37:44 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
raise SyntaxError.new("Indenting at the beginning of the document is illegal.", index) if first
|
|
|
|
if tab_str.include?(?\s) && tab_str.include?(?\t)
|
|
|
|
raise SyntaxError.new("Indentation can't use both tabs and spaces.", index)
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2006-11-28 14:43:58 -05:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
first &&= !tab_str.nil?
|
2008-08-08 21:20:14 -04:00
|
|
|
next Line.new(line.strip, 0, index, @options[:filename], []) if tab_str.nil?
|
2008-08-04 01:00:32 -04:00
|
|
|
|
|
|
|
line_tabs = line_tab_str.scan(tab_str).size
|
|
|
|
raise SyntaxError.new(<<END.strip.gsub("\n", ' '), index) if tab_str * line_tabs != line_tab_str
|
|
|
|
Inconsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation,
|
|
|
|
but the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}.
|
|
|
|
END
|
2007-03-17 16:48:29 -04:00
|
|
|
|
2008-08-08 21:20:14 -04:00
|
|
|
Line.new(line.strip, line_tabs, index, @options[:filename], [])
|
2008-08-04 01:00:32 -04:00
|
|
|
end.compact
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def tree(arr, i = 0)
|
|
|
|
base = arr[i].tabs
|
|
|
|
nodes = []
|
|
|
|
while (line = arr[i]) && line.tabs >= base
|
|
|
|
if line.tabs > base
|
|
|
|
if line.tabs > base + 1
|
|
|
|
raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.", line.index)
|
|
|
|
end
|
2008-04-19 12:58:45 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
nodes.last.children, i = tree(arr, i)
|
|
|
|
else
|
|
|
|
nodes << line
|
|
|
|
i += 1
|
2008-06-01 01:03:28 -04:00
|
|
|
end
|
2007-01-28 05:14:15 -05:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
return nodes, i
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-08-08 21:20:14 -04:00
|
|
|
def build_tree(line, root = false)
|
2008-08-04 01:00:32 -04:00
|
|
|
@line = line.index
|
2008-08-08 21:20:14 -04:00
|
|
|
node = parse_line(line, root)
|
2007-01-28 05:14:15 -05:00
|
|
|
|
2008-08-09 11:00:11 -04:00
|
|
|
# Node is a symbol if it's non-outputting, like a constant assignment,
|
|
|
|
# or an array if it's a group of nodes to add
|
|
|
|
return node unless node.is_a? Tree::Node
|
2007-03-18 00:52:16 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
node.line = line.index
|
2008-08-08 21:20:14 -04:00
|
|
|
node.filename = line.filename
|
2008-04-24 16:28:15 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
unless node.is_a?(Tree::CommentNode)
|
|
|
|
append_children(node, line.children, false)
|
|
|
|
else
|
|
|
|
node.children = line.children
|
2008-03-30 14:15:37 -04:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
return node
|
|
|
|
end
|
2008-03-30 14:15:37 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def append_children(parent, children, root)
|
|
|
|
continued_rule = nil
|
|
|
|
children.each do |line|
|
2008-08-08 21:20:14 -04:00
|
|
|
child = build_tree(line, root)
|
2008-08-04 01:00:32 -04:00
|
|
|
|
|
|
|
if child.is_a?(Tree::RuleNode) && child.continued?
|
|
|
|
raise SyntaxError.new("Rules can't end in commas.", child.line) unless child.children.empty?
|
|
|
|
if continued_rule
|
|
|
|
continued_rule.add_rules child
|
|
|
|
else
|
|
|
|
continued_rule = child
|
2007-03-18 00:52:16 -04:00
|
|
|
end
|
2008-08-04 01:00:32 -04:00
|
|
|
next
|
2007-03-18 00:52:16 -04:00
|
|
|
end
|
2008-03-30 14:15:37 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
if continued_rule
|
|
|
|
raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) unless child.is_a?(Tree::RuleNode)
|
|
|
|
continued_rule.add_rules child
|
|
|
|
continued_rule.children = child.children
|
|
|
|
continued_rule, child = nil, continued_rule
|
|
|
|
end
|
2008-03-30 14:15:37 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
validate_and_append_child(parent, child, line, root)
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) if continued_rule
|
2008-08-09 11:00:11 -04:00
|
|
|
|
|
|
|
parent
|
2006-12-19 00:22:19 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-08-04 01:00:32 -04:00
|
|
|
def validate_and_append_child(parent, child, line, root)
|
|
|
|
unless root
|
|
|
|
case child
|
|
|
|
when :constant
|
|
|
|
raise SyntaxError.new("Constants may only be declared at the root of a document.", line.index)
|
|
|
|
when :mixin
|
|
|
|
raise SyntaxError.new("Mixins may only be defined at the root of a document.", line.index)
|
|
|
|
when Tree::DirectiveNode
|
|
|
|
raise SyntaxError.new("Import directives may only be used at the root of a document.", line.index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
case child
|
|
|
|
when Array
|
2008-08-04 01:00:32 -04:00
|
|
|
child.each {|c| validate_and_append_child(parent, c, line, root)}
|
2008-04-09 05:21:49 -04:00
|
|
|
when Tree::Node
|
|
|
|
parent << child
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-08 21:20:14 -04:00
|
|
|
def parse_line(line, root)
|
2008-08-04 01:00:32 -04:00
|
|
|
case line.text[0]
|
2007-06-28 04:43:09 -04:00
|
|
|
when ATTRIBUTE_CHAR
|
2008-08-04 01:00:32 -04:00
|
|
|
parse_attribute(line.text, ATTRIBUTE)
|
2007-06-28 04:43:09 -04:00
|
|
|
when Constant::CONSTANT_CHAR
|
2008-08-09 11:00:11 -04:00
|
|
|
parse_constant(line)
|
2007-06-28 04:43:09 -04:00
|
|
|
when COMMENT_CHAR
|
2008-08-04 01:00:32 -04:00
|
|
|
parse_comment(line.text)
|
2007-06-28 04:43:09 -04:00
|
|
|
when DIRECTIVE_CHAR
|
2008-08-09 11:00:11 -04:00
|
|
|
parse_directive(line, root)
|
2007-12-11 05:36:40 -05:00
|
|
|
when ESCAPE_CHAR
|
2008-08-04 01:00:32 -04:00
|
|
|
Tree::RuleNode.new(line.text[1..-1], @options)
|
2008-04-09 05:21:49 -04:00
|
|
|
when MIXIN_DEFINITION_CHAR
|
|
|
|
parse_mixin_definition(line)
|
|
|
|
when MIXIN_INCLUDE_CHAR
|
2008-09-15 02:57:43 -04:00
|
|
|
if line.text[1].nil?
|
2008-08-04 01:00:32 -04:00
|
|
|
Tree::RuleNode.new(line.text, @options)
|
2008-05-29 15:05:04 -04:00
|
|
|
else
|
2008-08-09 11:00:11 -04:00
|
|
|
parse_mixin_include(line, root)
|
2008-05-29 15:05:04 -04:00
|
|
|
end
|
2007-03-17 16:48:29 -04:00
|
|
|
else
|
2008-08-04 01:00:32 -04:00
|
|
|
if line.text =~ ATTRIBUTE_ALTERNATE_MATCHER
|
|
|
|
parse_attribute(line.text, ATTRIBUTE_ALTERNATE)
|
2007-05-03 04:46:02 -04:00
|
|
|
else
|
2008-08-08 23:00:48 -04:00
|
|
|
Tree::RuleNode.new(interpolate(line.text), @options)
|
2007-05-03 04:46:02 -04:00
|
|
|
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
|
2008-08-09 11:00:11 -04:00
|
|
|
value = Sass::Constant.resolve(value, @constants, @line)
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-05-03 04:46:02 -04:00
|
|
|
|
2008-08-08 23:00:48 -04:00
|
|
|
Tree::AttrNode.new(interpolate(name), interpolate(value), @options)
|
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-08-09 11:00:11 -04:00
|
|
|
name, op, value = line.text.scan(Sass::Constant::MATCH)[0]
|
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath constants.", @line + 1) unless line.children.empty?
|
|
|
|
raise SyntaxError.new("Invalid constant: \"#{line.text}\".", @line) unless name && value
|
2008-02-29 16:18:52 -05:00
|
|
|
|
2008-10-03 14:58:34 -04:00
|
|
|
constant = Sass::Constant.parse(value, @constants, @line)
|
2008-02-29 16:18:52 -05:00
|
|
|
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
|
2008-07-19 23:27:17 -04:00
|
|
|
Tree::CommentNode.new(line, @options)
|
2007-03-17 16:48:29 -04:00
|
|
|
else
|
2008-07-19 23:27:17 -04:00
|
|
|
Tree::RuleNode.new(line, @options)
|
2007-03-17 16:48:29 -04:00
|
|
|
end
|
2006-12-04 03:40:23 -05:00
|
|
|
end
|
2007-03-18 17:11:32 -04:00
|
|
|
|
2008-08-09 11:00:11 -04:00
|
|
|
def parse_directive(line, root)
|
|
|
|
directive, value = line.text[1..-1].split(/\s+/, 2)
|
2007-03-18 17:11:32 -04:00
|
|
|
|
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\(|")/
|
2008-08-09 11:00:11 -04:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.", @line + 1) unless line.children.empty?
|
2007-03-18 17:11:32 -04:00
|
|
|
import(value)
|
2008-08-09 11:00:11 -04:00
|
|
|
elsif directive == "if"
|
|
|
|
parse_if(line, root, value)
|
2008-08-10 14:10:01 -04:00
|
|
|
elsif directive == "for"
|
|
|
|
parse_for(line, root, value)
|
2008-08-10 14:21:25 -04:00
|
|
|
elsif directive == "while"
|
|
|
|
parse_while(line, root, value)
|
2007-03-18 17:11:32 -04:00
|
|
|
else
|
2008-08-09 11:00:11 -04:00
|
|
|
Tree::DirectiveNode.new(line.text, @options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_if(line, root, text)
|
|
|
|
if Sass::Constant.parse(text, @constants, line.index).to_bool
|
|
|
|
append_children([], line.children, root)
|
|
|
|
else
|
|
|
|
[]
|
2007-03-18 17:11:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-10 14:10:01 -04:00
|
|
|
def parse_for(line, root, text)
|
|
|
|
var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
|
|
|
|
|
|
|
|
if var.nil? # scan failed, try to figure out why for error message
|
|
|
|
if text !~ /^[^\s]+/
|
|
|
|
expected = "constant name"
|
|
|
|
elsif text !~ /^[^\s]+\s+from\s+.+/
|
|
|
|
expected = "'from <expr>'"
|
|
|
|
else
|
|
|
|
expected = "'to <expr>' or 'through <expr>'"
|
|
|
|
end
|
|
|
|
raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.", @line)
|
|
|
|
end
|
|
|
|
raise SyntaxError.new("Invalid constant \"#{var}\".", @line) unless var =~ Constant::VALIDATE
|
|
|
|
|
|
|
|
from = Sass::Constant.parse(from_expr, @constants, @line).to_i
|
|
|
|
to = Sass::Constant.parse(to_expr, @constants, @line).to_i
|
|
|
|
range = Range.new(from, to, to_name == 'to')
|
|
|
|
|
|
|
|
tree = []
|
|
|
|
old_constants = @constants.dup
|
|
|
|
for i in range
|
2008-10-11 17:55:45 -04:00
|
|
|
@constants[var[1..-1]] = Constant::Number.new(i)
|
2008-08-10 14:10:01 -04:00
|
|
|
append_children(tree, line.children, root)
|
|
|
|
end
|
|
|
|
@constants = old_constants
|
|
|
|
tree
|
|
|
|
end
|
|
|
|
|
2008-08-10 14:21:25 -04:00
|
|
|
def parse_while(line, root, text)
|
|
|
|
tree = []
|
|
|
|
while Sass::Constant.parse(text, @constants, line.index).to_bool
|
|
|
|
append_children(tree, line.children, root)
|
|
|
|
end
|
|
|
|
tree
|
|
|
|
end
|
|
|
|
|
2008-09-30 01:43:05 -04:00
|
|
|
# parses out the arguments between the commas and cleans up the mixin arguments
|
|
|
|
# returns nil if it fails to parse, otherwise an array.
|
|
|
|
def parse_mixin_arguments(arg_string)
|
2008-10-03 03:24:05 -04:00
|
|
|
arg_string = arg_string.strip
|
|
|
|
return [] if arg_string.empty?
|
|
|
|
return nil unless (arg_string[0] == ?( && arg_string[-1] == ?))
|
|
|
|
arg_string = arg_string[1...-1]
|
2008-09-30 01:43:05 -04:00
|
|
|
arg_string.split(",", -1).map {|a| a.strip}
|
|
|
|
end
|
|
|
|
|
2008-04-09 05:21:49 -04:00
|
|
|
def parse_mixin_definition(line)
|
2008-09-30 01:43:05 -04:00
|
|
|
name, arg_string = line.text.scan(/^=\s*([^(]+)(.*)$/).first
|
2008-10-03 03:24:05 -04:00
|
|
|
args = parse_mixin_arguments(arg_string)
|
|
|
|
raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".", @line) if name.nil? || args.nil?
|
2008-08-10 16:14:10 -04:00
|
|
|
default_arg_found = false
|
|
|
|
required_arg_count = 0
|
2008-10-03 03:24:05 -04:00
|
|
|
args.map! do |arg|
|
2008-08-08 21:20:14 -04:00
|
|
|
raise SyntaxError.new("Mixin arguments can't be empty.", @line) if arg.empty? || arg == "!"
|
|
|
|
unless arg[0] == Constant::CONSTANT_CHAR
|
|
|
|
raise SyntaxError.new("Mixin argument \"#{arg}\" must begin with an exclamation point (!).", @line)
|
|
|
|
end
|
2008-08-10 16:14:10 -04:00
|
|
|
arg, default = arg.split(/\s*=\s*/, 2)
|
|
|
|
required_arg_count += 1 unless default
|
|
|
|
default_arg_found ||= default
|
2008-08-08 21:20:14 -04:00
|
|
|
raise SyntaxError.new("Invalid constant \"#{arg}\".", @line) unless arg =~ Constant::VALIDATE
|
2008-08-10 16:14:10 -04:00
|
|
|
raise SyntaxError.new("Required arguments must not follow optional arguments \"#{arg}\".", @line) if default_arg_found && !default
|
2008-10-03 14:58:34 -04:00
|
|
|
default = Sass::Constant.parse(default, @constants, @line) if default
|
2008-08-10 16:14:10 -04:00
|
|
|
{ :name => arg[1..-1], :default_value => default }
|
2008-08-08 21:20:14 -04:00
|
|
|
end
|
|
|
|
mixin = @mixins[name] = Mixin.new(args, line.children)
|
2008-04-09 05:21:49 -04:00
|
|
|
:mixin
|
|
|
|
end
|
|
|
|
|
2008-08-08 21:20:14 -04:00
|
|
|
def parse_mixin_include(line, root)
|
2008-09-30 01:43:05 -04:00
|
|
|
name, arg_string = line.text.scan(/^\+\s*([^(]+)(.*)$/).first
|
|
|
|
args = parse_mixin_arguments(arg_string)
|
2008-08-09 11:00:11 -04:00
|
|
|
raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.", @line + 1) unless line.children.empty?
|
2008-09-30 01:43:05 -04:00
|
|
|
raise SyntaxError.new("Invalid mixin include \"#{line.text}\".", @line) if name.nil? || args.nil?
|
2008-08-08 21:20:14 -04:00
|
|
|
raise SyntaxError.new("Undefined mixin '#{name}'.", @line) unless mixin = @mixins[name]
|
|
|
|
|
|
|
|
args.each {|a| raise SyntaxError.new("Mixin arguments can't be empty.", @line) if a.empty?}
|
2008-08-10 16:14:10 -04:00
|
|
|
raise SyntaxError.new(<<END.gsub("\n", "")) if mixin.args.size < args.size
|
|
|
|
Mixin #{name} takes #{mixin.args.size} argument#{'s' if mixin.args.size != 1}
|
|
|
|
but #{args.size} #{args.size == 1 ? 'was' : 'were'} passed.
|
2008-08-08 21:20:14 -04:00
|
|
|
END
|
|
|
|
|
|
|
|
old_constants = @constants.dup
|
2008-08-10 16:14:10 -04:00
|
|
|
mixin.args.zip(args).inject(@constants) do |constants, (arg, value)|
|
|
|
|
constants[arg[:name]] = if value
|
2008-10-03 14:58:34 -04:00
|
|
|
Sass::Constant.parse(value, old_constants, @line)
|
|
|
|
else
|
|
|
|
arg[:default_value]
|
|
|
|
end
|
2008-08-10 16:14:10 -04:00
|
|
|
raise SyntaxError.new("Mixin #{name} is missing parameter ##{mixin.args.index(arg)+1} (#{arg[:name]}).") unless constants[arg[:name]]
|
2008-08-08 21:20:14 -04:00
|
|
|
constants
|
2008-04-09 05:21:49 -04:00
|
|
|
end
|
2008-08-08 21:20:14 -04:00
|
|
|
|
2008-08-09 11:00:11 -04:00
|
|
|
tree = append_children([], mixin.tree, root)
|
2008-08-08 21:20:14 -04:00
|
|
|
@constants = old_constants
|
|
|
|
tree
|
2008-04-09 05:21:49 -04:00
|
|
|
end
|
|
|
|
|
2008-08-08 23:00:48 -04:00
|
|
|
def interpolate(text)
|
|
|
|
scan = StringScanner.new(text)
|
|
|
|
str = ''
|
|
|
|
|
|
|
|
while scan.scan(/(.*?)(\\*)\#\{/)
|
|
|
|
escapes = scan[2].size
|
|
|
|
str << scan.matched[0...-2 - escapes]
|
|
|
|
if escapes % 2 == 1
|
|
|
|
str << '#{'
|
|
|
|
else
|
2008-08-09 11:00:11 -04:00
|
|
|
str << Sass::Constant.resolve(balance(scan, ?{, ?}, 1)[0][0...-1], @constants, @line)
|
2008-08-08 23:00:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
str + scan.rest
|
|
|
|
end
|
2008-08-09 11:00:11 -04:00
|
|
|
|
2008-08-08 23:00:48 -04:00
|
|
|
def balance(*args)
|
2008-08-29 22:07:59 -04:00
|
|
|
res = Haml::Shared.balance(*args)
|
2008-08-08 23:00:48 -04:00
|
|
|
return res if res
|
|
|
|
raise SyntaxError.new("Unbalanced brackets.", @line)
|
|
|
|
end
|
|
|
|
|
2008-08-02 13:07:53 -04:00
|
|
|
def import_paths
|
|
|
|
paths = @options[:load_paths] || []
|
|
|
|
paths.unshift(File.dirname(@options[:filename])) if @options[:filename]
|
|
|
|
paths
|
|
|
|
end
|
|
|
|
|
2008-10-02 14:34:22 -04:00
|
|
|
def import_paths
|
|
|
|
paths = []
|
|
|
|
paths << File.dirname(@options[:filename]) if @options[:filename]
|
|
|
|
paths += @options[:load_paths] if @options[:load_paths]
|
|
|
|
paths
|
|
|
|
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
|
2008-08-02 13:07:53 -04:00
|
|
|
filename = self.class.find_file_to_import(filename, import_paths)
|
2007-09-01 21:48:25 -04:00
|
|
|
rescue Exception => e
|
|
|
|
raise SyntaxError.new(e.message, @line)
|
|
|
|
end
|
|
|
|
|
2007-03-25 00:34:55 -04:00
|
|
|
if filename =~ /\.css$/
|
2008-07-19 23:27:17 -04:00
|
|
|
nodes << Tree::DirectiveNode.new("@import url(#{filename})", @options)
|
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
|
2008-07-20 12:12:37 -04:00
|
|
|
engine = Sass::Engine.new(file.read, new_options)
|
2007-03-25 00:34:55 -04:00
|
|
|
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
|
2008-07-20 12:12:37 -04:00
|
|
|
nodes += root.children
|
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)
|
2008-10-02 14:34:22 -04:00
|
|
|
segments = filename.split(File::SEPARATOR)
|
|
|
|
segments.push "_#{segments.pop}"
|
|
|
|
partial_name = segments.join(File::SEPARATOR)
|
2007-09-01 21:48:25 -04:00
|
|
|
load_paths.each do |path|
|
2008-10-02 14:34:22 -04:00
|
|
|
[partial_name, filename].each do |name|
|
2007-08-28 01:33:57 -04:00
|
|
|
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
|