gohanlonllc-haml 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. data/README.rdoc +332 -0
  2. data/Rakefile +250 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/css2sass +7 -0
  5. data/bin/haml +9 -0
  6. data/bin/html2haml +7 -0
  7. data/bin/sass +8 -0
  8. data/lib/haml/buffer.rb +255 -0
  9. data/lib/haml/engine.rb +268 -0
  10. data/lib/haml/error.rb +22 -0
  11. data/lib/haml/exec.rb +403 -0
  12. data/lib/haml/filters.rb +275 -0
  13. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  14. data/lib/haml/helpers/action_view_mods.rb +181 -0
  15. data/lib/haml/helpers.rb +488 -0
  16. data/lib/haml/html.rb +219 -0
  17. data/lib/haml/precompiler.rb +904 -0
  18. data/lib/haml/shared.rb +47 -0
  19. data/lib/haml/template/patch.rb +58 -0
  20. data/lib/haml/template/plugin.rb +72 -0
  21. data/lib/haml/template.rb +42 -0
  22. data/lib/haml/util.rb +93 -0
  23. data/lib/haml/version.rb +46 -0
  24. data/lib/haml.rb +1044 -0
  25. data/lib/sass/css.rb +337 -0
  26. data/lib/sass/engine.rb +439 -0
  27. data/lib/sass/environment.rb +48 -0
  28. data/lib/sass/error.rb +42 -0
  29. data/lib/sass/files.rb +100 -0
  30. data/lib/sass/plugin/merb.rb +57 -0
  31. data/lib/sass/plugin/rails.rb +25 -0
  32. data/lib/sass/plugin.rb +203 -0
  33. data/lib/sass/repl.rb +51 -0
  34. data/lib/sass/script/bool.rb +13 -0
  35. data/lib/sass/script/color.rb +97 -0
  36. data/lib/sass/script/funcall.rb +29 -0
  37. data/lib/sass/script/functions.rb +134 -0
  38. data/lib/sass/script/lexer.rb +148 -0
  39. data/lib/sass/script/literal.rb +82 -0
  40. data/lib/sass/script/number.rb +231 -0
  41. data/lib/sass/script/operation.rb +30 -0
  42. data/lib/sass/script/parser.rb +142 -0
  43. data/lib/sass/script/string.rb +9 -0
  44. data/lib/sass/script/unary_operation.rb +21 -0
  45. data/lib/sass/script/variable.rb +20 -0
  46. data/lib/sass/script.rb +38 -0
  47. data/lib/sass/tree/attr_node.rb +72 -0
  48. data/lib/sass/tree/comment_node.rb +39 -0
  49. data/lib/sass/tree/debug_node.rb +22 -0
  50. data/lib/sass/tree/directive_node.rb +51 -0
  51. data/lib/sass/tree/file_node.rb +27 -0
  52. data/lib/sass/tree/for_node.rb +29 -0
  53. data/lib/sass/tree/if_node.rb +32 -0
  54. data/lib/sass/tree/mixin_def_node.rb +18 -0
  55. data/lib/sass/tree/mixin_node.rb +35 -0
  56. data/lib/sass/tree/node.rb +117 -0
  57. data/lib/sass/tree/rule_node.rb +161 -0
  58. data/lib/sass/tree/variable_node.rb +24 -0
  59. data/lib/sass/tree/while_node.rb +21 -0
  60. data/lib/sass.rb +1070 -0
  61. data/rails/init.rb +1 -0
  62. data/test/benchmark.rb +99 -0
  63. data/test/haml/engine_test.rb +795 -0
  64. data/test/haml/helper_test.rb +233 -0
  65. data/test/haml/html2haml_test.rb +108 -0
  66. data/test/haml/markaby/standard.mab +52 -0
  67. data/test/haml/mocks/article.rb +6 -0
  68. data/test/haml/results/content_for_layout.xhtml +15 -0
  69. data/test/haml/results/eval_suppressed.xhtml +9 -0
  70. data/test/haml/results/filters.xhtml +62 -0
  71. data/test/haml/results/helpers.xhtml +93 -0
  72. data/test/haml/results/helpful.xhtml +10 -0
  73. data/test/haml/results/just_stuff.xhtml +68 -0
  74. data/test/haml/results/list.xhtml +12 -0
  75. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  76. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  77. data/test/haml/results/original_engine.xhtml +20 -0
  78. data/test/haml/results/partial_layout.xhtml +5 -0
  79. data/test/haml/results/partials.xhtml +21 -0
  80. data/test/haml/results/render_layout.xhtml +3 -0
  81. data/test/haml/results/silent_script.xhtml +74 -0
  82. data/test/haml/results/standard.xhtml +162 -0
  83. data/test/haml/results/tag_parsing.xhtml +23 -0
  84. data/test/haml/results/very_basic.xhtml +5 -0
  85. data/test/haml/results/whitespace_handling.xhtml +89 -0
  86. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  87. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  88. data/test/haml/rhtml/action_view.rhtml +62 -0
  89. data/test/haml/rhtml/standard.rhtml +54 -0
  90. data/test/haml/template_test.rb +204 -0
  91. data/test/haml/templates/_av_partial_1.haml +9 -0
  92. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  93. data/test/haml/templates/_av_partial_2.haml +5 -0
  94. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  95. data/test/haml/templates/_layout.erb +3 -0
  96. data/test/haml/templates/_layout_for_partial.haml +3 -0
  97. data/test/haml/templates/_partial.haml +8 -0
  98. data/test/haml/templates/_text_area.haml +3 -0
  99. data/test/haml/templates/action_view.haml +47 -0
  100. data/test/haml/templates/action_view_ugly.haml +47 -0
  101. data/test/haml/templates/breakage.haml +8 -0
  102. data/test/haml/templates/content_for_layout.haml +10 -0
  103. data/test/haml/templates/eval_suppressed.haml +11 -0
  104. data/test/haml/templates/filters.haml +66 -0
  105. data/test/haml/templates/helpers.haml +95 -0
  106. data/test/haml/templates/helpful.haml +11 -0
  107. data/test/haml/templates/just_stuff.haml +83 -0
  108. data/test/haml/templates/list.haml +12 -0
  109. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  110. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  111. data/test/haml/templates/original_engine.haml +17 -0
  112. data/test/haml/templates/partial_layout.haml +3 -0
  113. data/test/haml/templates/partialize.haml +1 -0
  114. data/test/haml/templates/partials.haml +12 -0
  115. data/test/haml/templates/render_layout.haml +2 -0
  116. data/test/haml/templates/silent_script.haml +40 -0
  117. data/test/haml/templates/standard.haml +42 -0
  118. data/test/haml/templates/standard_ugly.haml +42 -0
  119. data/test/haml/templates/tag_parsing.haml +21 -0
  120. data/test/haml/templates/very_basic.haml +4 -0
  121. data/test/haml/templates/whitespace_handling.haml +87 -0
  122. data/test/haml/util_test.rb +92 -0
  123. data/test/linked_rails.rb +12 -0
  124. data/test/sass/css2sass_test.rb +215 -0
  125. data/test/sass/engine_test.rb +773 -0
  126. data/test/sass/functions_test.rb +109 -0
  127. data/test/sass/more_results/more1.css +9 -0
  128. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  129. data/test/sass/more_results/more_import.css +29 -0
  130. data/test/sass/more_templates/_more_partial.sass +2 -0
  131. data/test/sass/more_templates/more1.sass +23 -0
  132. data/test/sass/more_templates/more_import.sass +11 -0
  133. data/test/sass/plugin_test.rb +214 -0
  134. data/test/sass/results/alt.css +4 -0
  135. data/test/sass/results/basic.css +9 -0
  136. data/test/sass/results/compact.css +5 -0
  137. data/test/sass/results/complex.css +87 -0
  138. data/test/sass/results/compressed.css +1 -0
  139. data/test/sass/results/expanded.css +19 -0
  140. data/test/sass/results/import.css +29 -0
  141. data/test/sass/results/line_numbers.css +49 -0
  142. data/test/sass/results/mixins.css +95 -0
  143. data/test/sass/results/multiline.css +24 -0
  144. data/test/sass/results/nested.css +22 -0
  145. data/test/sass/results/parent_ref.css +13 -0
  146. data/test/sass/results/script.css +16 -0
  147. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  148. data/test/sass/results/subdir/subdir.css +3 -0
  149. data/test/sass/results/units.css +11 -0
  150. data/test/sass/script_test.rb +250 -0
  151. data/test/sass/templates/_partial.sass +2 -0
  152. data/test/sass/templates/alt.sass +16 -0
  153. data/test/sass/templates/basic.sass +23 -0
  154. data/test/sass/templates/bork.sass +2 -0
  155. data/test/sass/templates/bork2.sass +2 -0
  156. data/test/sass/templates/compact.sass +17 -0
  157. data/test/sass/templates/complex.sass +309 -0
  158. data/test/sass/templates/compressed.sass +15 -0
  159. data/test/sass/templates/expanded.sass +17 -0
  160. data/test/sass/templates/import.sass +11 -0
  161. data/test/sass/templates/importee.sass +19 -0
  162. data/test/sass/templates/line_numbers.sass +13 -0
  163. data/test/sass/templates/mixins.sass +76 -0
  164. data/test/sass/templates/multiline.sass +20 -0
  165. data/test/sass/templates/nested.sass +25 -0
  166. data/test/sass/templates/parent_ref.sass +25 -0
  167. data/test/sass/templates/script.sass +101 -0
  168. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  169. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  170. data/test/sass/templates/subdir/subdir.sass +6 -0
  171. data/test/sass/templates/units.sass +11 -0
  172. data/test/test_helper.rb +27 -0
  173. metadata +241 -0
@@ -0,0 +1,439 @@
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/attr_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/file_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
+ # :stopdoc:
24
+ Mixin = Struct.new(:name, :args, :environment, :tree)
25
+ # :startdoc:
26
+
27
+ # This is the class where all the parsing and processing of the Sass
28
+ # template is done. It can be directly used by the user by creating a
29
+ # new instance and calling <tt>render</tt> to render the template. For example:
30
+ #
31
+ # template = File.load('stylesheets/sassy.sass')
32
+ # sass_engine = Sass::Engine.new(template)
33
+ # output = sass_engine.render
34
+ # puts output
35
+ class Engine
36
+ include Haml::Util
37
+ Line = Struct.new(:text, :tabs, :index, :offset, :filename, :children)
38
+
39
+ # The character that begins a CSS attribute.
40
+ ATTRIBUTE_CHAR = ?:
41
+
42
+ # The character that designates that
43
+ # an attribute should be assigned to a SassScript expression.
44
+ SCRIPT_CHAR = ?=
45
+
46
+ # The character that designates the beginning of a comment,
47
+ # either Sass or CSS.
48
+ COMMENT_CHAR = ?/
49
+
50
+ # The character that follows the general COMMENT_CHAR and designates a Sass comment,
51
+ # which is not output as a CSS comment.
52
+ SASS_COMMENT_CHAR = ?/
53
+
54
+ # The character that follows the general COMMENT_CHAR and designates a CSS comment,
55
+ # which is embedded in the CSS document.
56
+ CSS_COMMENT_CHAR = ?*
57
+
58
+ # The character used to denote a compiler directive.
59
+ DIRECTIVE_CHAR = ?@
60
+
61
+ # Designates a non-parsed rule.
62
+ ESCAPE_CHAR = ?\\
63
+
64
+ # Designates block as mixin definition rather than CSS rules to output
65
+ MIXIN_DEFINITION_CHAR = ?=
66
+
67
+ # Includes named mixin declared using MIXIN_DEFINITION_CHAR
68
+ MIXIN_INCLUDE_CHAR = ?+
69
+
70
+ # The regex that matches and extracts data from
71
+ # attributes of the form <tt>:name attr</tt>.
72
+ ATTRIBUTE = /^:([^\s=:"]+)\s*(=?)(?:\s+|$)(.*)/
73
+
74
+ # The regex that matches attributes of the form <tt>name: attr</tt>.
75
+ ATTRIBUTE_ALTERNATE_MATCHER = /^[^\s:"]+\s*[=:](\s|$)/
76
+
77
+ # The regex that matches and extracts data from
78
+ # attributes of the form <tt>name: attr</tt>.
79
+ ATTRIBUTE_ALTERNATE = /^([^\s=:"]+)(\s*=|:)(?:\s+|$)(.*)/
80
+
81
+ # The default options for Sass::Engine.
82
+ DEFAULT_OPTIONS = {
83
+ :style => :nested,
84
+ :load_paths => ['.'],
85
+ :cache => true,
86
+ :cache_location => './.sass-cache',
87
+ }.freeze
88
+
89
+ # Creates a new instace of Sass::Engine that will compile the given
90
+ # template string when <tt>render</tt> is called.
91
+ # See README.rdoc for available options.
92
+ #
93
+ #--
94
+ #
95
+ # TODO: Add current options to REFRENCE. Remember :filename!
96
+ #
97
+ # When adding options, remember to add information about them
98
+ # to README.rdoc!
99
+ #++
100
+ #
101
+ def initialize(template, options={})
102
+ @options = DEFAULT_OPTIONS.merge(options)
103
+ @template = template
104
+ end
105
+
106
+ # Processes the template and returns the result as a string.
107
+ def render
108
+ to_tree.render
109
+ end
110
+
111
+ alias_method :to_css, :render
112
+
113
+ def to_tree
114
+ root = Tree::Node.new
115
+ append_children(root, tree(tabulate(@template)).first, true)
116
+ root.options = @options
117
+ root
118
+ rescue SyntaxError => e; e.add_metadata(@options[:filename], @line)
119
+ end
120
+
121
+ private
122
+
123
+ def tabulate(string)
124
+ tab_str = nil
125
+ first = true
126
+ enum_with_index(string.gsub(/\r|\n|\r\n|\r\n/, "\n").scan(/^.*?$/)).map do |line, index|
127
+ index += (@options[:line] || 1)
128
+ next if line.strip.empty?
129
+
130
+ line_tab_str = line[/^\s*/]
131
+ unless line_tab_str.empty?
132
+ tab_str ||= line_tab_str
133
+
134
+ raise SyntaxError.new("Indenting at the beginning of the document is illegal.", index) if first
135
+ if tab_str.include?(?\s) && tab_str.include?(?\t)
136
+ raise SyntaxError.new("Indentation can't use both tabs and spaces.", index)
137
+ end
138
+ end
139
+ first &&= !tab_str.nil?
140
+ next Line.new(line.strip, 0, index, 0, @options[:filename], []) if tab_str.nil?
141
+
142
+ line_tabs = line_tab_str.scan(tab_str).size
143
+ raise SyntaxError.new(<<END.strip.gsub("\n", ' '), index) if tab_str * line_tabs != line_tab_str
144
+ Inconsistent indentation: #{Haml::Shared.human_indentation line_tab_str, true} used for indentation,
145
+ but the rest of the document was indented using #{Haml::Shared.human_indentation tab_str}.
146
+ END
147
+ Line.new(line.strip, line_tabs, index, tab_str.size, @options[:filename], [])
148
+ end.compact
149
+ end
150
+
151
+ def tree(arr, i = 0)
152
+ return [], i if arr[i].nil?
153
+
154
+ base = arr[i].tabs
155
+ nodes = []
156
+ while (line = arr[i]) && line.tabs >= base
157
+ if line.tabs > base
158
+ if line.tabs > base + 1
159
+ raise SyntaxError.new("The line was indented #{line.tabs - base} levels deeper than the previous line.", line.index)
160
+ end
161
+
162
+ nodes.last.children, i = tree(arr, i)
163
+ else
164
+ nodes << line
165
+ i += 1
166
+ end
167
+ end
168
+ return nodes, i
169
+ end
170
+
171
+ def build_tree(parent, line, root = false)
172
+ @line = line.index
173
+ node = parse_line(parent, line, root)
174
+
175
+ # Node is a symbol if it's non-outputting, like a variable assignment,
176
+ # or an array if it's a group of nodes to add
177
+ return node unless node.is_a? Tree::Node
178
+
179
+ node.line = line.index
180
+ node.filename = line.filename
181
+
182
+ if node.is_a?(Tree::CommentNode)
183
+ node.lines = line.children
184
+ else
185
+ append_children(node, line.children, false)
186
+ end
187
+ return node
188
+ end
189
+
190
+ def append_children(parent, children, root)
191
+ continued_rule = nil
192
+ children.each do |line|
193
+ child = build_tree(parent, line, root)
194
+
195
+ if child.is_a?(Tree::RuleNode) && child.continued?
196
+ raise SyntaxError.new("Rules can't end in commas.", child.line) unless child.children.empty?
197
+ if continued_rule
198
+ continued_rule.add_rules child
199
+ else
200
+ continued_rule = child
201
+ end
202
+ next
203
+ end
204
+
205
+ if continued_rule
206
+ raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) unless child.is_a?(Tree::RuleNode)
207
+ continued_rule.add_rules child
208
+ continued_rule.children = child.children
209
+ continued_rule, child = nil, continued_rule
210
+ end
211
+
212
+ validate_and_append_child(parent, child, line, root)
213
+ end
214
+
215
+ raise SyntaxError.new("Rules can't end in commas.", continued_rule.line) if continued_rule
216
+
217
+ parent
218
+ end
219
+
220
+ def validate_and_append_child(parent, child, line, root)
221
+ unless root
222
+ case child
223
+ when Tree::MixinDefNode
224
+ raise SyntaxError.new("Mixins may only be defined at the root of a document.", line.index)
225
+ when Tree::DirectiveNode, Tree::FileNode
226
+ raise SyntaxError.new("Import directives may only be used at the root of a document.", line.index)
227
+ end
228
+ end
229
+
230
+ case child
231
+ when Array
232
+ child.each {|c| validate_and_append_child(parent, c, line, root)}
233
+ when Tree::Node
234
+ parent << child
235
+ end
236
+ end
237
+
238
+ def parse_line(parent, line, root)
239
+ case line.text[0]
240
+ when ATTRIBUTE_CHAR
241
+ if line.text[1] != ATTRIBUTE_CHAR
242
+ parse_attribute(line, ATTRIBUTE)
243
+ else
244
+ # Support CSS3-style pseudo-elements,
245
+ # which begin with ::
246
+ Tree::RuleNode.new(line.text)
247
+ end
248
+ when Script::VARIABLE_CHAR
249
+ parse_variable(line)
250
+ when COMMENT_CHAR
251
+ parse_comment(line.text)
252
+ when DIRECTIVE_CHAR
253
+ parse_directive(parent, line, root)
254
+ when ESCAPE_CHAR
255
+ Tree::RuleNode.new(line.text[1..-1])
256
+ when MIXIN_DEFINITION_CHAR
257
+ parse_mixin_definition(line)
258
+ when MIXIN_INCLUDE_CHAR
259
+ if line.text[1].nil?
260
+ Tree::RuleNode.new(line.text)
261
+ else
262
+ parse_mixin_include(line, root)
263
+ end
264
+ else
265
+ if line.text =~ ATTRIBUTE_ALTERNATE_MATCHER
266
+ parse_attribute(line, ATTRIBUTE_ALTERNATE)
267
+ else
268
+ Tree::RuleNode.new(line.text)
269
+ end
270
+ end
271
+ end
272
+
273
+ def parse_attribute(line, attribute_regx)
274
+ name, eq, value = line.text.scan(attribute_regx)[0]
275
+
276
+ if name.nil? || value.nil?
277
+ raise SyntaxError.new("Invalid attribute: \"#{line.text}\".", @line)
278
+ end
279
+ expr = if (eq.strip[0] == SCRIPT_CHAR)
280
+ parse_script(value, :offset => line.offset + line.text.index(value))
281
+ else
282
+ value
283
+ end
284
+ Tree::AttrNode.new(name, expr, attribute_regx == ATTRIBUTE ? :old : :new)
285
+ end
286
+
287
+ def parse_variable(line)
288
+ name, op, value = line.text.scan(Script::MATCH)[0]
289
+ raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath variable declarations.", @line + 1) unless line.children.empty?
290
+ raise SyntaxError.new("Invalid variable: \"#{line.text}\".", @line) unless name && value
291
+
292
+ Tree::VariableNode.new(name, parse_script(value, :offset => line.offset + line.text.index(value)), op == '||=')
293
+ end
294
+
295
+ def parse_comment(line)
296
+ if line[1] == CSS_COMMENT_CHAR || line[1] == SASS_COMMENT_CHAR
297
+ Tree::CommentNode.new(line, line[1] == SASS_COMMENT_CHAR)
298
+ else
299
+ Tree::RuleNode.new(line)
300
+ end
301
+ end
302
+
303
+ def parse_directive(parent, line, root)
304
+ directive, whitespace, value = line.text[1..-1].split(/(\s+)/, 2)
305
+ offset = directive.size + whitespace.size + 1 if whitespace
306
+
307
+ # If value begins with url( or ",
308
+ # it's a CSS @import rule and we don't want to touch it.
309
+ if directive == "import" && value !~ /^(url\(|")/
310
+ raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath import directives.", @line + 1) unless line.children.empty?
311
+ import(value)
312
+ elsif directive == "for"
313
+ parse_for(line, root, value)
314
+ elsif directive == "else"
315
+ parse_else(parent, line, value)
316
+ elsif directive == "while"
317
+ raise SyntaxError.new("Invalid while directive '@while': expected expression.") unless value
318
+ Tree::WhileNode.new(parse_script(value, :offset => offset))
319
+ elsif directive == "if"
320
+ raise SyntaxError.new("Invalid if directive '@if': expected expression.") unless value
321
+ Tree::IfNode.new(parse_script(value, :offset => offset))
322
+ elsif directive == "debug"
323
+ raise SyntaxError.new("Invalid debug directive '@debug': expected expression.") unless value
324
+ raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath debug directives.", @line + 1) unless line.children.empty?
325
+ offset = line.offset + line.text.index(value).to_i
326
+ Tree::DebugNode.new(parse_script(value, :offset => offset))
327
+ else
328
+ Tree::DirectiveNode.new(line.text)
329
+ end
330
+ end
331
+
332
+ def parse_for(line, root, text)
333
+ var, from_expr, to_name, to_expr = text.scan(/^([^\s]+)\s+from\s+(.+)\s+(to|through)\s+(.+)$/).first
334
+
335
+ if var.nil? # scan failed, try to figure out why for error message
336
+ if text !~ /^[^\s]+/
337
+ expected = "variable name"
338
+ elsif text !~ /^[^\s]+\s+from\s+.+/
339
+ expected = "'from <expr>'"
340
+ else
341
+ expected = "'to <expr>' or 'through <expr>'"
342
+ end
343
+ raise SyntaxError.new("Invalid for directive '@for #{text}': expected #{expected}.", @line)
344
+ end
345
+ raise SyntaxError.new("Invalid variable \"#{var}\".", @line) unless var =~ Script::VALIDATE
346
+
347
+ parsed_from = parse_script(from_expr, :offset => line.offset + line.text.index(from_expr))
348
+ parsed_to = parse_script(to_expr, :offset => line.offset + line.text.index(to_expr))
349
+ Tree::ForNode.new(var[1..-1], parsed_from, parsed_to, to_name == 'to')
350
+ end
351
+
352
+ def parse_else(parent, line, text)
353
+ previous = parent.last
354
+ raise SyntaxError.new("@else must come after @if.") unless previous.is_a?(Tree::IfNode)
355
+
356
+ if text
357
+ if text !~ /^if\s+(.+)/
358
+ raise SyntaxError.new("Invalid else directive '@else #{text}': expected 'if <expr>'.", @line)
359
+ end
360
+ expr = parse_script($1, :offset => line.offset + line.text.index($1))
361
+ end
362
+
363
+ node = Tree::IfNode.new(expr)
364
+ append_children(node, line.children, false)
365
+ previous.add_else node
366
+ nil
367
+ end
368
+
369
+ # parses out the arguments between the commas and cleans up the mixin arguments
370
+ # returns nil if it fails to parse, otherwise an array.
371
+ def parse_mixin_arguments(arg_string)
372
+ arg_string = arg_string.strip
373
+ return [] if arg_string.empty?
374
+ return nil unless (arg_string[0] == ?( && arg_string[-1] == ?))
375
+ arg_string = arg_string[1...-1]
376
+ arg_string.split(",", -1).map {|a| a.strip}
377
+ end
378
+
379
+ def parse_mixin_definition(line)
380
+ name, arg_string = line.text.scan(/^=\s*([^(]+)(.*)$/).first
381
+ args = parse_mixin_arguments(arg_string)
382
+ raise SyntaxError.new("Invalid mixin \"#{line.text[1..-1]}\".", @line) if name.nil? || args.nil?
383
+ default_arg_found = false
384
+ required_arg_count = 0
385
+ args.map! do |arg|
386
+ raise SyntaxError.new("Mixin arguments can't be empty.", @line) if arg.empty? || arg == "!"
387
+ unless arg[0] == Script::VARIABLE_CHAR
388
+ raise SyntaxError.new("Mixin argument \"#{arg}\" must begin with an exclamation point (!).", @line)
389
+ end
390
+ arg, default = arg.split(/\s*=\s*/, 2)
391
+ required_arg_count += 1 unless default
392
+ default_arg_found ||= default
393
+ raise SyntaxError.new("Invalid variable \"#{arg}\".", @line) unless arg =~ Script::VALIDATE
394
+ raise SyntaxError.new("Required arguments must not follow optional arguments \"#{arg}\".", @line) if default_arg_found && !default
395
+ default = parse_script(default, :offset => line.offset + line.text.index(default)) if default
396
+ [arg[1..-1], default]
397
+ end
398
+ Tree::MixinDefNode.new(name, args)
399
+ end
400
+
401
+ def parse_mixin_include(line, root)
402
+ name, arg_string = line.text.scan(/^\+\s*([^(]+)(.*)$/).first
403
+ args = parse_mixin_arguments(arg_string)
404
+ raise SyntaxError.new("Illegal nesting: Nothing may be nested beneath mixin directives.", @line + 1) unless line.children.empty?
405
+ raise SyntaxError.new("Invalid mixin include \"#{line.text}\".", @line) if name.nil? || args.nil?
406
+ args.each {|a| raise SyntaxError.new("Mixin arguments can't be empty.", @line) if a.empty?}
407
+
408
+ Tree::MixinNode.new(name, args.map {|s| parse_script(s, :offset => line.offset + line.text.index(s))})
409
+ end
410
+
411
+ def parse_script(script, options = {})
412
+ line = options[:line] || @line
413
+ offset = options[:offset] || 0
414
+ Script.parse(script, line, offset, @options[:filename])
415
+ end
416
+
417
+ def import_paths
418
+ paths = (@options[:load_paths] || []).dup
419
+ paths.unshift(File.dirname(@options[:filename])) if @options[:filename]
420
+ paths
421
+ end
422
+
423
+ def import(files)
424
+ files.split(/,\s*/).map do |filename|
425
+ engine = nil
426
+
427
+ begin
428
+ filename = Sass::Files.find_file_to_import(filename, import_paths)
429
+ rescue Exception => e
430
+ raise SyntaxError.new(e.message, @line)
431
+ end
432
+
433
+ next Tree::DirectiveNode.new("@import url(#{filename})") if filename =~ /\.css$/
434
+
435
+ Tree::FileNode.new(filename)
436
+ end.flatten
437
+ end
438
+ end
439
+ end
@@ -0,0 +1,48 @@
1
+ module Sass
2
+ class Environment
3
+ attr_reader :parent
4
+ attr_writer :options
5
+
6
+ def initialize(parent = nil)
7
+ @vars = {}
8
+ @mixins = {}
9
+ @parent = parent
10
+
11
+ set_var("important", Script::String.new("!important")) unless @parent
12
+ end
13
+
14
+ def options
15
+ @options || (parent && parent.options) || {}
16
+ end
17
+
18
+ def self.inherited_hash(name)
19
+ class_eval <<RUBY, __FILE__, __LINE__ + 1
20
+ def #{name}(name)
21
+ @#{name}s[name] || @parent && @parent.#{name}(name)
22
+ end
23
+
24
+ def set_#{name}(name, value)
25
+ @#{name}s[name] = value unless try_set_#{name}(name, value)
26
+ end
27
+
28
+ def try_set_#{name}(name, value)
29
+ if @#{name}s.include?(name)
30
+ @#{name}s[name] = value
31
+ true
32
+ elsif @parent
33
+ @parent.try_set_#{name}(name, value)
34
+ else
35
+ false
36
+ end
37
+ end
38
+ protected :try_set_#{name}
39
+
40
+ def set_local_#{name}(name, value)
41
+ @#{name}s[name] = value
42
+ end
43
+ RUBY
44
+ end
45
+ inherited_hash :var
46
+ inherited_hash :mixin
47
+ end
48
+ end
data/lib/sass/error.rb ADDED
@@ -0,0 +1,42 @@
1
+ module Sass
2
+ # Sass::SyntaxError encapsulates information about the exception,
3
+ # such as the line of the Sass template it was raised on
4
+ # and the Sass file that was being parsed (if applicable).
5
+ # It also provides a handy way to rescue only exceptions raised
6
+ # because of a faulty template.
7
+ class SyntaxError < StandardError
8
+ # The line of the Sass template on which the exception was thrown.
9
+ attr_accessor :sass_line
10
+
11
+ # The name of the file that was being parsed when the exception was raised.
12
+ # This will be nil unless Sass is being used as an ActionView plugin.
13
+ attr_reader :sass_filename
14
+
15
+ # Creates a new SyntaxError.
16
+ # +lineno+ should be the line of the Sass template on which the error occurred.
17
+ def initialize(msg, lineno = nil)
18
+ @message = msg
19
+ @sass_line = lineno
20
+ end
21
+
22
+ # Add information about the filename and line on which the error was raised.
23
+ def add_metadata(filename, line)
24
+ self.sass_line ||= line
25
+ add_backtrace_entry(filename) unless sass_filename
26
+ raise self
27
+ end
28
+
29
+ # Adds a properly formatted entry to the exception's backtrace.
30
+ # +filename+ should be the file in which the error occurred,
31
+ # if applicable (defaults to "(sass)").
32
+ def add_backtrace_entry(filename) # :nodoc:
33
+ @sass_filename ||= filename
34
+ self.backtrace ||= []
35
+ self.backtrace.unshift "#{@sass_filename || '(sass)'}:#{@sass_line}"
36
+ end
37
+
38
+ def to_s # :nodoc:
39
+ @message
40
+ end
41
+ end
42
+ end
data/lib/sass/files.rb ADDED
@@ -0,0 +1,100 @@
1
+ require 'digest/sha1'
2
+
3
+ module Sass
4
+ # This module contains various bits of functionality
5
+ # related to finding and caching Sass files.
6
+ module Files
7
+ extend self
8
+
9
+ def tree_for(filename, options)
10
+ options = Sass::Engine::DEFAULT_OPTIONS.merge(options)
11
+ text = File.read(filename)
12
+
13
+ if options[:cache]
14
+ compiled_filename = sassc_filename(filename, options)
15
+ sha = Digest::SHA1.hexdigest(text)
16
+
17
+ if dump = try_to_read_sassc(filename, compiled_filename, sha)
18
+ return Marshal.load(dump)
19
+ end
20
+ end
21
+
22
+ engine = Sass::Engine.new(text, options.merge(:filename => filename))
23
+
24
+ begin
25
+ root = engine.to_tree
26
+ rescue Sass::SyntaxError => err
27
+ err.add_backtrace_entry(filename)
28
+ raise err
29
+ end
30
+
31
+ try_to_write_sassc(root, compiled_filename, sha, options) if options[:cache]
32
+
33
+ root
34
+ end
35
+
36
+ def find_file_to_import(filename, load_paths)
37
+ was_sass = false
38
+ original_filename = filename
39
+
40
+ if filename[-5..-1] == ".sass"
41
+ filename = filename[0...-5]
42
+ was_sass = true
43
+ elsif filename[-4..-1] == ".css"
44
+ return filename
45
+ end
46
+
47
+ new_filename = find_full_path("#{filename}.sass", load_paths)
48
+
49
+ return new_filename if new_filename
50
+ return filename + '.css' unless was_sass
51
+ raise SyntaxError.new("File to import not found or unreadable: #{original_filename}.", @line)
52
+ end
53
+
54
+ private
55
+
56
+ def sassc_filename(filename, options)
57
+ File.join(options[:cache_location],
58
+ Digest::SHA1.hexdigest(File.dirname(File.expand_path(filename))),
59
+ File.basename(filename) + 'c')
60
+ end
61
+
62
+ def try_to_read_sassc(filename, compiled_filename, sha)
63
+ return unless File.readable?(compiled_filename)
64
+
65
+ File.open(compiled_filename) do |f|
66
+ return unless f.readline("\n").strip == Sass::VERSION
67
+ return unless f.readline("\n").strip == sha
68
+ return f.read
69
+ end
70
+ end
71
+
72
+ def try_to_write_sassc(root, compiled_filename, sha, options)
73
+ return unless File.writable?(File.dirname(options[:cache_location]))
74
+ return if File.exists?(options[:cache_location]) && !File.writable?(options[:cache_location])
75
+ return if File.exists?(File.dirname(compiled_filename)) && !File.writable?(File.dirname(compiled_filename))
76
+ return if File.exists?(compiled_filename) && !File.writable?(compiled_filename)
77
+ FileUtils.mkdir_p(File.dirname(compiled_filename))
78
+ File.open(compiled_filename, "w") do |f|
79
+ f.puts(Sass::VERSION)
80
+ f.puts(sha)
81
+ f.write(Marshal.dump(root))
82
+ end
83
+ end
84
+
85
+ def find_full_path(filename, load_paths)
86
+ segments = filename.split(File::SEPARATOR)
87
+ segments.push "_#{segments.pop}"
88
+ partial_name = segments.join(File::SEPARATOR)
89
+ load_paths.each do |path|
90
+ [partial_name, filename].each do |name|
91
+ full_path = File.join(path, name)
92
+ if File.readable?(full_path)
93
+ return full_path
94
+ end
95
+ end
96
+ end
97
+ nil
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,57 @@
1
+ unless defined?(Sass::MERB_LOADED)
2
+ Sass::MERB_LOADED = true
3
+
4
+ version = Merb::VERSION.split('.').map { |n| n.to_i }
5
+ if version[0] <= 0 && version[1] < 5
6
+ root = MERB_ROOT
7
+ env = MERB_ENV
8
+ else
9
+ root = Merb.root.to_s
10
+ env = Merb.environment
11
+ end
12
+
13
+ Sass::Plugin.options.merge!(:template_location => root + '/public/stylesheets/sass',
14
+ :css_location => root + '/public/stylesheets',
15
+ :cache_location => root + '/tmp/sass-cache',
16
+ :always_check => env != "production",
17
+ :full_exception => env != "production")
18
+ config = Merb::Plugins.config[:sass] || Merb::Plugins.config["sass"] || {}
19
+
20
+ if defined? config.symbolize_keys!
21
+ config.symbolize_keys!
22
+ end
23
+
24
+ Sass::Plugin.options.merge!(config)
25
+
26
+ if version[0] > 0 || version[1] >= 9
27
+
28
+ class Merb::Rack::Application # :nodoc:
29
+ def call_with_sass(env)
30
+ if !Sass::Plugin.checked_for_updates ||
31
+ Sass::Plugin.options[:always_update] || Sass::Plugin.options[:always_check]
32
+ Sass::Plugin.update_stylesheets
33
+ end
34
+
35
+ call_without_sass(env)
36
+ end
37
+ alias_method :call_without_sass, :call
38
+ alias_method :call, :call_with_sass
39
+ end
40
+
41
+ else
42
+
43
+ class MerbHandler # :nodoc:
44
+ def process_with_sass(request, response)
45
+ if !Sass::Plugin.checked_for_updates ||
46
+ Sass::Plugin.options[:always_update] || Sass::Plugin.options[:always_check]
47
+ Sass::Plugin.update_stylesheets
48
+ end
49
+
50
+ process_without_sass(request, response)
51
+ end
52
+ alias_method :process_without_sass, :process
53
+ alias_method :process, :process_with_sass
54
+ end
55
+
56
+ end
57
+ end