jsmin 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/HISTORY +5 -0
  2. data/lib/jsmin.rb +233 -0
  3. metadata +54 -0
data/HISTORY ADDED
@@ -0,0 +1,5 @@
1
+ CSSMin History
2
+ ================================================================================
3
+
4
+ Version 1.0.0 (2008-03-22)
5
+ * First release.
@@ -0,0 +1,233 @@
1
+ #--
2
+ # jsmin.rb - Ruby implementation of Douglas Crockford's JSMin.
3
+ #
4
+ # This is a port of jsmin.c, and is distributed under the same terms, which are
5
+ # as follows:
6
+ #
7
+ # Copyright (c) 2002 Douglas Crockford (www.crockford.com)
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in all
17
+ # copies or substantial portions of the Software.
18
+ #
19
+ # The Software shall be used for Good, not Evil.
20
+ #
21
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ # SOFTWARE.
28
+ #++
29
+
30
+ require 'strscan'
31
+
32
+ # = JSMin
33
+ #
34
+ # Ruby implementation of Douglas Crockford's JavaScript minifier, JSMin.
35
+ #
36
+ # Author:: Ryan Grove (mailto:ryan@wonko.com)
37
+ # Version:: 1.0.0 (2008-03-22)
38
+ # Copyright:: Copyright (c) 2008 Ryan Grove. All rights reserved.
39
+ # Website:: http://github.com/rgrove/jsmin/
40
+ #
41
+ # == Example
42
+ #
43
+ # require 'rubygems'
44
+ # require 'jsmin'
45
+ #
46
+ # File.open('example.js', 'r') {|file| puts JSMin.minify(file) }
47
+ #
48
+ module JSMin
49
+ ORD_LF = "\n"[0].freeze
50
+ ORD_SPACE = ' '[0].freeze
51
+
52
+ class << self
53
+
54
+ # Reads JavaScript from +input+ (which can be a String or an IO object) and
55
+ # returns a String containing minified JS.
56
+ def minify(input)
57
+ @js = StringScanner.new(input.is_a?(IO) ? input.read : input.to_s)
58
+
59
+ @a = "\n"
60
+ @b = nil
61
+ @lookahead = nil
62
+ @output = ''
63
+
64
+ action_get
65
+
66
+ while !@a.nil? do
67
+ case @a
68
+ when ' '
69
+ if alphanum?(@b)
70
+ action_output
71
+ else
72
+ action_copy
73
+ end
74
+
75
+ when "\n"
76
+ if @b == ' '
77
+ action_get
78
+ elsif @b =~ /[{\[\(+-]/
79
+ action_output
80
+ else
81
+ if alphanum?(@b)
82
+ action_output
83
+ else
84
+ action_copy
85
+ end
86
+ end
87
+
88
+ else
89
+ if @b == ' '
90
+ if alphanum?(@a)
91
+ action_output
92
+ else
93
+ action_get
94
+ end
95
+ elsif @b == "\n"
96
+ if @a =~ /[}\]\)\\"+-]/
97
+ action_output
98
+ else
99
+ if alphanum?(@a)
100
+ action_output
101
+ else
102
+ action_get
103
+ end
104
+ end
105
+ else
106
+ action_output
107
+ end
108
+ end
109
+ end
110
+
111
+ @output
112
+ end
113
+
114
+ private
115
+
116
+ # Corresponds to action(1) in jsmin.c.
117
+ def action_output
118
+ @output << @a
119
+ action_copy
120
+ end
121
+
122
+ # Corresponds to action(2) in jsmin.c.
123
+ def action_copy
124
+ @a = @b
125
+
126
+ if @a == '\'' || @a == '"'
127
+ loop do
128
+ @output << @a
129
+ @a = get
130
+
131
+ break if @a == @b
132
+
133
+ if @a[0] <= ORD_LF
134
+ raise "JSMin parse error: unterminated string literal: #{@a}"
135
+ end
136
+
137
+ if @a == '\\'
138
+ @output << @a
139
+ @a = get
140
+
141
+ if @a[0] <= ORD_LF
142
+ raise "JSMin parse error: unterminated string literal: #{@a}"
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ action_get
149
+ end
150
+
151
+ # Corresponds to action(3) in jsmin.c.
152
+ def action_get
153
+ @b = nextchar
154
+
155
+ if @b == '/' && (@a == "\n" || @a =~ /[\(,=:\[!&|?{};]/)
156
+ @output << @a
157
+ @output << @b
158
+
159
+ loop do
160
+ @a = get
161
+
162
+ if @a == '/'
163
+ break
164
+ elsif @a == '\\'
165
+ @output << @a
166
+ @a = get
167
+ elsif @a[0] <= ORD_LF
168
+ raise "JSMin parse error: unterminated regular expression " +
169
+ "literal: #{@a}"
170
+ end
171
+
172
+ @output << @a
173
+ end
174
+
175
+ @b = nextchar
176
+ end
177
+ end
178
+
179
+ # Returns true if +c+ is a letter, digit, underscore, dollar sign,
180
+ # backslash, or non-ASCII character.
181
+ def alphanum?(c)
182
+ c.is_a?(String) && !c.empty? && (c[0] > 126 || c =~ /[0-9a-z_$\\]/i)
183
+ end
184
+
185
+ # Returns the next character from the input. If the character is a control
186
+ # character, it will be translated to a space or linefeed.
187
+ def get
188
+ c = @lookahead.nil? ? @js.getch : @lookahead
189
+ @lookahead = nil
190
+
191
+ return c if c.nil? || c == "\n" || c[0] >= ORD_SPACE
192
+ return "\n" if c == "\r"
193
+ return ' '
194
+ end
195
+
196
+ # Gets the next character, excluding comments.
197
+ def nextchar
198
+ c = get
199
+ return c unless c == '/'
200
+
201
+ case peek
202
+ when '/'
203
+ loop do
204
+ c = get
205
+ return c if c[0] <= ORD_LF
206
+ end
207
+
208
+ when '*'
209
+ get
210
+ loop do
211
+ case get
212
+ when '*'
213
+ if peek == '/'
214
+ get
215
+ return ' '
216
+ end
217
+
218
+ when nil
219
+ raise 'JSMin parse error: unterminated comment'
220
+ end
221
+ end
222
+
223
+ else
224
+ return c
225
+ end
226
+ end
227
+
228
+ # Gets the next character without getting it.
229
+ def peek
230
+ @lookahead = get
231
+ end
232
+ end
233
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsmin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Grove
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ryan@wonko.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/jsmin.rb
26
+ - HISTORY
27
+ has_rdoc: true
28
+ homepage: http://github.com/rgrove/jsmin/
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 1.8.6
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project: riposte
49
+ rubygems_version: 1.0.1
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Ruby implementation of Douglas Crockford's JSMin JavaScript minifier.
53
+ test_files: []
54
+