oreorenasass 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +11 -0
  3. data/CONTRIBUTING +3 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.md +221 -0
  6. data/Rakefile +370 -0
  7. data/VERSION +1 -0
  8. data/VERSION_NAME +1 -0
  9. data/bin/sass +13 -0
  10. data/bin/sass-convert +12 -0
  11. data/bin/scss +13 -0
  12. data/extra/update_watch.rb +13 -0
  13. data/init.rb +18 -0
  14. data/lib/sass/cache_stores/base.rb +88 -0
  15. data/lib/sass/cache_stores/chain.rb +34 -0
  16. data/lib/sass/cache_stores/filesystem.rb +60 -0
  17. data/lib/sass/cache_stores/memory.rb +47 -0
  18. data/lib/sass/cache_stores/null.rb +25 -0
  19. data/lib/sass/cache_stores.rb +15 -0
  20. data/lib/sass/callbacks.rb +67 -0
  21. data/lib/sass/css.rb +407 -0
  22. data/lib/sass/engine.rb +1181 -0
  23. data/lib/sass/environment.rb +191 -0
  24. data/lib/sass/error.rb +198 -0
  25. data/lib/sass/exec/base.rb +187 -0
  26. data/lib/sass/exec/sass_convert.rb +264 -0
  27. data/lib/sass/exec/sass_scss.rb +424 -0
  28. data/lib/sass/exec.rb +9 -0
  29. data/lib/sass/features.rb +47 -0
  30. data/lib/sass/importers/base.rb +182 -0
  31. data/lib/sass/importers/filesystem.rb +211 -0
  32. data/lib/sass/importers.rb +22 -0
  33. data/lib/sass/logger/base.rb +30 -0
  34. data/lib/sass/logger/log_level.rb +45 -0
  35. data/lib/sass/logger.rb +12 -0
  36. data/lib/sass/media.rb +210 -0
  37. data/lib/sass/plugin/compiler.rb +565 -0
  38. data/lib/sass/plugin/configuration.rb +118 -0
  39. data/lib/sass/plugin/generic.rb +15 -0
  40. data/lib/sass/plugin/merb.rb +48 -0
  41. data/lib/sass/plugin/rack.rb +60 -0
  42. data/lib/sass/plugin/rails.rb +47 -0
  43. data/lib/sass/plugin/staleness_checker.rb +199 -0
  44. data/lib/sass/plugin.rb +133 -0
  45. data/lib/sass/railtie.rb +10 -0
  46. data/lib/sass/repl.rb +57 -0
  47. data/lib/sass/root.rb +7 -0
  48. data/lib/sass/script/css_lexer.rb +33 -0
  49. data/lib/sass/script/css_parser.rb +34 -0
  50. data/lib/sass/script/functions.rb +2626 -0
  51. data/lib/sass/script/lexer.rb +449 -0
  52. data/lib/sass/script/parser.rb +637 -0
  53. data/lib/sass/script/tree/funcall.rb +306 -0
  54. data/lib/sass/script/tree/interpolation.rb +118 -0
  55. data/lib/sass/script/tree/list_literal.rb +77 -0
  56. data/lib/sass/script/tree/literal.rb +45 -0
  57. data/lib/sass/script/tree/map_literal.rb +64 -0
  58. data/lib/sass/script/tree/node.rb +109 -0
  59. data/lib/sass/script/tree/operation.rb +103 -0
  60. data/lib/sass/script/tree/selector.rb +26 -0
  61. data/lib/sass/script/tree/string_interpolation.rb +104 -0
  62. data/lib/sass/script/tree/unary_operation.rb +69 -0
  63. data/lib/sass/script/tree/variable.rb +57 -0
  64. data/lib/sass/script/tree.rb +16 -0
  65. data/lib/sass/script/value/arg_list.rb +36 -0
  66. data/lib/sass/script/value/base.rb +240 -0
  67. data/lib/sass/script/value/bool.rb +35 -0
  68. data/lib/sass/script/value/color.rb +680 -0
  69. data/lib/sass/script/value/helpers.rb +262 -0
  70. data/lib/sass/script/value/list.rb +113 -0
  71. data/lib/sass/script/value/map.rb +70 -0
  72. data/lib/sass/script/value/null.rb +44 -0
  73. data/lib/sass/script/value/number.rb +530 -0
  74. data/lib/sass/script/value/string.rb +97 -0
  75. data/lib/sass/script/value.rb +11 -0
  76. data/lib/sass/script.rb +66 -0
  77. data/lib/sass/scss/css_parser.rb +42 -0
  78. data/lib/sass/scss/parser.rb +1209 -0
  79. data/lib/sass/scss/rx.rb +141 -0
  80. data/lib/sass/scss/script_lexer.rb +15 -0
  81. data/lib/sass/scss/script_parser.rb +25 -0
  82. data/lib/sass/scss/static_parser.rb +368 -0
  83. data/lib/sass/scss.rb +16 -0
  84. data/lib/sass/selector/abstract_sequence.rb +109 -0
  85. data/lib/sass/selector/comma_sequence.rb +175 -0
  86. data/lib/sass/selector/pseudo.rb +256 -0
  87. data/lib/sass/selector/sequence.rb +600 -0
  88. data/lib/sass/selector/simple.rb +117 -0
  89. data/lib/sass/selector/simple_sequence.rb +325 -0
  90. data/lib/sass/selector.rb +326 -0
  91. data/lib/sass/shared.rb +76 -0
  92. data/lib/sass/source/map.rb +210 -0
  93. data/lib/sass/source/position.rb +39 -0
  94. data/lib/sass/source/range.rb +41 -0
  95. data/lib/sass/stack.rb +120 -0
  96. data/lib/sass/supports.rb +227 -0
  97. data/lib/sass/tree/at_root_node.rb +83 -0
  98. data/lib/sass/tree/charset_node.rb +22 -0
  99. data/lib/sass/tree/comment_node.rb +82 -0
  100. data/lib/sass/tree/content_node.rb +9 -0
  101. data/lib/sass/tree/css_import_node.rb +60 -0
  102. data/lib/sass/tree/debug_node.rb +18 -0
  103. data/lib/sass/tree/directive_node.rb +59 -0
  104. data/lib/sass/tree/each_node.rb +24 -0
  105. data/lib/sass/tree/error_node.rb +18 -0
  106. data/lib/sass/tree/extend_node.rb +43 -0
  107. data/lib/sass/tree/for_node.rb +36 -0
  108. data/lib/sass/tree/function_node.rb +39 -0
  109. data/lib/sass/tree/if_node.rb +52 -0
  110. data/lib/sass/tree/import_node.rb +74 -0
  111. data/lib/sass/tree/keyframe_rule_node.rb +15 -0
  112. data/lib/sass/tree/media_node.rb +48 -0
  113. data/lib/sass/tree/mixin_def_node.rb +38 -0
  114. data/lib/sass/tree/mixin_node.rb +52 -0
  115. data/lib/sass/tree/node.rb +238 -0
  116. data/lib/sass/tree/prop_node.rb +171 -0
  117. data/lib/sass/tree/return_node.rb +19 -0
  118. data/lib/sass/tree/root_node.rb +44 -0
  119. data/lib/sass/tree/rule_node.rb +145 -0
  120. data/lib/sass/tree/supports_node.rb +38 -0
  121. data/lib/sass/tree/trace_node.rb +33 -0
  122. data/lib/sass/tree/variable_node.rb +36 -0
  123. data/lib/sass/tree/visitors/base.rb +72 -0
  124. data/lib/sass/tree/visitors/check_nesting.rb +177 -0
  125. data/lib/sass/tree/visitors/convert.rb +334 -0
  126. data/lib/sass/tree/visitors/cssize.rb +369 -0
  127. data/lib/sass/tree/visitors/deep_copy.rb +107 -0
  128. data/lib/sass/tree/visitors/extend.rb +68 -0
  129. data/lib/sass/tree/visitors/perform.rb +539 -0
  130. data/lib/sass/tree/visitors/set_options.rb +139 -0
  131. data/lib/sass/tree/visitors/to_css.rb +381 -0
  132. data/lib/sass/tree/warn_node.rb +18 -0
  133. data/lib/sass/tree/while_node.rb +18 -0
  134. data/lib/sass/util/cross_platform_random.rb +19 -0
  135. data/lib/sass/util/multibyte_string_scanner.rb +157 -0
  136. data/lib/sass/util/normalized_map.rb +130 -0
  137. data/lib/sass/util/ordered_hash.rb +192 -0
  138. data/lib/sass/util/subset_map.rb +110 -0
  139. data/lib/sass/util/test.rb +9 -0
  140. data/lib/sass/util.rb +1318 -0
  141. data/lib/sass/version.rb +124 -0
  142. data/lib/sass.rb +102 -0
  143. data/rails/init.rb +1 -0
  144. data/test/sass/cache_test.rb +131 -0
  145. data/test/sass/callbacks_test.rb +61 -0
  146. data/test/sass/compiler_test.rb +232 -0
  147. data/test/sass/conversion_test.rb +2054 -0
  148. data/test/sass/css2sass_test.rb +477 -0
  149. data/test/sass/data/hsl-rgb.txt +319 -0
  150. data/test/sass/encoding_test.rb +219 -0
  151. data/test/sass/engine_test.rb +3301 -0
  152. data/test/sass/exec_test.rb +86 -0
  153. data/test/sass/extend_test.rb +1661 -0
  154. data/test/sass/fixtures/test_staleness_check_across_importers.css +1 -0
  155. data/test/sass/fixtures/test_staleness_check_across_importers.scss +1 -0
  156. data/test/sass/functions_test.rb +1926 -0
  157. data/test/sass/importer_test.rb +412 -0
  158. data/test/sass/logger_test.rb +58 -0
  159. data/test/sass/mock_importer.rb +49 -0
  160. data/test/sass/more_results/more1.css +9 -0
  161. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  162. data/test/sass/more_results/more_import.css +29 -0
  163. data/test/sass/more_templates/_more_partial.sass +2 -0
  164. data/test/sass/more_templates/more1.sass +23 -0
  165. data/test/sass/more_templates/more_import.sass +11 -0
  166. data/test/sass/plugin_test.rb +554 -0
  167. data/test/sass/results/alt.css +4 -0
  168. data/test/sass/results/basic.css +9 -0
  169. data/test/sass/results/cached_import_option.css +3 -0
  170. data/test/sass/results/compact.css +5 -0
  171. data/test/sass/results/complex.css +86 -0
  172. data/test/sass/results/compressed.css +1 -0
  173. data/test/sass/results/expanded.css +19 -0
  174. data/test/sass/results/filename_fn.css +3 -0
  175. data/test/sass/results/if.css +3 -0
  176. data/test/sass/results/import.css +31 -0
  177. data/test/sass/results/import_charset.css +5 -0
  178. data/test/sass/results/import_charset_1_8.css +5 -0
  179. data/test/sass/results/import_charset_ibm866.css +5 -0
  180. data/test/sass/results/import_content.css +1 -0
  181. data/test/sass/results/line_numbers.css +49 -0
  182. data/test/sass/results/mixins.css +95 -0
  183. data/test/sass/results/multiline.css +24 -0
  184. data/test/sass/results/nested.css +22 -0
  185. data/test/sass/results/options.css +1 -0
  186. data/test/sass/results/parent_ref.css +13 -0
  187. data/test/sass/results/script.css +16 -0
  188. data/test/sass/results/scss_import.css +31 -0
  189. data/test/sass/results/scss_importee.css +2 -0
  190. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  191. data/test/sass/results/subdir/subdir.css +3 -0
  192. data/test/sass/results/units.css +11 -0
  193. data/test/sass/results/warn.css +0 -0
  194. data/test/sass/results/warn_imported.css +0 -0
  195. data/test/sass/script_conversion_test.rb +328 -0
  196. data/test/sass/script_test.rb +1054 -0
  197. data/test/sass/scss/css_test.rb +1215 -0
  198. data/test/sass/scss/rx_test.rb +156 -0
  199. data/test/sass/scss/scss_test.rb +3900 -0
  200. data/test/sass/scss/test_helper.rb +37 -0
  201. data/test/sass/source_map_test.rb +977 -0
  202. data/test/sass/superselector_test.rb +191 -0
  203. data/test/sass/templates/_cached_import_option_partial.scss +1 -0
  204. data/test/sass/templates/_double_import_loop2.sass +1 -0
  205. data/test/sass/templates/_filename_fn_import.scss +11 -0
  206. data/test/sass/templates/_imported_charset_ibm866.sass +4 -0
  207. data/test/sass/templates/_imported_charset_utf8.sass +4 -0
  208. data/test/sass/templates/_imported_content.sass +3 -0
  209. data/test/sass/templates/_partial.sass +2 -0
  210. data/test/sass/templates/_same_name_different_partiality.scss +1 -0
  211. data/test/sass/templates/alt.sass +16 -0
  212. data/test/sass/templates/basic.sass +23 -0
  213. data/test/sass/templates/bork1.sass +2 -0
  214. data/test/sass/templates/bork2.sass +2 -0
  215. data/test/sass/templates/bork3.sass +2 -0
  216. data/test/sass/templates/bork4.sass +2 -0
  217. data/test/sass/templates/bork5.sass +3 -0
  218. data/test/sass/templates/cached_import_option.scss +3 -0
  219. data/test/sass/templates/compact.sass +17 -0
  220. data/test/sass/templates/complex.sass +305 -0
  221. data/test/sass/templates/compressed.sass +15 -0
  222. data/test/sass/templates/double_import_loop1.sass +1 -0
  223. data/test/sass/templates/expanded.sass +17 -0
  224. data/test/sass/templates/filename_fn.scss +18 -0
  225. data/test/sass/templates/if.sass +11 -0
  226. data/test/sass/templates/import.sass +12 -0
  227. data/test/sass/templates/import_charset.sass +9 -0
  228. data/test/sass/templates/import_charset_1_8.sass +6 -0
  229. data/test/sass/templates/import_charset_ibm866.sass +11 -0
  230. data/test/sass/templates/import_content.sass +4 -0
  231. data/test/sass/templates/importee.less +2 -0
  232. data/test/sass/templates/importee.sass +19 -0
  233. data/test/sass/templates/line_numbers.sass +13 -0
  234. data/test/sass/templates/mixin_bork.sass +5 -0
  235. data/test/sass/templates/mixins.sass +76 -0
  236. data/test/sass/templates/multiline.sass +20 -0
  237. data/test/sass/templates/nested.sass +25 -0
  238. data/test/sass/templates/nested_bork1.sass +2 -0
  239. data/test/sass/templates/nested_bork2.sass +2 -0
  240. data/test/sass/templates/nested_bork3.sass +2 -0
  241. data/test/sass/templates/nested_bork4.sass +2 -0
  242. data/test/sass/templates/nested_import.sass +2 -0
  243. data/test/sass/templates/nested_mixin_bork.sass +6 -0
  244. data/test/sass/templates/options.sass +2 -0
  245. data/test/sass/templates/parent_ref.sass +25 -0
  246. data/test/sass/templates/same_name_different_ext.sass +2 -0
  247. data/test/sass/templates/same_name_different_ext.scss +1 -0
  248. data/test/sass/templates/same_name_different_partiality.scss +1 -0
  249. data/test/sass/templates/script.sass +101 -0
  250. data/test/sass/templates/scss_import.scss +12 -0
  251. data/test/sass/templates/scss_importee.scss +1 -0
  252. data/test/sass/templates/single_import_loop.sass +1 -0
  253. data/test/sass/templates/subdir/import_up1.scss +1 -0
  254. data/test/sass/templates/subdir/import_up2.scss +1 -0
  255. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  256. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  257. data/test/sass/templates/subdir/subdir.sass +6 -0
  258. data/test/sass/templates/units.sass +11 -0
  259. data/test/sass/templates/warn.sass +3 -0
  260. data/test/sass/templates/warn_imported.sass +4 -0
  261. data/test/sass/test_helper.rb +8 -0
  262. data/test/sass/util/multibyte_string_scanner_test.rb +147 -0
  263. data/test/sass/util/normalized_map_test.rb +51 -0
  264. data/test/sass/util/subset_map_test.rb +91 -0
  265. data/test/sass/util_test.rb +467 -0
  266. data/test/sass/value_helpers_test.rb +179 -0
  267. data/test/test_helper.rb +109 -0
  268. metadata +386 -0
@@ -0,0 +1,191 @@
1
+ require 'set'
2
+
3
+ module Sass
4
+ # The abstract base class for lexical environments for SassScript.
5
+ class BaseEnvironment
6
+ class << self
7
+ # Note: when updating this,
8
+ # update sass/yard/inherited_hash.rb as well.
9
+ def inherited_hash_accessor(name)
10
+ inherited_hash_reader(name)
11
+ inherited_hash_writer(name)
12
+ end
13
+
14
+ def inherited_hash_reader(name)
15
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
+ def #{name}(name)
17
+ _#{name}(name.tr('_', '-'))
18
+ end
19
+
20
+ def _#{name}(name)
21
+ (@#{name}s && @#{name}s[name]) || @parent && @parent._#{name}(name)
22
+ end
23
+ protected :_#{name}
24
+
25
+ def is_#{name}_global?(name)
26
+ return !@parent if @#{name}s && @#{name}s.has_key?(name)
27
+ @parent && @parent.is_#{name}_global?(name)
28
+ end
29
+ RUBY
30
+ end
31
+
32
+ def inherited_hash_writer(name)
33
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
34
+ def set_#{name}(name, value)
35
+ name = name.tr('_', '-')
36
+ @#{name}s[name] = value unless try_set_#{name}(name, value)
37
+ end
38
+
39
+ def try_set_#{name}(name, value)
40
+ @#{name}s ||= {}
41
+ if @#{name}s.include?(name)
42
+ @#{name}s[name] = value
43
+ true
44
+ elsif @parent && !@parent.global?
45
+ @parent.try_set_#{name}(name, value)
46
+ else
47
+ false
48
+ end
49
+ end
50
+ protected :try_set_#{name}
51
+
52
+ def set_local_#{name}(name, value)
53
+ @#{name}s ||= {}
54
+ @#{name}s[name.tr('_', '-')] = value
55
+ end
56
+
57
+ def set_global_#{name}(name, value)
58
+ global_env.set_#{name}(name, value)
59
+ end
60
+ RUBY
61
+ end
62
+ end
63
+
64
+ # The options passed to the Sass Engine.
65
+ attr_reader :options
66
+
67
+ attr_writer :caller
68
+ attr_writer :content
69
+ attr_writer :selector
70
+
71
+ # variable
72
+ # Script::Value
73
+ inherited_hash_reader :var
74
+
75
+ # mixin
76
+ # Sass::Callable
77
+ inherited_hash_reader :mixin
78
+
79
+ # function
80
+ # Sass::Callable
81
+ inherited_hash_reader :function
82
+
83
+ # @param options [{Symbol => Object}] The options hash. See
84
+ # {file:SASS_REFERENCE.md#sass_options the Sass options documentation}.
85
+ # @param parent [Environment] See \{#parent}
86
+ def initialize(parent = nil, options = nil)
87
+ @parent = parent
88
+ @options = options || (parent && parent.options) || {}
89
+ @stack = Sass::Stack.new if @parent.nil?
90
+ end
91
+
92
+ # Returns whether this is the global environment.
93
+ #
94
+ # @return [Boolean]
95
+ def global?
96
+ @parent.nil?
97
+ end
98
+
99
+ # The environment of the caller of this environment's mixin or function.
100
+ # @return {Environment?}
101
+ def caller
102
+ @caller || (@parent && @parent.caller)
103
+ end
104
+
105
+ # The content passed to this environment. This is naturally only set
106
+ # for mixin body environments with content passed in.
107
+ #
108
+ # @return {[Array<Sass::Tree::Node>, Environment]?} The content nodes and
109
+ # the lexical environment of the content block.
110
+ def content
111
+ @content || (@parent && @parent.content)
112
+ end
113
+
114
+ # The selector for the current CSS rule, or nil if there is no
115
+ # current CSS rule.
116
+ #
117
+ # @return [Selector::CommaSequence?] The current selector, with any
118
+ # nesting fully resolved.
119
+ def selector
120
+ @selector || (@caller && @caller.selector) || (@parent && @parent.selector)
121
+ end
122
+
123
+ # The top-level Environment object.
124
+ #
125
+ # @return [Environment]
126
+ def global_env
127
+ @global_env ||= global? ? self : @parent.global_env
128
+ end
129
+
130
+ # The import/mixin stack.
131
+ #
132
+ # @return [Sass::Stack]
133
+ def stack
134
+ @stack || global_env.stack
135
+ end
136
+ end
137
+
138
+ # The lexical environment for SassScript.
139
+ # This keeps track of variable, mixin, and function definitions.
140
+ #
141
+ # A new environment is created for each level of Sass nesting.
142
+ # This allows variables to be lexically scoped.
143
+ # The new environment refers to the environment in the upper scope,
144
+ # so it has access to variables defined in enclosing scopes,
145
+ # but new variables are defined locally.
146
+ #
147
+ # Environment also keeps track of the {Engine} options
148
+ # so that they can be made available to {Sass::Script::Functions}.
149
+ class Environment < BaseEnvironment
150
+ # The enclosing environment,
151
+ # or nil if this is the global environment.
152
+ #
153
+ # @return [Environment]
154
+ attr_reader :parent
155
+
156
+ # variable
157
+ # Script::Value
158
+ inherited_hash_writer :var
159
+
160
+ # mixin
161
+ # Sass::Callable
162
+ inherited_hash_writer :mixin
163
+
164
+ # function
165
+ # Sass::Callable
166
+ inherited_hash_writer :function
167
+ end
168
+
169
+ # A read-only wrapper for a lexical environment for SassScript.
170
+ class ReadOnlyEnvironment < BaseEnvironment
171
+ # The read-only environment of the caller of this environment's mixin or function.
172
+ #
173
+ # @see BaseEnvironment#caller
174
+ # @return {ReadOnlyEnvironment}
175
+ def caller
176
+ return @caller if @caller
177
+ env = super
178
+ @caller ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
179
+ end
180
+
181
+ # The read-only content passed to this environment.
182
+ #
183
+ # @see BaseEnvironment#content
184
+ # @return {ReadOnlyEnvironment}
185
+ def content
186
+ return @content if @content
187
+ env = super
188
+ @content ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
189
+ end
190
+ end
191
+ end
data/lib/sass/error.rb ADDED
@@ -0,0 +1,198 @@
1
+ module Sass
2
+ # An exception class that keeps track of
3
+ # the line of the Sass template it was raised on
4
+ # and the Sass file that was being parsed (if applicable).
5
+ #
6
+ # All Sass errors are raised as {Sass::SyntaxError}s.
7
+ #
8
+ # When dealing with SyntaxErrors,
9
+ # it's important to provide filename and line number information.
10
+ # This will be used in various error reports to users, including backtraces;
11
+ # see \{#sass\_backtrace} for details.
12
+ #
13
+ # Some of this information is usually provided as part of the constructor.
14
+ # New backtrace entries can be added with \{#add\_backtrace},
15
+ # which is called when an exception is raised between files (e.g. with `@import`).
16
+ #
17
+ # Often, a chunk of code will all have similar backtrace information -
18
+ # the same filename or even line.
19
+ # It may also be useful to have a default line number set.
20
+ # In those situations, the default values can be used
21
+ # by omitting the information on the original exception,
22
+ # and then calling \{#modify\_backtrace} in a wrapper `rescue`.
23
+ # When doing this, be sure that all exceptions ultimately end up
24
+ # with the information filled in.
25
+ class SyntaxError < StandardError
26
+ # The backtrace of the error within Sass files.
27
+ # This is an array of hashes containing information for a single entry.
28
+ # The hashes have the following keys:
29
+ #
30
+ # `:filename`
31
+ # : The name of the file in which the exception was raised,
32
+ # or `nil` if no filename is available.
33
+ #
34
+ # `:mixin`
35
+ # : The name of the mixin in which the exception was raised,
36
+ # or `nil` if it wasn't raised in a mixin.
37
+ #
38
+ # `:line`
39
+ # : The line of the file on which the error occurred. Never nil.
40
+ #
41
+ # This information is also included in standard backtrace format
42
+ # in the output of \{#backtrace}.
43
+ #
44
+ # @return [Aray<{Symbol => Object>}]
45
+ attr_accessor :sass_backtrace
46
+
47
+ # The text of the template where this error was raised.
48
+ #
49
+ # @return [String]
50
+ attr_accessor :sass_template
51
+
52
+ # @param msg [String] The error message
53
+ # @param attrs [{Symbol => Object}] The information in the backtrace entry.
54
+ # See \{#sass\_backtrace}
55
+ def initialize(msg, attrs = {})
56
+ @message = msg
57
+ @sass_backtrace = []
58
+ add_backtrace(attrs)
59
+ end
60
+
61
+ # The name of the file in which the exception was raised.
62
+ # This could be `nil` if no filename is available.
63
+ #
64
+ # @return [String, nil]
65
+ def sass_filename
66
+ sass_backtrace.first[:filename]
67
+ end
68
+
69
+ # The name of the mixin in which the error occurred.
70
+ # This could be `nil` if the error occurred outside a mixin.
71
+ #
72
+ # @return [Fixnum]
73
+ def sass_mixin
74
+ sass_backtrace.first[:mixin]
75
+ end
76
+
77
+ # The line of the Sass template on which the error occurred.
78
+ #
79
+ # @return [Fixnum]
80
+ def sass_line
81
+ sass_backtrace.first[:line]
82
+ end
83
+
84
+ # Adds an entry to the exception's Sass backtrace.
85
+ #
86
+ # @param attrs [{Symbol => Object}] The information in the backtrace entry.
87
+ # See \{#sass\_backtrace}
88
+ def add_backtrace(attrs)
89
+ sass_backtrace << attrs.reject {|k, v| v.nil?}
90
+ end
91
+
92
+ # Modify the top Sass backtrace entries
93
+ # (that is, the most deeply nested ones)
94
+ # to have the given attributes.
95
+ #
96
+ # Specifically, this goes through the backtrace entries
97
+ # from most deeply nested to least,
98
+ # setting the given attributes for each entry.
99
+ # If an entry already has one of the given attributes set,
100
+ # the pre-existing attribute takes precedence
101
+ # and is not used for less deeply-nested entries
102
+ # (even if they don't have that attribute set).
103
+ #
104
+ # @param attrs [{Symbol => Object}] The information to add to the backtrace entry.
105
+ # See \{#sass\_backtrace}
106
+ def modify_backtrace(attrs)
107
+ attrs = attrs.reject {|k, v| v.nil?}
108
+ # Move backwards through the backtrace
109
+ (0...sass_backtrace.size).to_a.reverse.each do |i|
110
+ entry = sass_backtrace[i]
111
+ sass_backtrace[i] = attrs.merge(entry)
112
+ attrs.reject! {|k, v| entry.include?(k)}
113
+ break if attrs.empty?
114
+ end
115
+ end
116
+
117
+ # @return [String] The error message
118
+ def to_s
119
+ @message
120
+ end
121
+
122
+ # Returns the standard exception backtrace,
123
+ # including the Sass backtrace.
124
+ #
125
+ # @return [Array<String>]
126
+ def backtrace
127
+ return nil if super.nil?
128
+ return super if sass_backtrace.all? {|h| h.empty?}
129
+ sass_backtrace.map do |h|
130
+ "#{h[:filename] || "(sass)"}:#{h[:line]}" +
131
+ (h[:mixin] ? ":in `#{h[:mixin]}'" : "")
132
+ end + super
133
+ end
134
+
135
+ # Returns a string representation of the Sass backtrace.
136
+ #
137
+ # @param default_filename [String] The filename to use for unknown files
138
+ # @see #sass_backtrace
139
+ # @return [String]
140
+ def sass_backtrace_str(default_filename = "an unknown file")
141
+ lines = message.split("\n")
142
+ msg = lines[0] + lines[1..-1].
143
+ map {|l| "\n" + (" " * "Error: ".size) + l}.join
144
+ "Error: #{msg}" +
145
+ Sass::Util.enum_with_index(sass_backtrace).map do |entry, i|
146
+ "\n #{i == 0 ? "on" : "from"} line #{entry[:line]}" +
147
+ " of #{entry[:filename] || default_filename}" +
148
+ (entry[:mixin] ? ", in `#{entry[:mixin]}'" : "")
149
+ end.join
150
+ end
151
+
152
+ class << self
153
+ # Returns an error report for an exception in CSS format.
154
+ #
155
+ # @param e [Exception]
156
+ # @param line_offset [Fixnum] The number of the first line of the Sass template.
157
+ # @return [String] The error report
158
+ # @raise [Exception] `e`, if the
159
+ # {file:SASS_REFERENCE.md#full_exception-option `:full_exception`} option
160
+ # is set to false.
161
+ def exception_to_css(e, line_offset = 1)
162
+ header = header_string(e, line_offset)
163
+
164
+ <<END
165
+ /*
166
+ #{header.gsub("*/", "*\\/")}
167
+
168
+ Backtrace:\n#{e.backtrace.join("\n").gsub("*/", "*\\/")}
169
+ */
170
+ body:before {
171
+ white-space: pre;
172
+ font-family: monospace;
173
+ content: "#{header.gsub('"', '\"').gsub("\n", '\\A ')}"; }
174
+ END
175
+ end
176
+
177
+ private
178
+
179
+ def header_string(e, line_offset)
180
+ unless e.is_a?(Sass::SyntaxError) && e.sass_line && e.sass_template
181
+ return "#{e.class}: #{e.message}"
182
+ end
183
+
184
+ line_num = e.sass_line + 1 - line_offset
185
+ min = [line_num - 6, 0].max
186
+ section = e.sass_template.rstrip.split("\n")[min ... line_num + 5]
187
+ return e.sass_backtrace_str if section.nil? || section.empty?
188
+
189
+ e.sass_backtrace_str + "\n\n" + Sass::Util.enum_with_index(section).
190
+ map {|line, i| "#{line_offset + min + i}: #{line}"}.join("\n")
191
+ end
192
+ end
193
+ end
194
+
195
+ # The class for Sass errors that are raised due to invalid unit conversions
196
+ # in SassScript.
197
+ class UnitConversionError < SyntaxError; end
198
+ end
@@ -0,0 +1,187 @@
1
+ require 'optparse'
2
+
3
+ module Sass::Exec
4
+ # The abstract base class for Sass executables.
5
+ class Base
6
+ # @param args [Array<String>] The command-line arguments
7
+ def initialize(args)
8
+ @args = args
9
+ @options = {}
10
+ end
11
+
12
+ # Parses the command-line arguments and runs the executable.
13
+ # Calls `Kernel#exit` at the end, so it never returns.
14
+ #
15
+ # @see #parse
16
+ def parse!
17
+ # rubocop:disable RescueException
18
+ begin
19
+ parse
20
+ rescue Exception => e
21
+ raise e if @options[:trace] || e.is_a?(SystemExit)
22
+
23
+ $stderr.print "#{e.class}: " unless e.class == RuntimeError
24
+ $stderr.puts "#{e.message}"
25
+ $stderr.puts " Use --trace for backtrace."
26
+ exit 1
27
+ end
28
+ exit 0
29
+ # rubocop:enable RescueException
30
+ end
31
+
32
+ # Parses the command-line arguments and runs the executable.
33
+ # This does not handle exceptions or exit the program.
34
+ #
35
+ # @see #parse!
36
+ def parse
37
+ @opts = OptionParser.new(&method(:set_opts))
38
+ @opts.parse!(@args)
39
+
40
+ process_result
41
+
42
+ @options
43
+ end
44
+
45
+ # @return [String] A description of the executable
46
+ def to_s
47
+ @opts.to_s
48
+ end
49
+
50
+ protected
51
+
52
+ # Finds the line of the source template
53
+ # on which an exception was raised.
54
+ #
55
+ # @param exception [Exception] The exception
56
+ # @return [String] The line number
57
+ def get_line(exception)
58
+ # SyntaxErrors have weird line reporting
59
+ # when there's trailing whitespace
60
+ if exception.is_a?(::SyntaxError)
61
+ return (exception.message.scan(/:(\d+)/).first || ["??"]).first
62
+ end
63
+ (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
64
+ end
65
+
66
+ # Tells optparse how to parse the arguments
67
+ # available for all executables.
68
+ #
69
+ # This is meant to be overridden by subclasses
70
+ # so they can add their own options.
71
+ #
72
+ # @param opts [OptionParser]
73
+ def set_opts(opts)
74
+ Sass::Util.abstract(this)
75
+ end
76
+
77
+ # Set an option for specifying `Encoding.default_external`.
78
+ #
79
+ # @param opts [OptionParser]
80
+ def encoding_option(opts)
81
+ encoding_desc = if Sass::Util.ruby1_8?
82
+ 'Does not work in Ruby 1.8.'
83
+ else
84
+ 'Specify the default encoding for input files.'
85
+ end
86
+ opts.on('-E', '--default-encoding ENCODING', encoding_desc) do |encoding|
87
+ if Sass::Util.ruby1_8?
88
+ $stderr.puts "Specifying the encoding is not supported in ruby 1.8."
89
+ exit 1
90
+ else
91
+ Encoding.default_external = encoding
92
+ end
93
+ end
94
+ end
95
+
96
+ # Processes the options set by the command-line arguments. In particular,
97
+ # sets `@options[:input]` and `@options[:output]` to appropriate IO streams.
98
+ #
99
+ # This is meant to be overridden by subclasses
100
+ # so they can run their respective programs.
101
+ def process_result
102
+ input, output = @options[:input], @options[:output]
103
+ args = @args.dup
104
+ input ||=
105
+ begin
106
+ filename = args.shift
107
+ @options[:filename] = filename
108
+ open_file(filename) || $stdin
109
+ end
110
+ @options[:output_filename] = args.shift
111
+ output ||= @options[:output_filename] || $stdout
112
+ @options[:input], @options[:output] = input, output
113
+ end
114
+
115
+ COLORS = {:red => 31, :green => 32, :yellow => 33}
116
+
117
+ # Prints a status message about performing the given action,
118
+ # colored using the given color (via terminal escapes) if possible.
119
+ #
120
+ # @param name [#to_s] A short name for the action being performed.
121
+ # Shouldn't be longer than 11 characters.
122
+ # @param color [Symbol] The name of the color to use for this action.
123
+ # Can be `:red`, `:green`, or `:yellow`.
124
+ def puts_action(name, color, arg)
125
+ return if @options[:for_engine][:quiet]
126
+ printf color(color, "%11s %s\n"), name, arg
127
+ STDOUT.flush
128
+ end
129
+
130
+ # Same as `Kernel.puts`, but doesn't print anything if the `--quiet` option is set.
131
+ #
132
+ # @param args [Array] Passed on to `Kernel.puts`
133
+ def puts(*args)
134
+ return if @options[:for_engine][:quiet]
135
+ Kernel.puts(*args)
136
+ end
137
+
138
+ # Wraps the given string in terminal escapes
139
+ # causing it to have the given color.
140
+ # If terminal esapes aren't supported on this platform,
141
+ # just returns the string instead.
142
+ #
143
+ # @param color [Symbol] The name of the color to use.
144
+ # Can be `:red`, `:green`, or `:yellow`.
145
+ # @param str [String] The string to wrap in the given color.
146
+ # @return [String] The wrapped string.
147
+ def color(color, str)
148
+ raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
149
+
150
+ # Almost any real Unix terminal will support color,
151
+ # so we just filter for Windows terms (which don't set TERM)
152
+ # and not-real terminals, which aren't ttys.
153
+ return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
154
+ "\e[#{COLORS[color]}m#{str}\e[0m"
155
+ end
156
+
157
+ def write_output(text, destination)
158
+ if destination.is_a?(String)
159
+ open_file(destination, 'w') {|file| file.write(text)}
160
+ else
161
+ destination.write(text)
162
+ end
163
+ end
164
+
165
+ private
166
+
167
+ def open_file(filename, flag = 'r')
168
+ return if filename.nil?
169
+ flag = 'wb' if @options[:unix_newlines] && flag == 'w'
170
+ file = File.open(filename, flag)
171
+ return file unless block_given?
172
+ yield file
173
+ file.close
174
+ end
175
+
176
+ def handle_load_error(err)
177
+ dep = err.message[/^no such file to load -- (.*)/, 1]
178
+ raise err if @options[:trace] || dep.nil? || dep.empty?
179
+ $stderr.puts <<MESSAGE
180
+ Required dependency #{dep} not found!
181
+ Run "gem install #{dep}" to get it.
182
+ Use --trace for backtrace.
183
+ MESSAGE
184
+ exit 1
185
+ end
186
+ end
187
+ end