ambethia-jsmin 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +9 -0
- data/bin/jsmin +20 -0
- data/lib/jsmin.rb +249 -0
- metadata +55 -0
data/HISTORY
ADDED
data/bin/jsmin
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'jsmin'
|
5
|
+
|
6
|
+
source_file = ARGV[0]
|
7
|
+
|
8
|
+
if source_file.nil?
|
9
|
+
puts "Usage: jsmin <source file> [destination file]"
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
destination_file = $2 || "#{source_file}.minified"
|
14
|
+
|
15
|
+
if !File.exist?(source_file)
|
16
|
+
puts "Could not locate source file: #{source_file}"
|
17
|
+
exit 1;
|
18
|
+
end
|
19
|
+
|
20
|
+
File.open(destination_file, 'w') {|file| file << JSMin.minify(File.read(source_file)) }
|
data/lib/jsmin.rb
ADDED
@@ -0,0 +1,249 @@
|
|
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.1 (2008-11-10)
|
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
|
+
CHR_APOS = "'".freeze
|
50
|
+
CHR_ASTERISK = '*'.freeze
|
51
|
+
CHR_BACKSLASH = '\\'.freeze
|
52
|
+
CHR_CR = "\r".freeze
|
53
|
+
CHR_FRONTSLASH = '/'.freeze
|
54
|
+
CHR_LF = "\n".freeze
|
55
|
+
CHR_QUOTE = '"'.freeze
|
56
|
+
CHR_SPACE = ' '.freeze
|
57
|
+
|
58
|
+
if RUBY_VERSION >= '1.9'
|
59
|
+
ORD_LF = "\n".freeze
|
60
|
+
ORD_SPACE = ' '.freeze
|
61
|
+
ORD_TILDE = '~'.freeze
|
62
|
+
else
|
63
|
+
ORD_LF = "\n"[0].freeze
|
64
|
+
ORD_SPACE = ' '[0].freeze
|
65
|
+
ORD_TILDE = '~'[0].freeze
|
66
|
+
end
|
67
|
+
|
68
|
+
class << self
|
69
|
+
|
70
|
+
# Reads JavaScript from _input_ (which can be a String or an IO object) and
|
71
|
+
# returns a String containing minified JS.
|
72
|
+
def minify(input)
|
73
|
+
@js = StringScanner.new(input.is_a?(IO) ? input.read : input.to_s)
|
74
|
+
|
75
|
+
@a = "\n"
|
76
|
+
@b = nil
|
77
|
+
@lookahead = nil
|
78
|
+
@output = ''
|
79
|
+
|
80
|
+
action_get
|
81
|
+
|
82
|
+
while !@a.nil? do
|
83
|
+
case @a
|
84
|
+
when CHR_SPACE
|
85
|
+
if alphanum?(@b)
|
86
|
+
action_output
|
87
|
+
else
|
88
|
+
action_copy
|
89
|
+
end
|
90
|
+
|
91
|
+
when CHR_LF
|
92
|
+
if @b == CHR_SPACE
|
93
|
+
action_get
|
94
|
+
elsif @b =~ /[{\[\(+-]/
|
95
|
+
action_output
|
96
|
+
else
|
97
|
+
if alphanum?(@b)
|
98
|
+
action_output
|
99
|
+
else
|
100
|
+
action_copy
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
else
|
105
|
+
if @b == CHR_SPACE
|
106
|
+
if alphanum?(@a)
|
107
|
+
action_output
|
108
|
+
else
|
109
|
+
action_get
|
110
|
+
end
|
111
|
+
elsif @b == CHR_LF
|
112
|
+
if @a =~ /[}\]\)\\"+-]/
|
113
|
+
action_output
|
114
|
+
else
|
115
|
+
if alphanum?(@a)
|
116
|
+
action_output
|
117
|
+
else
|
118
|
+
action_get
|
119
|
+
end
|
120
|
+
end
|
121
|
+
else
|
122
|
+
action_output
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
@output
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
# Corresponds to action(1) in jsmin.c.
|
133
|
+
def action_output
|
134
|
+
@output << @a
|
135
|
+
action_copy
|
136
|
+
end
|
137
|
+
|
138
|
+
# Corresponds to action(2) in jsmin.c.
|
139
|
+
def action_copy
|
140
|
+
@a = @b
|
141
|
+
|
142
|
+
if @a == CHR_APOS || @a == CHR_QUOTE
|
143
|
+
loop do
|
144
|
+
@output << @a
|
145
|
+
@a = get
|
146
|
+
|
147
|
+
break if @a == @b
|
148
|
+
|
149
|
+
if @a[0] <= ORD_LF
|
150
|
+
raise "JSMin parse error: unterminated string literal: #{@a}"
|
151
|
+
end
|
152
|
+
|
153
|
+
if @a == CHR_BACKSLASH
|
154
|
+
@output << @a
|
155
|
+
@a = get
|
156
|
+
|
157
|
+
if @a[0] <= ORD_LF
|
158
|
+
raise "JSMin parse error: unterminated string literal: #{@a}"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
action_get
|
165
|
+
end
|
166
|
+
|
167
|
+
# Corresponds to action(3) in jsmin.c.
|
168
|
+
def action_get
|
169
|
+
@b = nextchar
|
170
|
+
|
171
|
+
if @b == CHR_FRONTSLASH && (@a == CHR_LF || @a =~ /[\(,=:\[!&|?{};]/)
|
172
|
+
@output << @a
|
173
|
+
@output << @b
|
174
|
+
|
175
|
+
loop do
|
176
|
+
@a = get
|
177
|
+
|
178
|
+
if @a == CHR_FRONTSLASH
|
179
|
+
break
|
180
|
+
elsif @a == CHR_BACKSLASH
|
181
|
+
@output << @a
|
182
|
+
@a = get
|
183
|
+
elsif @a[0] <= ORD_LF
|
184
|
+
raise "JSMin parse error: unterminated regular expression " +
|
185
|
+
"literal: #{@a}"
|
186
|
+
end
|
187
|
+
|
188
|
+
@output << @a
|
189
|
+
end
|
190
|
+
|
191
|
+
@b = nextchar
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
# Returns true if +c+ is a letter, digit, underscore, dollar sign,
|
196
|
+
# backslash, or non-ASCII character.
|
197
|
+
def alphanum?(c)
|
198
|
+
c.is_a?(String) && !c.empty? && (c[0] > ORD_TILDE || c =~ /[0-9a-z_$\\]/i)
|
199
|
+
end
|
200
|
+
|
201
|
+
# Returns the next character from the input. If the character is a control
|
202
|
+
# character, it will be translated to a space or linefeed.
|
203
|
+
def get
|
204
|
+
c = @lookahead.nil? ? @js.getch : @lookahead
|
205
|
+
@lookahead = nil
|
206
|
+
|
207
|
+
return c if c.nil? || c == CHR_LF || c[0] >= ORD_SPACE
|
208
|
+
return "\n" if c == CHR_CR
|
209
|
+
return ' '
|
210
|
+
end
|
211
|
+
|
212
|
+
# Gets the next character, excluding comments.
|
213
|
+
def nextchar
|
214
|
+
c = get
|
215
|
+
return c unless c == CHR_FRONTSLASH
|
216
|
+
|
217
|
+
case peek
|
218
|
+
when CHR_FRONTSLASH
|
219
|
+
loop do
|
220
|
+
c = get
|
221
|
+
return c if c[0] <= ORD_LF
|
222
|
+
end
|
223
|
+
|
224
|
+
when CHR_ASTERISK
|
225
|
+
get
|
226
|
+
loop do
|
227
|
+
case get
|
228
|
+
when CHR_ASTERISK
|
229
|
+
if peek == CHR_FRONTSLASH
|
230
|
+
get
|
231
|
+
return ' '
|
232
|
+
end
|
233
|
+
|
234
|
+
when nil
|
235
|
+
raise 'JSMin parse error: unterminated comment'
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
else
|
240
|
+
return c
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# Gets the next character without getting it.
|
245
|
+
def peek
|
246
|
+
@lookahead = get
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ambethia-jsmin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Grove
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-20 00:00:00 -08:00
|
13
|
+
default_executable: jsmin
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: ryan@wonko.com
|
18
|
+
executables:
|
19
|
+
- jsmin
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- HISTORY
|
26
|
+
- lib/jsmin.rb
|
27
|
+
- bin/jsmin
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/rgrove/jsmin/
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 1.8.6
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.2.0
|
51
|
+
signing_key:
|
52
|
+
specification_version: 2
|
53
|
+
summary: Ruby implementation of Douglas Crockford's JSMin JavaScript minifier.
|
54
|
+
test_files: []
|
55
|
+
|