jsmin_c 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/License.txt +23 -0
  2. data/README.rdoc +63 -0
  3. data/extconf.rb +5 -0
  4. data/jsmin_c.c +315 -0
  5. data/jsmin_c.gemspec +32 -0
  6. data/test_jsmin.rb +48 -0
  7. metadata +73 -0
data/License.txt ADDED
@@ -0,0 +1,23 @@
1
+
2
+ Copyright (c) 2002 Douglas Crockford (www.crockford.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ this software and associated documentation files (the "Software"), to deal in
6
+ the Software without restriction, including without limitation the rights to
7
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8
+ of the Software, and to permit persons to whom the Software is furnished to do
9
+ so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ The Software shall be used for Good, not Evil.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+
data/README.rdoc ADDED
@@ -0,0 +1,63 @@
1
+
2
+
3
+ = Riassence JSMin Wrapper
4
+
5
+ * http://rsence.org/
6
+
7
+
8
+ == Description:
9
+
10
+ This is a ruby -> c wrapper extension for Douglas Crockford's awesome jsmin.
11
+ It's just as fast as the original (orders of magnitude faster than the pure ruby version in the 'jsmin' gem).
12
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
13
+
14
+ == Known issues:
15
+
16
+ Don't use the (slow) pure ruby version of jsmin in combination with jsmin_c
17
+
18
+
19
+ == Usage:
20
+
21
+ require 'jsmin_c'
22
+
23
+ # Makes a JSMin instance that removes white space from js source.
24
+ jsmin = JSMin.new
25
+ js1 = File.read( 'big_js1.js' )
26
+ minimized_js1 = jsmin.minimize( js1 )
27
+ js2 = File.read( 'big_js2.js' )
28
+ minimized_js2 = jsmin.minimize( js2 )
29
+
30
+
31
+ == Install:
32
+
33
+ * sudo gem install jsmin
34
+
35
+ == License:
36
+
37
+ Ruby extension by: Domen Puncer <domen@cba.si>
38
+
39
+ jsmin.c
40
+ 2008-08-03
41
+
42
+ Copyright (c) 2002 Douglas Crockford (www.crockford.com)
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
45
+ this software and associated documentation files (the "Software"), to deal in
46
+ the Software without restriction, including without limitation the rights to
47
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
48
+ of the Software, and to permit persons to whom the Software is furnished to do
49
+ so, subject to the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be included in all
52
+ copies or substantial portions of the Software.
53
+
54
+ The Software shall be used for Good, not Evil.
55
+
56
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
57
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
58
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
59
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
60
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
61
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62
+ SOFTWARE.
63
+
data/extconf.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'mkmf'
2
+ create_makefile('jsmin_c')
3
+ system('make clean')
4
+ system('make all')
5
+ require 'test_jsmin'
data/jsmin_c.c ADDED
@@ -0,0 +1,315 @@
1
+ /* jsmin.c
2
+ 2008-08-03
3
+
4
+ Copyright (c) 2002 Douglas Crockford (www.crockford.com)
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
7
+ this software and associated documentation files (the "Software"), to deal in
8
+ the Software without restriction, including without limitation the rights to
9
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10
+ of the Software, and to permit persons to whom the Software is furnished to do
11
+ so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ The Software shall be used for Good, not Evil.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ */
26
+
27
+ /*
28
+ * Adapted to be used from Ruby by Domen Puncer <domen@cba.si>
29
+ */
30
+
31
+ #include <stdlib.h>
32
+ #include <stdio.h>
33
+ #include "ruby.h"
34
+
35
+ static int theA;
36
+ static int theB;
37
+ static int theLookahead = EOF;
38
+
39
+ static const char *src;
40
+ static int src_len;
41
+ static char *dest;
42
+ static int srci, desti;
43
+
44
+ /* isAlphanum -- return true if the character is a letter, digit, underscore,
45
+ dollar sign, or non-ASCII character.
46
+ */
47
+
48
+ static int
49
+ isAlphanum(int c)
50
+ {
51
+ return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
52
+ (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
53
+ c > 126);
54
+ }
55
+
56
+
57
+ /* get -- return the next character from stdin. Watch out for lookahead. If
58
+ the character is a control character, translate it to a space or
59
+ linefeed.
60
+ */
61
+
62
+ static int
63
+ get()
64
+ {
65
+ int c = theLookahead;
66
+ theLookahead = EOF;
67
+ if (c == EOF) {
68
+ if (srci < src_len)
69
+ c = src[srci++];
70
+ else
71
+ c = EOF;
72
+ }
73
+ if (c >= ' ' || c == '\n' || c == EOF) {
74
+ return c;
75
+ }
76
+ if (c == '\r') {
77
+ return '\n';
78
+ }
79
+ return ' ';
80
+ }
81
+
82
+
83
+ /* peek -- get the next character without getting it.
84
+ */
85
+
86
+ static int
87
+ peek()
88
+ {
89
+ theLookahead = get();
90
+ return theLookahead;
91
+ }
92
+
93
+
94
+ /* next -- get the next character, excluding comments. peek() is used to see
95
+ if a '/' is followed by a '/' or '*'.
96
+ */
97
+
98
+ static int
99
+ next()
100
+ {
101
+ int c = get();
102
+ if (c == '/') {
103
+ switch (peek()) {
104
+ case '/':
105
+ for (;;) {
106
+ c = get();
107
+ if (c <= '\n') {
108
+ return c;
109
+ }
110
+ }
111
+ case '*':
112
+ get();
113
+ for (;;) {
114
+ switch (get()) {
115
+ case '*':
116
+ if (peek() == '/') {
117
+ get();
118
+ return ' ';
119
+ }
120
+ break;
121
+ case EOF:
122
+ rb_raise(rb_eException, "JSMIN Unterminated comment.");
123
+ }
124
+ }
125
+ default:
126
+ return c;
127
+ }
128
+ }
129
+ return c;
130
+ }
131
+
132
+
133
+ static inline void put_to_dest(int c)
134
+ {
135
+ /* this should be impossible? */
136
+ if (desti < src_len)
137
+ dest[desti++] = c;
138
+ else
139
+ rb_raise(rb_eIndexError, "JSMIN target file is larger than source.");
140
+ }
141
+
142
+ /* action -- do something! What you do is determined by the argument:
143
+ 1 Output A. Copy B to A. Get the next B.
144
+ 2 Copy B to A. Get the next B. (Delete A).
145
+ 3 Get the next B. (Delete B).
146
+ action treats a string as a single character. Wow!
147
+ action recognizes a regular expression if it is preceded by ( or , or =.
148
+ */
149
+
150
+ static void
151
+ action(int d)
152
+ {
153
+ switch (d) {
154
+ case 1:
155
+ put_to_dest(theA);
156
+ case 2:
157
+ theA = theB;
158
+ if (theA == '\'' || theA == '"') {
159
+ for (;;) {
160
+ put_to_dest(theA);
161
+ theA = get();
162
+ if (theA == theB) {
163
+ break;
164
+ }
165
+ if (theA == '\\') {
166
+ put_to_dest(theA);
167
+ theA = get();
168
+ }
169
+ if (theA == EOF) {
170
+ rb_raise(rb_eException, "JSMIN unterminated string literal.");
171
+ }
172
+ }
173
+ }
174
+ case 3:
175
+ theB = next();
176
+ if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
177
+ theA == ':' || theA == '[' || theA == '!' ||
178
+ theA == '&' || theA == '|' || theA == '?' ||
179
+ theA == '{' || theA == '}' || theA == ';' ||
180
+ theA == '\n')) {
181
+ put_to_dest(theA);
182
+ put_to_dest(theB);
183
+ for (;;) {
184
+ theA = get();
185
+ if (theA == '/') {
186
+ break;
187
+ }
188
+ if (theA =='\\') {
189
+ put_to_dest(theA);
190
+ theA = get();
191
+ }
192
+ if (theA == EOF) {
193
+ rb_raise(rb_eException, "JSMIN unterminated Regular Expression literal.");
194
+ }
195
+ put_to_dest(theA);
196
+ }
197
+ theB = next();
198
+ }
199
+ }
200
+ }
201
+
202
+
203
+ /* jsmin -- Copy the input to the output, deleting the characters which are
204
+ insignificant to JavaScript. Comments will be removed. Tabs will be
205
+ replaced with spaces. Carriage returns will be replaced with linefeeds.
206
+ Most spaces and linefeeds will be removed.
207
+ */
208
+
209
+ static void
210
+ jsmin()
211
+ {
212
+ theA = '\n';
213
+ action(3);
214
+ while (theA != EOF) {
215
+ switch (theA) {
216
+ case ' ':
217
+ if (isAlphanum(theB)) {
218
+ action(1);
219
+ } else {
220
+ action(2);
221
+ }
222
+ break;
223
+ case '\n':
224
+ switch (theB) {
225
+ case '{':
226
+ case '[':
227
+ case '(':
228
+ case '+':
229
+ case '-':
230
+ action(1);
231
+ break;
232
+ case ' ':
233
+ action(3);
234
+ break;
235
+ default:
236
+ if (isAlphanum(theB)) {
237
+ action(1);
238
+ } else {
239
+ action(2);
240
+ }
241
+ }
242
+ break;
243
+ default:
244
+ switch (theB) {
245
+ case ' ':
246
+ if (isAlphanum(theA)) {
247
+ action(1);
248
+ break;
249
+ }
250
+ action(3);
251
+ break;
252
+ case '\n':
253
+ switch (theA) {
254
+ case '}':
255
+ case ']':
256
+ case ')':
257
+ case '+':
258
+ case '-':
259
+ case '"':
260
+ case '\'':
261
+ action(1);
262
+ break;
263
+ default:
264
+ if (isAlphanum(theA)) {
265
+ action(1);
266
+ } else {
267
+ action(3);
268
+ }
269
+ }
270
+ break;
271
+ default:
272
+ action(1);
273
+ break;
274
+ }
275
+ }
276
+ }
277
+ }
278
+
279
+
280
+
281
+ static VALUE jsmin_initialize(VALUE self)
282
+ {
283
+ return self;
284
+ }
285
+
286
+ static VALUE jsmin_convert(VALUE self, VALUE str)
287
+ {
288
+ VALUE ret_str;
289
+
290
+ src = RSTRING_PTR(str);
291
+ src_len = RSTRING_LEN(str);
292
+
293
+ srci = desti = 0;
294
+ dest = malloc(src_len);
295
+ if (!dest)
296
+ rb_raise(rb_eNoMemError, "malloc failed in %s", __func__);
297
+
298
+ jsmin();
299
+
300
+ ret_str = rb_str_new(dest, desti);
301
+
302
+ free(dest);
303
+
304
+ return ret_str;
305
+ }
306
+
307
+ static VALUE cl;
308
+
309
+ void Init_jsmin_c()
310
+ {
311
+ cl = rb_define_class("JSMin", rb_cObject);
312
+ rb_define_method(cl, "initialize", jsmin_initialize, 0);
313
+ rb_define_method(cl, "convert", jsmin_convert, 1);
314
+ rb_define_method(cl, "minimize", jsmin_convert, 1);
315
+ }
data/jsmin_c.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'jsmin_c'
3
+ s.author = 'Domen Puncer'
4
+ s.email = 'domen@cba.si'
5
+ s.version = '1.0.0'
6
+ s.date = '2010-02-26'
7
+ s.homepage = 'http://www.riassence.org/'
8
+ s.summary = 'Riassence JSMin Wrapper'
9
+ s.has_rdoc = true
10
+ s.require_path = 'jsmin_c'
11
+ s.description = <<END
12
+ This is a ruby -> C wrapper extension for Douglas Crockford's awesome jsmin.
13
+ It's just as fast as the original (orders of magnitude faster than the pure ruby version in the 'jsmin' gem).
14
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
15
+ END
16
+ s.files = %w(
17
+ License.txt
18
+ README.rdoc
19
+ extconf.rb
20
+ jsmin_c.c
21
+ jsmin_c.gemspec
22
+ test_jsmin.rb
23
+ )
24
+ s.files.reject! { |fn| fn.include? ".svn" }
25
+ s.files.reject! { |fn| fn.include? ".git" }
26
+ s.test_file = 'test_jsmin.rb'
27
+ s.required_ruby_version = '>= 1.8.6'
28
+ s.extensions = [
29
+ 'extconf.rb'
30
+ ]
31
+ end
32
+
data/test_jsmin.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "test/unit"
2
+ require "rubygems"
3
+ require "jsmin_c"
4
+
5
+ class TestJSMin < Test::Unit::TestCase
6
+
7
+ @@test_input = %{
8
+
9
+ var Something = Foo.extend({
10
+ _fooBarLongName: function(_fooBar){
11
+ if(!_fooBar){
12
+ var _this = this;
13
+ _this['_anotherLongName']();
14
+ }
15
+ else{
16
+ this._dontCompressMe();
17
+ }
18
+ },
19
+ _anotherLongName: function(){
20
+ var _this = this;
21
+ _this.fooBarLongName(_this);
22
+ },
23
+ _dontCompressMe: function(){},
24
+ dontCompressMeEither: '_thisShouldNotBeCompressed';
25
+ });
26
+
27
+ }
28
+
29
+
30
+ @@test_output = "\nvar Something=Foo.extend({_fooBarLongName:function(_fooBar){if(!_fooBar){var _this=this;_this['_anotherLongName']();}\nelse{this._dontCompressMe();}},_anotherLongName:function(){var _this=this;_this.fooBarLongName(_this);},_dontCompressMe:function(){},dontCompressMeEither:'_thisShouldNotBeCompressed';});"
31
+
32
+ def test_init
33
+ jsmin = JSMin.new
34
+ end
35
+
36
+ def test_types
37
+ jsmin = JSMin.new
38
+ assert_equal( JSMin, jsmin.class )
39
+ assert_equal( String, jsmin.minimize( @@test_input ).class )
40
+ end
41
+
42
+ def test_value
43
+ jsmin = JSMin.new
44
+ assert_equal( @@test_output, jsmin.minimize( @@test_input ) )
45
+ end
46
+
47
+ end
48
+
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsmin_c
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Domen Puncer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-02-26 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: |
22
+ This is a ruby -> C wrapper extension for Douglas Crockford's awesome jsmin.
23
+ It's just as fast as the original (orders of magnitude faster than the pure ruby version in the 'jsmin' gem).
24
+ This used to be a fixed part of the Riassence Framework, but it's distributed as a separate gem now.
25
+
26
+ email: domen@cba.si
27
+ executables: []
28
+
29
+ extensions:
30
+ - extconf.rb
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - License.txt
35
+ - README.rdoc
36
+ - extconf.rb
37
+ - jsmin_c.c
38
+ - jsmin_c.gemspec
39
+ - test_jsmin.rb
40
+ has_rdoc: true
41
+ homepage: http://www.riassence.org/
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - jsmin_c
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 8
56
+ - 6
57
+ version: 1.8.6
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Riassence JSMin Wrapper
72
+ test_files:
73
+ - test_jsmin.rb