lotus-assets 0.0.0 → 0.1.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.
@@ -0,0 +1,119 @@
1
+ require 'lotus/utils/string'
2
+ require 'lotus/utils/class'
3
+
4
+ module Lotus
5
+ module Assets
6
+ module Compressors
7
+ # Unknown compressor error
8
+ #
9
+ # It's raised when trying to load an unknown compressor.
10
+ #
11
+ # @since 0.1.0
12
+ # @api private
13
+ #
14
+ # @see Lotus::Assets::Configuration#javascript_compressor
15
+ # @see Lotus::Assets::Configuration#stylesheet_compressor
16
+ # @see Lotus::Assets::Compressors::Abstract#for
17
+ class UnknownCompressorError < Error
18
+ # @since 0.1.0
19
+ # @api private
20
+ def initialize(type, engine_name)
21
+ super("Unknown #{ type } compressor: :#{ engine_name }")
22
+ end
23
+ end
24
+
25
+ # Abstract base class for compressors.
26
+ #
27
+ # Don't use this class directly, but please use subclasses instead.
28
+ #
29
+ # @since 0.1.0
30
+ # @api private
31
+ class Abstract
32
+ # Compress the given asset
33
+ #
34
+ # @param filename [String, Pathname] the absolute path to the asset
35
+ #
36
+ # @return [String] the compressed asset
37
+ #
38
+ # @since 0.1.0
39
+ # @api private
40
+ def compress(filename)
41
+ compressor.compress(
42
+ read(filename)
43
+ )
44
+ end
45
+
46
+ protected
47
+ # @since 0.1.0
48
+ # @api private
49
+ attr_reader :compressor
50
+
51
+ # Read the contents of given filename
52
+ #
53
+ # @param filename [String, Pathname] the absolute path to the asset
54
+ #
55
+ # @return [String] the contents of asset
56
+ #
57
+ # @since 0.1.0
58
+ # @api private
59
+ def read(filename)
60
+ ::File.read(filename)
61
+ end
62
+
63
+ private
64
+
65
+ # Factory for compressors.
66
+ #
67
+ # It loads a compressor for the given name.
68
+ #
69
+ # @abstract Please use this method from the subclasses
70
+ #
71
+ # @param engine_name [Symbol,String,NilClass,#compress] the name of the
72
+ # engine to load or an instance of an engine
73
+ #
74
+ # @return [Lotus::Assets::Compressors::Abstract] returns a concrete
75
+ # implementation of a compressor
76
+ #
77
+ # @raise [Lotus::Assets::Compressors::UnknownCompressorError] when the
78
+ # given name refers to an unknown compressor engine
79
+ #
80
+ # @since 0.1.0
81
+ # @api private
82
+ def self.for(engine_name)
83
+ case engine_name
84
+ when Symbol, String
85
+ load_engine(name, engine_name)
86
+ when nil
87
+ require 'lotus/assets/compressors/null_compressor'
88
+ NullCompressor.new
89
+ else
90
+ engine_name
91
+ end
92
+ end
93
+
94
+ # Load the compressor for the given type and engine name.
95
+ #
96
+ # @param type [String] asset type (eg. "Javascript" or "Stylesheet")
97
+ # @param engine_name [Symbol,String] the name of the engine to load (eg. `:yui`)
98
+ #
99
+ # @return [Lotus::Assets::Compress::Abstract] returns a concrete
100
+ # implementation of a compressor
101
+ #
102
+ # @since 0.1.0
103
+ # @api private
104
+ def self.load_engine(type, engine_name)
105
+ type = Utils::String.new(type).demodulize
106
+
107
+ require "lotus/assets/compressors/#{ engine_name }_#{ type.underscore }"
108
+ Utils::Class.load!("#{ Utils::String.new(engine_name).classify }#{ type }", Lotus::Assets::Compressors).new
109
+ rescue LoadError
110
+ raise UnknownCompressorError.new(type, engine_name)
111
+ end
112
+
113
+ class << self
114
+ private :for, :load_engine
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,36 @@
1
+ require 'lotus/assets/compressors/javascript'
2
+ require_relative './jsmin'
3
+
4
+ module Lotus
5
+ module Assets
6
+ module Compressors
7
+ # Builtin compressor for stylesheet
8
+ #
9
+ # This is a port of jsmin
10
+ # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
11
+ #
12
+ # This Ruby port was implemented by Ryan Grove (@rgrove) as work for
13
+ # <tt>jsmin</tt> gem.
14
+ #
15
+ # Copyright (c) 2008-2012 Ryan Grove
16
+ #
17
+ # @since 0.1.0
18
+ # @api private
19
+ #
20
+ # @see https://github.com/sbecker/asset_packager
21
+ class BuiltinJavascript < Javascript
22
+ def initialize
23
+ @compressor = JSMin
24
+ end
25
+
26
+ # @since 0.1.0
27
+ # @api private
28
+ def compress(filename)
29
+ compressor.minify(
30
+ read(filename)
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ require 'lotus/assets/compressors/stylesheet'
2
+
3
+ module Lotus
4
+ module Assets
5
+ module Compressors
6
+ # Builtin compressor for stylesheet
7
+ #
8
+ # This is a basic algorithm based on Scott Becker (@sbecker) work on
9
+ # <tt>asset_packager</tt> gem.
10
+ #
11
+ # Copyright (c) 2006-2008 Scott Becker
12
+ #
13
+ # @since 0.1.0
14
+ # @api private
15
+ #
16
+ # @see https://github.com/sbecker/asset_packager
17
+ class BuiltinStylesheet < Stylesheet
18
+ # @since 0.1.0
19
+ # @api private
20
+ SPACE_REPLACEMENT = " ".freeze
21
+
22
+ # @since 0.1.0
23
+ # @api private
24
+ COMMENTS_REPLACEMENT = "".freeze
25
+
26
+ # @since 0.1.0
27
+ # @api private
28
+ LINE_BREAKS_REPLACEMENT = "}\n".freeze
29
+
30
+ # @since 0.1.0
31
+ # @api private
32
+ LAST_BREAK_REPLACEMENT = "".freeze
33
+
34
+ # @since 0.1.0
35
+ # @api private
36
+ INSIDE_LEFT_BRACKET_REPLACEMENT = " {".freeze
37
+
38
+ # @since 0.1.0
39
+ # @api private
40
+ INSIDE_RIGHT_BRACKET_REPLACEMENT = "}".freeze
41
+
42
+ # @since 0.1.0
43
+ # @api private
44
+ def compress(filename)
45
+ result = read(filename)
46
+ result.gsub!(/\s+/, SPACE_REPLACEMENT) # collapse space
47
+ result.gsub!(/\/\*(.*?)\*\/ /, COMMENTS_REPLACEMENT) # remove comments - caution, might want to remove this if using css hacks
48
+ result.gsub!(/\} /, LINE_BREAKS_REPLACEMENT) # add line breaks
49
+ result.gsub!(/\n$/, LAST_BREAK_REPLACEMENT) # remove last break
50
+ result.gsub!(/ \{ /, INSIDE_LEFT_BRACKET_REPLACEMENT) # trim inside brackets
51
+ result.gsub!(/; \}/, INSIDE_RIGHT_BRACKET_REPLACEMENT) # trim inside brackets
52
+ result
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,25 @@
1
+ require 'lotus/assets/compressors/javascript'
2
+ require 'closure-compiler'
3
+
4
+ module Lotus
5
+ module Assets
6
+ module Compressors
7
+ # Google Closure Compiler for JavaScript
8
+ #
9
+ # Depends on <tt>closure-compiler</tt> gem
10
+ #
11
+ # @see https://developers.google.com/closure/compiler
12
+ # @see https://rubygems.org/gems/closure-compiler
13
+ #
14
+ # @since 0.1.0
15
+ # @api private
16
+ class ClosureJavascript < Javascript
17
+ # @since 0.1.0
18
+ # @api private
19
+ def initialize
20
+ @compressor = Closure::Compiler.new
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,77 @@
1
+ require 'lotus/assets/compressors/abstract'
2
+
3
+ module Lotus
4
+ module Assets
5
+ module Compressors
6
+ # Base class for JavaScript compressors
7
+ #
8
+ # @since 0.1.0
9
+ # @api private
10
+ class Javascript < Abstract
11
+ # Factory for Javascript compressors.
12
+ #
13
+ # It loads a compressor for the given name.
14
+ #
15
+ # @param engine_name [Symbol,String,NilClass,#compress] the name of the
16
+ # engine to load or an instance of an engine
17
+ #
18
+ # @return [Lotus::Assets::Compressors::Abstract] returns a concrete
19
+ # implementation of a compressor
20
+ #
21
+ # @raise [Lotus::Assets::Compressors::UnknownCompressorError] when the
22
+ # given name refers to an unknown compressor engine
23
+ #
24
+ # @since 0.1.0
25
+ # @api private
26
+ #
27
+ # @see Lotus::Assets::Compressors::Abstract#for
28
+ # @see Lotus::Assets::Configuration#javascript_compressor
29
+ #
30
+ # @example Basic Usage
31
+ # require 'lotus/assets'
32
+ # require 'lotus/assets/compressors/javascript'
33
+ #
34
+ # Lotus::Assets::Compressors::Javascript.for(:closure)
35
+ # # => #<Lotus::Assets::Compressors::ClosureJavascript:0x007fa32a32e108 ...>
36
+ #
37
+ # @example Null Compressor
38
+ # require 'lotus/assets'
39
+ # require 'lotus/assets/compressors/javascript'
40
+ #
41
+ # Lotus::Assets::Compressors::Javascript.for(nil)
42
+ # # => #<Lotus::Assets::Compressors::NullCompressor:0x007fa32a314258>
43
+ #
44
+ # @example Custom Compressor
45
+ # require 'lotus/assets'
46
+ # require 'lotus/assets/compressors/javascript'
47
+ #
48
+ # class CustomJavascriptCompressor
49
+ # def compress(filename)
50
+ # # ...
51
+ # end
52
+ # end
53
+ #
54
+ # Lotus::Assets::Compressors::Javascript.for(CustomJavascriptCompressor.new)
55
+ # # => #<CustomJavascriptCompressor:0x007fa32a2cdf10>
56
+ #
57
+ # @example Third Party Compressor
58
+ # require 'lotus/assets'
59
+ # require 'lotus/assets/compressors/javascript'
60
+ # require 'lotus/foo/compressor' # third party gem
61
+ #
62
+ # Lotus::Assets::Compressors::Javascript.for(:foo)
63
+ # # => #<Lotus::Assets::Compressors::FooJavascript:0x007fa3dd9ed968>
64
+ #
65
+ # @example Unknown Engine
66
+ # require 'lotus/assets'
67
+ # require 'lotus/assets/compressors/javascript'
68
+ #
69
+ # Lotus::Assets::Compressors::Javascript.for(:wat)
70
+ # # => Lotus::Assets::Compressors::UnknownCompressorError: Unknown Javascript compressor: :wat
71
+ def self.for(engine_name)
72
+ super
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,283 @@
1
+ #--
2
+ # jsmin.rb - Ruby implementation of Douglas Crockford's JSMin.
3
+ #
4
+ # This port of jsmin.c was made by Ryan Grove (@rgrove) as work for <tt>jsmin</tt> gem.
5
+ # Copyright (c) 2008-2012 Ryan Grove
6
+ #
7
+ # This is a port of jsmin.c, and is distributed under the same terms, which are
8
+ # as follows:
9
+ #
10
+ # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
11
+ #
12
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ # of this software and associated documentation files (the "Software"), to deal
14
+ # in the Software without restriction, including without limitation the rights
15
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ # copies of the Software, and to permit persons to whom the Software is
17
+ # furnished to do so, subject to the following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included in all
20
+ # copies or substantial portions of the Software.
21
+ #
22
+ # The Software shall be used for Good, not Evil.
23
+ #
24
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ # SOFTWARE.
31
+ #++
32
+
33
+ require 'strscan'
34
+
35
+ # = JSMin
36
+ #
37
+ # Ruby implementation of Douglas Crockford's JavaScript minifier, JSMin.
38
+ #
39
+ # *Author*:: Ryan Grove (mailto:ryan@wonko.com)
40
+ # *Version*:: 1.0.1 (2008-11-10)
41
+ # *Copyright*:: Copyright (c) 2008 Ryan Grove. All rights reserved.
42
+ # *Website*:: http://github.com/rgrove/jsmin
43
+ #
44
+ # == Example
45
+ #
46
+ # require 'rubygems'
47
+ # require 'jsmin'
48
+ #
49
+ # File.open('example.js', 'r') {|file| puts JSMin.minify(file) }
50
+ #
51
+ module JSMin
52
+ CHR_APOS = "'".freeze
53
+ CHR_ASTERISK = '*'.freeze
54
+ CHR_BACKSLASH = '\\'.freeze
55
+ CHR_CR = "\r".freeze
56
+ CHR_FRONTSLASH = '/'.freeze
57
+ CHR_LF = "\n".freeze
58
+ CHR_QUOTE = '"'.freeze
59
+ CHR_SPACE = ' '.freeze
60
+
61
+ ORD_LF = ?\n
62
+ ORD_SPACE = ?\
63
+ ORD_TILDE = ?~
64
+
65
+ class ParseError < RuntimeError
66
+ attr_accessor :source, :line
67
+ def initialize(err, source, line)
68
+ @source = source,
69
+ @line = line
70
+ super "JSMin Parse Error: #{err} at line #{line} of #{source}"
71
+ end
72
+ end
73
+
74
+ class << self
75
+ def raise(err)
76
+ super ParseError.new(err, @source, @line)
77
+ end
78
+
79
+ # Reads JavaScript from _input_ (which can be a String or an IO object) and
80
+ # returns a String containing minified JS.
81
+ def minify(input)
82
+ @js = StringScanner.new(input.is_a?(IO) ? input.read : input.to_s)
83
+ @source = input.is_a?(IO) ? input.inspect : input.to_s[0..100]
84
+ @line = 1
85
+
86
+ @a = "\n"
87
+ @b = nil
88
+ @lookahead = nil
89
+ @output = ''
90
+
91
+ action_get
92
+
93
+ while !@a.nil? do
94
+ case @a
95
+ when CHR_SPACE
96
+ if alphanum?(@b)
97
+ action_output
98
+ else
99
+ action_copy
100
+ end
101
+
102
+ when CHR_LF
103
+ if @b == CHR_SPACE
104
+ action_get
105
+ elsif @b =~ /[{\[\(+-]/
106
+ action_output
107
+ else
108
+ if alphanum?(@b)
109
+ action_output
110
+ else
111
+ action_copy
112
+ end
113
+ end
114
+
115
+ else
116
+ if @b == CHR_SPACE
117
+ if alphanum?(@a)
118
+ action_output
119
+ else
120
+ action_get
121
+ end
122
+ elsif @b == CHR_LF
123
+ if @a =~ /[}\]\)\\"+-]/
124
+ action_output
125
+ else
126
+ if alphanum?(@a)
127
+ action_output
128
+ else
129
+ action_get
130
+ end
131
+ end
132
+ else
133
+ action_output
134
+ end
135
+ end
136
+ end
137
+
138
+ @output
139
+ end
140
+
141
+ private
142
+
143
+ # Corresponds to action(1) in jsmin.c.
144
+ def action_output
145
+ @output << @a
146
+ action_copy
147
+ end
148
+
149
+ # Corresponds to action(2) in jsmin.c.
150
+ def action_copy
151
+ @a = @b
152
+
153
+ if @a == CHR_APOS || @a == CHR_QUOTE
154
+ loop do
155
+ @output << @a
156
+ @a = get
157
+
158
+ break if @a == @b
159
+
160
+ if @a[0] <= ORD_LF
161
+ raise "unterminated string literal: #{@a.inspect}"
162
+ end
163
+
164
+ if @a == CHR_BACKSLASH
165
+ @output << @a
166
+ @a = get
167
+
168
+ if @a[0] <= ORD_LF
169
+ raise "unterminated string literal: #{@a.inspect}"
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ action_get
176
+ end
177
+
178
+ # Corresponds to action(3) in jsmin.c.
179
+ def action_get
180
+ @b = nextchar
181
+
182
+ if @b == CHR_FRONTSLASH && (@a == CHR_LF || @a =~ /[\(,=:\[!&|?{};]/)
183
+ @output << @a
184
+ @output << @b
185
+
186
+ loop do
187
+ @a = get
188
+
189
+ # Inside a regex [...] set, which MAY contain a '/' itself.
190
+ # Example:
191
+ # mootools Form.Validator near line 460:
192
+ # return Form.Validator.getValidator('IsEmpty').test(element) || (/^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]\.?){0,63}[a-z0-9!#$%&'*+/=?^_`{|}~-]@(?:(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\])$/i).test(element.get('value'));
193
+ if @a == '['
194
+ loop do
195
+ @output << @a
196
+ @a = get
197
+ case @a
198
+ when ']' then break
199
+ when CHR_BACKSLASH then
200
+ @output << @a
201
+ @a = get
202
+ when @a[0] <= ORD_LF
203
+ raise "JSMin parse error: unterminated regular expression " +
204
+ "literal: #{@a}"
205
+ end
206
+ end
207
+ elsif @a == CHR_FRONTSLASH
208
+ break
209
+ elsif @a == CHR_BACKSLASH
210
+ @output << @a
211
+ @a = get
212
+ elsif @a[0] <= ORD_LF
213
+ raise "unterminated regular expression : #{@a.inspect}"
214
+ end
215
+
216
+ @output << @a
217
+ end
218
+
219
+ @b = nextchar
220
+ end
221
+ end
222
+
223
+ # Returns true if +c+ is a letter, digit, underscore, dollar sign,
224
+ # backslash, or non-ASCII character.
225
+ def alphanum?(c)
226
+ c.is_a?(String) && !c.empty? && (c[0] > ORD_TILDE || c =~ /[0-9a-z_$\\]/i)
227
+ end
228
+
229
+ # Returns the next character from the input. If the character is a control
230
+ # character, it will be translated to a space or linefeed.
231
+ def get
232
+ if @lookahead
233
+ c = @lookahead
234
+ @lookahead = nil
235
+ else
236
+ c = @js.getch
237
+ if c == CHR_LF || c == CHR_CR
238
+ @line += 1
239
+ return CHR_LF
240
+ end
241
+ return ' ' unless c.nil? || c[0] >= ORD_SPACE
242
+ end
243
+ c
244
+ end
245
+
246
+ # Gets the next character, excluding comments.
247
+ def nextchar
248
+ c = get
249
+ return c unless c == CHR_FRONTSLASH
250
+
251
+ case peek
252
+ when CHR_FRONTSLASH
253
+ loop do
254
+ c = get
255
+ return c if c[0] <= ORD_LF
256
+ end
257
+
258
+ when CHR_ASTERISK
259
+ get
260
+ loop do
261
+ case get
262
+ when CHR_ASTERISK
263
+ if peek == CHR_FRONTSLASH
264
+ get
265
+ return ' '
266
+ end
267
+
268
+ when nil
269
+ raise 'unterminated comment'
270
+ end
271
+ end
272
+
273
+ else
274
+ return c
275
+ end
276
+ end
277
+
278
+ # Gets the next character without getting it.
279
+ def peek
280
+ @lookahead = get
281
+ end
282
+ end
283
+ end