sass4 4.0.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 (147) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +13 -0
  3. data/AGENTS.md +534 -0
  4. data/CODE_OF_CONDUCT.md +10 -0
  5. data/CONTRIBUTING.md +148 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +242 -0
  8. data/VERSION +1 -0
  9. data/VERSION_NAME +1 -0
  10. data/bin/sass +13 -0
  11. data/bin/sass-convert +12 -0
  12. data/bin/scss +13 -0
  13. data/extra/sass-spec-ref.sh +40 -0
  14. data/extra/update_watch.rb +13 -0
  15. data/init.rb +18 -0
  16. data/lib/sass/cache_stores/base.rb +88 -0
  17. data/lib/sass/cache_stores/chain.rb +34 -0
  18. data/lib/sass/cache_stores/filesystem.rb +60 -0
  19. data/lib/sass/cache_stores/memory.rb +46 -0
  20. data/lib/sass/cache_stores/null.rb +25 -0
  21. data/lib/sass/cache_stores.rb +15 -0
  22. data/lib/sass/callbacks.rb +67 -0
  23. data/lib/sass/css.rb +407 -0
  24. data/lib/sass/deprecation.rb +55 -0
  25. data/lib/sass/engine.rb +1236 -0
  26. data/lib/sass/environment.rb +236 -0
  27. data/lib/sass/error.rb +198 -0
  28. data/lib/sass/exec/base.rb +188 -0
  29. data/lib/sass/exec/sass_convert.rb +283 -0
  30. data/lib/sass/exec/sass_scss.rb +436 -0
  31. data/lib/sass/exec.rb +9 -0
  32. data/lib/sass/features.rb +48 -0
  33. data/lib/sass/importers/base.rb +182 -0
  34. data/lib/sass/importers/deprecated_path.rb +51 -0
  35. data/lib/sass/importers/filesystem.rb +221 -0
  36. data/lib/sass/importers.rb +23 -0
  37. data/lib/sass/logger/base.rb +47 -0
  38. data/lib/sass/logger/delayed.rb +50 -0
  39. data/lib/sass/logger/log_level.rb +45 -0
  40. data/lib/sass/logger.rb +17 -0
  41. data/lib/sass/media.rb +210 -0
  42. data/lib/sass/plugin/compiler.rb +552 -0
  43. data/lib/sass/plugin/configuration.rb +134 -0
  44. data/lib/sass/plugin/generic.rb +15 -0
  45. data/lib/sass/plugin/merb.rb +48 -0
  46. data/lib/sass/plugin/rack.rb +60 -0
  47. data/lib/sass/plugin/rails.rb +47 -0
  48. data/lib/sass/plugin/staleness_checker.rb +199 -0
  49. data/lib/sass/plugin.rb +134 -0
  50. data/lib/sass/railtie.rb +10 -0
  51. data/lib/sass/repl.rb +57 -0
  52. data/lib/sass/root.rb +7 -0
  53. data/lib/sass/script/css_lexer.rb +33 -0
  54. data/lib/sass/script/css_parser.rb +36 -0
  55. data/lib/sass/script/functions.rb +3103 -0
  56. data/lib/sass/script/lexer.rb +518 -0
  57. data/lib/sass/script/parser.rb +1164 -0
  58. data/lib/sass/script/tree/funcall.rb +314 -0
  59. data/lib/sass/script/tree/interpolation.rb +220 -0
  60. data/lib/sass/script/tree/list_literal.rb +119 -0
  61. data/lib/sass/script/tree/literal.rb +49 -0
  62. data/lib/sass/script/tree/map_literal.rb +64 -0
  63. data/lib/sass/script/tree/node.rb +119 -0
  64. data/lib/sass/script/tree/operation.rb +149 -0
  65. data/lib/sass/script/tree/selector.rb +26 -0
  66. data/lib/sass/script/tree/string_interpolation.rb +125 -0
  67. data/lib/sass/script/tree/unary_operation.rb +69 -0
  68. data/lib/sass/script/tree/variable.rb +57 -0
  69. data/lib/sass/script/tree.rb +16 -0
  70. data/lib/sass/script/value/arg_list.rb +36 -0
  71. data/lib/sass/script/value/base.rb +258 -0
  72. data/lib/sass/script/value/bool.rb +35 -0
  73. data/lib/sass/script/value/callable.rb +25 -0
  74. data/lib/sass/script/value/color.rb +704 -0
  75. data/lib/sass/script/value/function.rb +19 -0
  76. data/lib/sass/script/value/helpers.rb +298 -0
  77. data/lib/sass/script/value/list.rb +135 -0
  78. data/lib/sass/script/value/map.rb +70 -0
  79. data/lib/sass/script/value/null.rb +44 -0
  80. data/lib/sass/script/value/number.rb +564 -0
  81. data/lib/sass/script/value/string.rb +138 -0
  82. data/lib/sass/script/value.rb +13 -0
  83. data/lib/sass/script.rb +66 -0
  84. data/lib/sass/scss/css_parser.rb +61 -0
  85. data/lib/sass/scss/parser.rb +1343 -0
  86. data/lib/sass/scss/rx.rb +134 -0
  87. data/lib/sass/scss/static_parser.rb +351 -0
  88. data/lib/sass/scss.rb +14 -0
  89. data/lib/sass/selector/abstract_sequence.rb +112 -0
  90. data/lib/sass/selector/comma_sequence.rb +195 -0
  91. data/lib/sass/selector/pseudo.rb +291 -0
  92. data/lib/sass/selector/sequence.rb +661 -0
  93. data/lib/sass/selector/simple.rb +124 -0
  94. data/lib/sass/selector/simple_sequence.rb +348 -0
  95. data/lib/sass/selector.rb +327 -0
  96. data/lib/sass/shared.rb +76 -0
  97. data/lib/sass/source/map.rb +209 -0
  98. data/lib/sass/source/position.rb +39 -0
  99. data/lib/sass/source/range.rb +41 -0
  100. data/lib/sass/stack.rb +140 -0
  101. data/lib/sass/supports.rb +225 -0
  102. data/lib/sass/tree/at_root_node.rb +83 -0
  103. data/lib/sass/tree/charset_node.rb +22 -0
  104. data/lib/sass/tree/comment_node.rb +82 -0
  105. data/lib/sass/tree/content_node.rb +9 -0
  106. data/lib/sass/tree/css_import_node.rb +68 -0
  107. data/lib/sass/tree/debug_node.rb +18 -0
  108. data/lib/sass/tree/directive_node.rb +59 -0
  109. data/lib/sass/tree/each_node.rb +24 -0
  110. data/lib/sass/tree/error_node.rb +18 -0
  111. data/lib/sass/tree/extend_node.rb +43 -0
  112. data/lib/sass/tree/for_node.rb +36 -0
  113. data/lib/sass/tree/function_node.rb +44 -0
  114. data/lib/sass/tree/if_node.rb +52 -0
  115. data/lib/sass/tree/import_node.rb +75 -0
  116. data/lib/sass/tree/keyframe_rule_node.rb +15 -0
  117. data/lib/sass/tree/media_node.rb +48 -0
  118. data/lib/sass/tree/mixin_def_node.rb +38 -0
  119. data/lib/sass/tree/mixin_node.rb +52 -0
  120. data/lib/sass/tree/node.rb +240 -0
  121. data/lib/sass/tree/prop_node.rb +162 -0
  122. data/lib/sass/tree/return_node.rb +19 -0
  123. data/lib/sass/tree/root_node.rb +44 -0
  124. data/lib/sass/tree/rule_node.rb +153 -0
  125. data/lib/sass/tree/supports_node.rb +38 -0
  126. data/lib/sass/tree/trace_node.rb +33 -0
  127. data/lib/sass/tree/variable_node.rb +36 -0
  128. data/lib/sass/tree/visitors/base.rb +72 -0
  129. data/lib/sass/tree/visitors/check_nesting.rb +173 -0
  130. data/lib/sass/tree/visitors/convert.rb +350 -0
  131. data/lib/sass/tree/visitors/cssize.rb +362 -0
  132. data/lib/sass/tree/visitors/deep_copy.rb +107 -0
  133. data/lib/sass/tree/visitors/extend.rb +64 -0
  134. data/lib/sass/tree/visitors/perform.rb +572 -0
  135. data/lib/sass/tree/visitors/set_options.rb +139 -0
  136. data/lib/sass/tree/visitors/to_css.rb +440 -0
  137. data/lib/sass/tree/warn_node.rb +18 -0
  138. data/lib/sass/tree/while_node.rb +18 -0
  139. data/lib/sass/util/multibyte_string_scanner.rb +151 -0
  140. data/lib/sass/util/normalized_map.rb +122 -0
  141. data/lib/sass/util/subset_map.rb +109 -0
  142. data/lib/sass/util/test.rb +9 -0
  143. data/lib/sass/util.rb +1137 -0
  144. data/lib/sass/version.rb +120 -0
  145. data/lib/sass.rb +102 -0
  146. data/rails/init.rb +1 -0
  147. metadata +283 -0
@@ -0,0 +1,236 @@
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#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 = @parent.nil? ? Sass::Stack.new : nil
90
+ @caller = nil
91
+ @content = nil
92
+ @filename = nil
93
+ @functions = nil
94
+ @mixins = nil
95
+ @selector = nil
96
+ @vars = nil
97
+ end
98
+
99
+ # Returns whether this is the global environment.
100
+ #
101
+ # @return [Boolean]
102
+ def global?
103
+ @parent.nil?
104
+ end
105
+
106
+ # The environment of the caller of this environment's mixin or function.
107
+ # @return {Environment?}
108
+ def caller
109
+ @caller || (@parent && @parent.caller)
110
+ end
111
+
112
+ # The content passed to this environment. This is naturally only set
113
+ # for mixin body environments with content passed in.
114
+ #
115
+ # @return {[Array<Sass::Tree::Node>, Environment]?} The content nodes and
116
+ # the lexical environment of the content block.
117
+ def content
118
+ @content || (@parent && @parent.content)
119
+ end
120
+
121
+ # The selector for the current CSS rule, or nil if there is no
122
+ # current CSS rule.
123
+ #
124
+ # @return [Selector::CommaSequence?] The current selector, with any
125
+ # nesting fully resolved.
126
+ def selector
127
+ @selector || (@caller && @caller.selector) || (@parent && @parent.selector)
128
+ end
129
+
130
+ # The top-level Environment object.
131
+ #
132
+ # @return [Environment]
133
+ def global_env
134
+ @global_env ||= global? ? self : @parent.global_env
135
+ end
136
+
137
+ # The import/mixin stack.
138
+ #
139
+ # @return [Sass::Stack]
140
+ def stack
141
+ @stack || global_env.stack
142
+ end
143
+ end
144
+
145
+ # The lexical environment for SassScript.
146
+ # This keeps track of variable, mixin, and function definitions.
147
+ #
148
+ # A new environment is created for each level of Sass nesting.
149
+ # This allows variables to be lexically scoped.
150
+ # The new environment refers to the environment in the upper scope,
151
+ # so it has access to variables defined in enclosing scopes,
152
+ # but new variables are defined locally.
153
+ #
154
+ # Environment also keeps track of the {Engine} options
155
+ # so that they can be made available to {Sass::Script::Functions}.
156
+ class Environment < BaseEnvironment
157
+ # The enclosing environment,
158
+ # or nil if this is the global environment.
159
+ #
160
+ # @return [Environment]
161
+ attr_reader :parent
162
+
163
+ # variable
164
+ # Script::Value
165
+ inherited_hash_writer :var
166
+
167
+ # mixin
168
+ # Sass::Callable
169
+ inherited_hash_writer :mixin
170
+
171
+ # function
172
+ # Sass::Callable
173
+ inherited_hash_writer :function
174
+ end
175
+
176
+ # A read-only wrapper for a lexical environment for SassScript.
177
+ class ReadOnlyEnvironment < BaseEnvironment
178
+ def initialize(parent = nil, options = nil)
179
+ super
180
+ @content_cached = nil
181
+ end
182
+ # The read-only environment of the caller of this environment's mixin or function.
183
+ #
184
+ # @see BaseEnvironment#caller
185
+ # @return {ReadOnlyEnvironment}
186
+ def caller
187
+ return @caller if @caller
188
+ env = super
189
+ @caller ||= env.is_a?(ReadOnlyEnvironment) ? env : ReadOnlyEnvironment.new(env, env.options)
190
+ end
191
+
192
+ # The content passed to this environment. If the content's environment isn't already
193
+ # read-only, it's made read-only.
194
+ #
195
+ # @see BaseEnvironment#content
196
+ #
197
+ # @return {[Array<Sass::Tree::Node>, ReadOnlyEnvironment]?} The content nodes and
198
+ # the lexical environment of the content block.
199
+ # Returns `nil` when there is no content in this environment.
200
+ def content
201
+ # Return the cached content from a previous invocation if any
202
+ return @content if @content_cached
203
+ # get the content with a read-write environment from the superclass
204
+ read_write_content = super
205
+ if read_write_content
206
+ tree, env = read_write_content
207
+ # make the content's environment read-only
208
+ if env && !env.is_a?(ReadOnlyEnvironment)
209
+ env = ReadOnlyEnvironment.new(env, env.options)
210
+ end
211
+ @content_cached = true
212
+ @content = [tree, env]
213
+ else
214
+ @content_cached = true
215
+ @content = nil
216
+ end
217
+ end
218
+ end
219
+
220
+ # An environment that can write to in-scope global variables, but doesn't
221
+ # create new variables in the global scope. Useful for top-level control
222
+ # directives.
223
+ class SemiGlobalEnvironment < Environment
224
+ def try_set_var(name, value)
225
+ @vars ||= {}
226
+ if @vars.include?(name)
227
+ @vars[name] = value
228
+ true
229
+ elsif @parent
230
+ @parent.try_set_var(name, value)
231
+ else
232
+ false
233
+ end
234
+ end
235
+ end
236
+ 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 [String]
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 [Integer]
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_backtrace.each_with_index.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 [Integer] 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" + section.each_with_index.
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,188 @@
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
+ begin
18
+ parse
19
+ rescue Exception => e
20
+ # Exit code 65 indicates invalid data per
21
+ # http://www.freebsd.org/cgi/man.cgi?query=sysexits. Setting it via
22
+ # at_exit is a bit of a hack, but it allows us to rethrow when --trace
23
+ # is active and get both the built-in exception formatting and the
24
+ # correct exit code.
25
+ at_exit {exit Sass::Util.windows? ? 13 : 65} if e.is_a?(Sass::SyntaxError)
26
+
27
+ raise e if @options[:trace] || e.is_a?(SystemExit)
28
+
29
+ if e.is_a?(Sass::SyntaxError)
30
+ $stderr.puts e.sass_backtrace_str("standard input")
31
+ else
32
+ $stderr.print "#{e.class}: " unless e.class == RuntimeError
33
+ $stderr.puts e.message.to_s
34
+ end
35
+ $stderr.puts " Use --trace for backtrace."
36
+
37
+ exit 1
38
+ end
39
+ exit 0
40
+ end
41
+
42
+ # Parses the command-line arguments and runs the executable.
43
+ # This does not handle exceptions or exit the program.
44
+ #
45
+ # @see #parse!
46
+ def parse
47
+ @opts = OptionParser.new(&method(:set_opts))
48
+ @opts.parse!(@args)
49
+
50
+ process_result
51
+
52
+ @options
53
+ end
54
+
55
+ # @return [String] A description of the executable
56
+ def to_s
57
+ @opts.to_s
58
+ end
59
+
60
+ protected
61
+
62
+ # Finds the line of the source template
63
+ # on which an exception was raised.
64
+ #
65
+ # @param exception [Exception] The exception
66
+ # @return [String] The line number
67
+ def get_line(exception)
68
+ # SyntaxErrors have weird line reporting
69
+ # when there's trailing whitespace
70
+ if exception.is_a?(::SyntaxError)
71
+ return (exception.message.scan(/:(\d+)/).first || ["??"]).first
72
+ end
73
+ (exception.backtrace[0].scan(/:(\d+)/).first || ["??"]).first
74
+ end
75
+
76
+ # Tells optparse how to parse the arguments
77
+ # available for all executables.
78
+ #
79
+ # This is meant to be overridden by subclasses
80
+ # so they can add their own options.
81
+ #
82
+ # @param opts [OptionParser]
83
+ def set_opts(opts)
84
+ Sass::Util.abstract(this)
85
+ end
86
+
87
+ # Set an option for specifying `Encoding.default_external`.
88
+ #
89
+ # @param opts [OptionParser]
90
+ def encoding_option(opts)
91
+ encoding_desc = 'Specify the default encoding for input files.'
92
+ opts.on('-E', '--default-encoding ENCODING', encoding_desc) do |encoding|
93
+ Encoding.default_external = encoding
94
+ end
95
+ end
96
+
97
+ # Processes the options set by the command-line arguments. In particular,
98
+ # sets `@options[:input]` and `@options[:output]` to appropriate IO streams.
99
+ #
100
+ # This is meant to be overridden by subclasses
101
+ # so they can run their respective programs.
102
+ def process_result
103
+ input, output = @options[:input], @options[:output]
104
+ args = @args.dup
105
+ input ||=
106
+ begin
107
+ filename = args.shift
108
+ @options[:filename] = filename
109
+ open_file(filename) || $stdin
110
+ end
111
+ @options[:output_filename] = args.shift
112
+ output ||= @options[:output_filename] || $stdout
113
+ @options[:input], @options[:output] = input, output
114
+ end
115
+
116
+ COLORS = {:red => 31, :green => 32, :yellow => 33}
117
+
118
+ # Prints a status message about performing the given action,
119
+ # colored using the given color (via terminal escapes) if possible.
120
+ #
121
+ # @param name [#to_s] A short name for the action being performed.
122
+ # Shouldn't be longer than 11 characters.
123
+ # @param color [Symbol] The name of the color to use for this action.
124
+ # Can be `:red`, `:green`, or `:yellow`.
125
+ def puts_action(name, color, arg)
126
+ return if @options[:for_engine][:quiet]
127
+ printf color(color, "%11s %s\n"), name, arg
128
+ STDOUT.flush
129
+ end
130
+
131
+ # Same as `Kernel.puts`, but doesn't print anything if the `--quiet` option is set.
132
+ #
133
+ # @param args [Array] Passed on to `Kernel.puts`
134
+ def puts(*args)
135
+ return if @options[:for_engine][:quiet]
136
+ Kernel.puts(*args)
137
+ end
138
+
139
+ # Wraps the given string in terminal escapes
140
+ # causing it to have the given color.
141
+ # If terminal escapes aren't supported on this platform,
142
+ # just returns the string instead.
143
+ #
144
+ # @param color [Symbol] The name of the color to use.
145
+ # Can be `:red`, `:green`, or `:yellow`.
146
+ # @param str [String] The string to wrap in the given color.
147
+ # @return [String] The wrapped string.
148
+ def color(color, str)
149
+ raise "[BUG] Unrecognized color #{color}" unless COLORS[color]
150
+
151
+ # Almost any real Unix terminal will support color,
152
+ # so we just filter for Windows terms (which don't set TERM)
153
+ # and not-real terminals, which aren't ttys.
154
+ return str if ENV["TERM"].nil? || ENV["TERM"].empty? || !STDOUT.tty?
155
+ "\e[#{COLORS[color]}m#{str}\e[0m"
156
+ end
157
+
158
+ def write_output(text, destination)
159
+ if destination.is_a?(String)
160
+ open_file(destination, 'w') {|file| file.write(text)}
161
+ else
162
+ destination.write(text)
163
+ end
164
+ end
165
+
166
+ private
167
+
168
+ def open_file(filename, flag = 'r')
169
+ return if filename.nil?
170
+ flag = 'wb' if @options[:unix_newlines] && flag == 'w'
171
+ file = File.open(filename, flag)
172
+ return file unless block_given?
173
+ yield file
174
+ file.close
175
+ end
176
+
177
+ def handle_load_error(err)
178
+ dep = err.message[/^no such file to load -- (.*)/, 1]
179
+ raise err if @options[:trace] || dep.nil? || dep.empty?
180
+ $stderr.puts <<MESSAGE
181
+ Required dependency #{dep} not found!
182
+ Run "gem install #{dep}" to get it.
183
+ Use --trace for backtrace.
184
+ MESSAGE
185
+ exit 1
186
+ end
187
+ end
188
+ end