Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 17576 Bytes
1 | require 'strscan' |
2 | require 'digest/sha1' |
3 | require 'sass/tree/node' |
4 | require 'sass/tree/rule_node' |
5 | require 'sass/tree/comment_node' |
6 | require 'sass/tree/prop_node' |
7 | require 'sass/tree/directive_node' |
8 | require 'sass/tree/variable_node' |
9 | require 'sass/tree/mixin_def_node' |
10 | require 'sass/tree/mixin_node' |
11 | require 'sass/tree/if_node' |
12 | require 'sass/tree/while_node' |
13 | require 'sass/tree/for_node' |
14 | require 'sass/tree/debug_node' |
15 | require 'sass/tree/import_node' |
16 | require 'sass/environment' |
17 | require 'sass/script' |
18 | require 'sass/error' |
19 | require 'sass/files' |
20 | require 'haml/shared' |
21 | |
22 | module Sass |
23 | # A Sass mixin. |
24 | # |
25 | # `name`: `String` |
26 | # : The name of the mixin. |
27 | # |
28 | # `args`: `Array<(String, Script::Node)>` |
29 | # : The arguments for the mixin. |
30 | # Each element is a tuple containing the name of the argument |
31 | # and the parse tree for the default value of the argument. |
32 | # |
33 | # `environment`: {Sass::Environment} |
34 | # : The environment in which the mixin was defined. |
35 | # This is captured so that the mixin can have access |
36 | # to local variables defined in its scope. |
37 | # |
38 | # `tree`: {Sass::Tree::Node} |
39 | # : The parse tree for the mixin. |
40 | Mixin = Struct.new(:name, :args, :environment, :tree) |
41 | |
42 | # This class handles the parsing and compilation of the Sass template. |
43 | # Example usage: |
44 | # |
45 | # template = File.load('stylesheets/sassy.sass') |
46 | # sass_engine = Sass::Engine.new(template) |
47 | # output = sass_engine.render |
48 | # puts output |
49 | class Engine |
50 | include Haml::Util |
51 | |
52 | # A line of Sass code. |
53 | # |
54 | # `text`: `String` |
55 | # : The text in the line, without any whitespace at the beginning or end. |
56 | # |
57 | # `tabs`: `Fixnum` |
58 | # : The level of indentation of the line. |
59 | # |
60 | # `index`: `Fixnum` |
61 | # : The line number in the original document. |
62 | # |
63 | # `offset`: `Fixnum` |
64 | # : The number of bytes in on the line that the text begins. |
65 | # This ends up being the number of bytes of leading whitespace. |
66 | # |
67 | # `filename`: `String` |
68 | # : The name of the file in which this line appeared. |
69 | # |
70 | # `children`: `Array<Line>` |
71 | # : The lines nested below this one. |
72 | class Line < Struct.new(:text, :tabs, :index, :offset, :filename, :children) |
73 | def comment? |
74 | text[0] == COMMENT_CHAR && (text[1] == SASS_COMMENT_CHAR || text[1] == CSS_COMMENT_CHAR) |
75 | end |
76 | end |
77 | |
78 | # The character that begins a CSS property. |
79 | # @private |
80 | PROPERTY_CHAR = ?: |
81 | |
82 | # The character that designates that |
83 | # a property should be assigned to a SassScript expression. |
84 | # @private |
85 | SCRIPT_CHAR = ?= |
86 | |
87 | # The character that designates the beginning of a comment, |
88 | # either Sass or CSS. |
89 | # @private |
90 | COMMENT_CHAR = ?/ |
91 | |
92 | # The character that follows the general COMMENT_CHAR and designates a Sass comment, |
93 | # which is not output as a CSS comment. |
94 | # @private |
95 | SASS_COMMENT_CHAR = ?/ |
96 | |
97 | # The character that follows the general COMMENT_CHAR and designates a CSS comment, |
98 | # which is embedded in the CSS document. |
99 | # @private |
100 | CSS_COMMENT_CHAR = ?* |
101 | |
102 | # The character used to denote a compiler directive. |
103 | # @private |
104 | DIRECTIVE_CHAR = ?@ |
105 | |
106 | # Designates a non-parsed rule. |
107 | # @private |
108 | ESCAPE_CHAR = ?\\ |
109 | |
110 | # Designates block as mixin definition rather than CSS rules to output |
111 | # @private |
112 | MIXIN_DEFINITION_CHAR = ?= |
113 | |
114 | # Includes named mixin declared using MIXIN_DEFINITION_CHAR |
115 | # @private |
116 | MIXIN_INCLUDE_CHAR = ?+ |
117 | |
118 | # The regex that matches properties of the form `name: prop`. |
119 | # @private |
120 | PROPERTY_NEW_MATCHER = /^[^\s:"\[]+\s*[=:](\s|$)/ |
121 | |
122 | # The regex that matches and extracts data from |
123 | # properties of the form `name: prop`. |
124 | # @private |
125 | PROPERTY_NEW = /^([^\s=:"]+)(\s*=|:)(?:\s+|$)(.*)/ |
126 | |
127 | # The regex that matches and extracts data from |
128 | # properties of the form `:name prop`. |
129 | # @private |
130 | PROPERTY_OLD = /^:([^\s=:"]+)\s*(=?)(?:\s+|$)(.*)/ |
131 | |
132 | # The default options for Sass::Engine. |
133 | DEFAULT_OPTIONS = { |
134 | :style => :nested, |
135 | :load_paths => ['.'], |
136 | :cache => true, |
137 | :cache_location => './.sass-cache', |
138 | }.freeze |
139 | |
140 | # @param template [String] The Sass template. |
141 | # @param options [{Symbol => Object}] An options hash; |
142 | # see {file:SASS_REFERENCE.md#sass_options the Sass options documentation} |
143 | def initialize(template, options={}) |
144 | @options = DEFAULT_OPTIONS.merge(options.reject {|k, v| v.nil?}) |
145 | @template = template |
146 | |
147 | # Support both, because the docs said one and the other actually worked |
148 | # for quite a long time. |
149 | @options[:line_comments] ||= @options[:line_numbers] |
150 | |
151 | # Backwards compatibility |
152 | @options[:property_syntax] ||= @options[:attribute_syntax] |
153 | case @options[:property_syntax] |
154 | when :alternate; @options[:property_syntax] = :new |
155 | when :normal; @options[:property_syntax] = :old |
156 | end |
157 | end |
158 | |
159 | # Render the template to CSS. |
160 | # |
161 | # @return [String] The CSS |
162 | # @raise [Sass::SyntaxError] if there's an error in the document |
163 | def render |
164 | to_tree.render |
165 | end |
166 | |
167 | alias_method :to_css, :render |
168 | |
169 | # Parses the document into its parse tree. |
170 | # |
171 | # @return [Sass::Tree::Node] The root of the parse tree. |
172 | # @raise [Sass::SyntaxError] if there's an error in the document |
173 | def to_tree |
174 | root = Tree::Node.new |
175 | append_children(root, tree(tabulate(@template)).first, true) |
176 | root.options = @options |
177 | root |
178 | rescue SyntaxError => e; e.add_metadata(@options[:filename], @line) |
179 | end |
180 | |
181 | private |
182 | |
183 | def tabulate(string) |
184 | tab_str = nil |
185 | comment_tab_str = nil |
186 | first = true |
187 | lines = [] |
188 | string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/).each_with_index do |line, index| |
189 | index += (@options[:line] || 1) |
190 | if line.strip.empty? |
191 | lines.last.text << "\n" if lines.last && lines.last.comment? |
192 | next |
193 | end |
194 | |
195 | line_tab_str = line[/^\s*/] |
196 | unless line_tab_str.empty? |
197 | if tab_str.nil? |
198 | comment_tab_str ||= line_tab_str |
199 | next if try_comment(line, lines.last, "", comment_tab_str, index) |
200 | comment_tab_str = nil |
201 | end |
202 | |
203 | tab_str ||= line_tab_str |
204 | |
205 | raise SyntaxError.new("Indenting at the beginning of the document is illegal.", index) if first |
206 | if tab_str.include?(?\s) && tab_str.include?(?\t) |
207 | raise SyntaxError.new("Indentation can't use both tabs and spaces.", index) |
208 | end |
209 | end |
210 | first &&= !tab_str.nil? |
211 | if tab_str.nil? |
212 | lines << Line.new(line.strip, 0, index, 0, @options[:filename], []) |
213 | next |
214 | end |
215 | |
216 | comment_tab_str ||= line_tab_str |
217 | if try_comment(line, lines.last, tab_str * (lines.last.tabs + 1), comment_tab_str, index) |
218 | next |
219 | else |
220 | comment_tab_str = nil |
221 | end |
222 | |
223 | line_tabs = line_tab_str.scan(tab_str).size |
224 | raise SyntaxError.new(<<END.strip.gsub("\n", ' '), index) if tab_str * line_tabs != line_tab_str |
225 | Inconsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation, |
226 | but the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}. |
227 | END |
228 | lines << Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], []) |
229 | end |
230 | lines |
231 | end |
232 | |
233 | def try_comment(line, last, tab_str, comment_tab_str, index) |
234 | return unless last && last.comment? |
235 | return unless line =~ /^#{tab_str}/ |
236 | unless line =~ /^(?:#{comment_tab_str})(.*)$/ |
237 | raise SyntaxError.new(<<MSG.strip.gsub("\n", " "), index) |
238 | Inconsistent indentation: |
239 | previous line was indented by #{Haml::Shared.human_indentation comment_tab_str}, |
240 | but this line was indented by #{Haml::Shared.human_indentation line[/^\s*/]}. |
241 | MSG |
242 | end |
243 | |
244 | last.text << "\n" << $1 |
245 | true |
246 | end |
247 | |
248 | def tree(arr, i = 0) |
249 | return [], i if arr[i].nil? |
250 | |
251 | base = arr[i].tabs |
252 | nodes = [] |
253 | while (line = arr[i]) && line.tabs >= base |
254 | if line.tabs > base |
255 | if line.tabs > base + 1 |
256 | raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.", line.index) |
257 | end |
258 | |
259 | nodes.last.children, i = tree(arr, i) |
260 | else |
261 | nodes << line |
262 | i += 1 |
263 | end |
264 | end |
265 | return nodes, i |
266 | end |
267 | |
268 | def build_tree(parent, line, root = false) |
269 | @line = line.index |
270 | node_or_nodes = parse_line(parent, line, root) |
271 | |
272 | Array(node_or_nodes).each do |node| |
273 | # Node is a symbol if it's non-outputting, like a variable assignment |
274 | next unless node.is_a? Tree::Node |
275 | |
276 | node.line = line.index |
277 | node.filename = line.filename |
278 | |
279 | if node.is_a?(Tree::CommentNode) |
280 | node.lines = line.children |
281 | else |
282 | append_children(node, line.children, false) |
283 | end |
284 | end |
285 | |
286 | node_or_nodes |
287 | end |
288 | |
289 | def append_children(parent, children, root) |
290 | continued_rule = nil |
291 | children.each do |line| |
292 | child = build_tree(parent, line, root) |
293 | |
294 | if child.is_a?(Tree::RuleNode) && child.continued? |
295 | raise SyntaxError.new("Rules can't end in commas.", child.line) unless child.children.empty? |
296 | if continued_rule |
297 | continued_rule.add_rules child |
298 | else |
299 | continued_rule = child |
300 | end |
301 | next |
302 | end |
303 | |
304 | if continued_rule |
305 | raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) unless child.is_a?(Tree::RuleNode) |
306 | continued_rule.add_rules child |
307 | continued_rule.children = child.children |
308 | continued_rule, child = nil, continued_rule |
309 | end |
310 | |
311 | check_for_no_children(child) |
312 | validate_and_append_child(parent, child, line, root) |
313 | end |
314 | |
315 | raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) if continued_rule |
316 | |
317 | parent |
318 | end |
319 | |
320 | def validate_and_append_child(parent, child, line, root) |
321 | unless root |
322 | case child |
323 | when Tree::MixinDefNode |
324 | raise SyntaxError.new("Mixins may only be defined at the root of a document.", line.index) |
325 | when Tree::ImportNode |
326 | raise SyntaxError.new("Import directives may only be used at the root of a document.", line.index) |
327 | end |
328 | end |
329 | |
330 | case child |
331 | when Array |
332 | child.each {|c| validate_and_append_child(parent, c, line, root)} |
333 | when Tree::Node |
334 | parent << child |
335 | end |
336 | end |
337 | |
338 | def check_for_no_children(node) |
339 | return unless node.is_a?(Tree::RuleNode) && node.children.empty? |
340 | warning = (node.rules.size == 1) ? <<SHORT : <<LONG |
341 | WARNING on line #{node.line}#{" of #{node.filename}" if node.filename}: |
342 | Selector #{node.rules.first.inspect} doesn't have any properties and will not be rendered. |
343 | SHORT |
344 | |
345 | WARNING on line #{node.line}#{" of #{node.filename}" if node.filename}: |
346 | Selector |
347 | #{node.rules.join("\n ")} |
348 | doesn't have any properties and will not be rendered. |
349 | LONG |
350 | |
351 | warn(warning.strip) |
352 | end |
353 | |
354 | def parse_line(parent, line, root) |
355 | case line.text[0] |
356 | when PROPERTY_CHAR |
357 | if line.text[1] == PROPERTY_CHAR || |
358 | (@options[:property_syntax] == :new && |
359 | line.text =~ PROPERTY_OLD && $3.empty?) |
360 | # Support CSS3-style pseudo-elements, |
361 | # which begin with ::, |
362 | # as well as pseudo-classes |
363 | # if we're using the new property syntax |
364 | Tree::RuleNode.new(line.text) |
365 | else |
366 | parse_property(line, PROPERTY_OLD) |
367 | end |
368 | when Script::VARIABLE_CHAR |
369 | parse_variable(line) |
370 | when COMMENT_CHAR |
371 | parse_comment(line.text) |
372 | when DIRECTIVE_CHAR |
373 | parse_directive(parent, line, root) |
374 | when ESCAPE_CHAR |
375 | Tree::RuleNode.new(line.text[1..-1]) |
376 | when MIXIN_DEFINITION_CHAR |
377 | parse_mixin_definition(line) |
378 | when MIXIN_INCLUDE_CHAR |
379 | if line.text[1].nil? || line.text[1] == ?\s |
380 | Tree::RuleNode.new(line.text) |
381 | else |
382 | parse_mixin_include(line, root) |
383 | end |
384 | else |
385 | if line.text =~ PROPERTY_NEW_MATCHER |
386 | parse_property(line, PROPERTY_NEW) |
387 | else |
388 | Tree::RuleNode.new(line.text) |
389 | end |
390 | end |
391 | end |
392 | |
393 | def parse_property(line, property_regx) |
394 | name, eq, value = line.text.scan(property_regx)[0] |
395 | |
396 | if name.nil? || value.nil? |
397 | raise SyntaxError.new("Invalid property: \"#{line.text}\".", @line) |
398 | end |
399 | expr = if (eq.strip[0] == SCRIPT_CHAR) |
400 | parse_script(value, :offset => line.offset + line.text.index(value)) |
401 | else |
402 | value |
403 | end |
404 | Tree::PropNode.new(name, expr, property_regx == PROPERTY_OLD ? :old : :new) |
405 | end |
406 | |
407 | def parse_variable(line) |
408 | name, op, value = line.text.scan(Script::MATCH)[0] |
409 | raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.", @line + 1) unless line.children.empty? |
410 | raise SyntaxError.new("Invalid variable: \"#{line.text}\".", @line) unless name && value |
411 | |
412 | Tree::VariableNode.new(name, parse_script(value, :offset => line.offset + line.text.index(value)), op == '||=') |
413 | end |
414 | |
415 | def parse_comment(line) |
416 | if line[1] == CSS_COMMENT_CHAR || line[1] == SASS_COMMENT_CHAR |
417 | Tree::CommentNode.new(line, line[1] == SASS_COMMENT_CHAR) |
418 | else |
419 | Tree::RuleNode.new(line) |
420 | end |
421 | end |
422 | |
423 | def parse_directive(parent, line, root) |
424 | directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2) |
425 | offset = directive.size + whitespace.size + 1 if whitespace |
426 | |
427 | # If value begins with url( or ", |
428 | # it's a CSS @import rule and we don't want to touch it. |
429 | if directive == "import" && value !~ /^(url\(|")/ |
430 | raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.", @line + 1) unless line.children.empty? |
431 | value.split(/,\s*/).map {|f| Tree::ImportNode.new(f)} |
432 | elsif directive == "for" |
433 | parse_for(line, root, value) |
434 | elsif directive == "else" |
435 | parse_else(parent, line, value) |
436 | elsif directive == "while" |
437 | raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value |
438 | Tree::WhileNode.new(parse_script(value, :offset => offset)) |
439 | elsif directive == "if" |
440 | raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value |
441 | Tree::IfNode.new(parse_script(value, :offset => offset)) |
442 | elsif directive == "debug" |
443 | raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value |
444 | raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.", @line + 1) unless line.children.empty? |
445 | offset = line.offset + line.text.index(value).to_i |
446 | Tree::DebugNode.new(parse_script(value, :offset => offset)) |
447 | else |
448 | Tree::DirectiveNode.new(line.text) |
449 | end |
450 | end |
451 | |
452 | def parse_for(line, root, text) |
453 | var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first |
454 | |
455 | if var.nil? # scan failed, try to figure out why for error message |
456 | if text !~ /^[^\s]+/ |
457 | expected = "variable name" |
458 | elsif text !~ /^[^\s]+\s+from\s+.+/ |
459 | expected = "'from <expr>'" |
460 | else |
461 | expected = "'to <expr>' or 'through <expr>'" |
462 | end |
463 | raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.", @line) |
464 | end |
465 | raise SyntaxError.new("Invalid variable \"#{var}\".", @line) unless var =~ Script::VALIDATE |
466 | |
467 | parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr)) |
468 | parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr)) |
469 | Tree::ForNode.new(var[1..-1], parsed_from, parsed_to, to_name == 'to') |
470 | end |
471 | |
472 | def parse_else(parent, line, text) |
473 | previous = parent.last |
474 | raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode) |
475 | |
476 | if text |
477 | if text !~ /^if\s+(.+)/ |
478 | raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.", @line) |
479 | end |
480 | expr = parse_script($1, :offset => line.offset + line.text.index($1)) |
481 | end |
482 | |
483 | node = Tree::IfNode.new(expr) |
484 | append_children(node, line.children, false) |
485 | previous.add_else node |
486 | nil |
487 | end |
488 | |
489 | def parse_mixin_definition(line) |
490 | name, arg_string = line.text.scan(/^=\s*([^(]+)(.*)$/).first |
491 | raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".", @line) if name.nil? |
492 | |
493 | offset = line.offset + line.text.size - arg_string.size |
494 | args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_definition_arglist |
495 | default_arg_found = false |
496 | Tree::MixinDefNode.new(name, args) |
497 | end |
498 | |
499 | def parse_mixin_include(line, root) |
500 | name, arg_string = line.text.scan(/^\+\s*([^(]+)(.*)$/).first |
501 | raise SyntaxError.new("Invalid mixin include \"#{line.text}\".", @line) if name.nil? |
502 | |
503 | offset = line.offset + line.text.size - arg_string.size |
504 | args = Script::Parser.new(arg_string.strip, @line, offset).parse_mixin_include_arglist |
505 | raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.", @line + 1) unless line.children.empty? |
506 | Tree::MixinNode.new(name, args) |
507 | end |
508 | |
509 | def parse_script(script, options = {}) |
510 | line = options[:line] || @line |
511 | offset = options[:offset] || 0 |
512 | Script.parse(script, line, offset, @options[:filename]) |
513 | end |
514 | end |
515 | end |