jsminc 1.0.1
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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +23 -0
- data/Rakefile +8 -0
- data/ext/jsminc/extconf.rb +13 -0
- data/ext/jsminc/jsmic.c +283 -0
- data/jsminc.gemspec +23 -0
- data/lib/jsminc.rb +2 -0
- data/lib/jsminc/version.rb +3 -0
- metadata +66 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2002 Douglas Crockford (www.crockford.com)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
5
|
+
the Software without restriction, including without limitation the rights to
|
6
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
7
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
8
|
+
so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
11
|
+
copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
The Software shall be used for Good, not Evil.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
23
|
+
Modifications (c) 2011 Ryan Fitzgerald (rynftz.gr)
|
data/Rakefile
ADDED
data/ext/jsminc/jsmic.c
ADDED
@@ -0,0 +1,283 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
static char theA;
|
4
|
+
static char theB;
|
5
|
+
static char theLookahead = '\0';
|
6
|
+
|
7
|
+
static char *current_in;
|
8
|
+
static char *current_out;
|
9
|
+
|
10
|
+
/* isAlphanum -- return true if the character is a letter, digit, underscore,
|
11
|
+
dollar sign, or non-ASCII character.
|
12
|
+
*/
|
13
|
+
|
14
|
+
static int
|
15
|
+
isAlphanum(char c)
|
16
|
+
{
|
17
|
+
return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
18
|
+
(c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
|
19
|
+
c > 126);
|
20
|
+
}
|
21
|
+
|
22
|
+
|
23
|
+
/* get -- return the next character from the string. Watch out for lookahead. If
|
24
|
+
the character is a control character, translate it to a space or
|
25
|
+
linefeed.
|
26
|
+
*/
|
27
|
+
|
28
|
+
static char
|
29
|
+
get()
|
30
|
+
{
|
31
|
+
char c = theLookahead;
|
32
|
+
theLookahead = '\0';
|
33
|
+
if (c == '\0') {
|
34
|
+
c = *current_in;
|
35
|
+
current_in++;
|
36
|
+
}
|
37
|
+
if (c >= ' ' || c == '\n' || c == '\0') {
|
38
|
+
return c;
|
39
|
+
}
|
40
|
+
if (c == '\r') {
|
41
|
+
return '\n';
|
42
|
+
}
|
43
|
+
return ' ';
|
44
|
+
}
|
45
|
+
|
46
|
+
static void
|
47
|
+
write_char(const char c)
|
48
|
+
{
|
49
|
+
*current_out = c;
|
50
|
+
current_out++;
|
51
|
+
}
|
52
|
+
|
53
|
+
/* peek -- get the next character without getting it.
|
54
|
+
*/
|
55
|
+
|
56
|
+
static char
|
57
|
+
peek()
|
58
|
+
{
|
59
|
+
theLookahead = get();
|
60
|
+
return theLookahead;
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
/* next -- get the next character, excluding comments. peek() is used to see
|
65
|
+
if a '/' is followed by a '/' or '*'.
|
66
|
+
*/
|
67
|
+
|
68
|
+
static char
|
69
|
+
next()
|
70
|
+
{
|
71
|
+
char c = get();
|
72
|
+
if (c == '/') {
|
73
|
+
switch (peek()) {
|
74
|
+
case '/':
|
75
|
+
for (;;) {
|
76
|
+
c = get();
|
77
|
+
if (c <= '\n') {
|
78
|
+
return c;
|
79
|
+
}
|
80
|
+
}
|
81
|
+
case '*':
|
82
|
+
get();
|
83
|
+
for (;;) {
|
84
|
+
switch (get()) {
|
85
|
+
case '*':
|
86
|
+
if (peek() == '/') {
|
87
|
+
get();
|
88
|
+
return ' ';
|
89
|
+
}
|
90
|
+
break;
|
91
|
+
case '\0':
|
92
|
+
rb_raise(rb_eStandardError, "unterminated comment");
|
93
|
+
}
|
94
|
+
}
|
95
|
+
default:
|
96
|
+
return c;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
return c;
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
/* action -- do something! What you do is determined by the argument:
|
104
|
+
1 Output A. Copy B to A. Get the next B.
|
105
|
+
2 Copy B to A. Get the next B. (Delete A).
|
106
|
+
3 Get the next B. (Delete B).
|
107
|
+
action treats a string as a single character. Wow!
|
108
|
+
action recognizes a regular expression if it is preceded by ( or , or =.
|
109
|
+
*/
|
110
|
+
|
111
|
+
static void
|
112
|
+
action(int d)
|
113
|
+
{
|
114
|
+
switch (d) {
|
115
|
+
case 1:
|
116
|
+
write_char(theA);
|
117
|
+
case 2:
|
118
|
+
theA = theB;
|
119
|
+
if (theA == '\'' || theA == '"' || theA == '`') {
|
120
|
+
for (;;) {
|
121
|
+
write_char(theA);
|
122
|
+
theA = get();
|
123
|
+
if (theA == theB) {
|
124
|
+
break;
|
125
|
+
}
|
126
|
+
if (theA == '\\') {
|
127
|
+
write_char(theA);
|
128
|
+
theA = get();
|
129
|
+
}
|
130
|
+
if (theA == '\0') {
|
131
|
+
rb_raise(rb_eStandardError, "unterminated string literal");
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}
|
135
|
+
case 3:
|
136
|
+
theB = next();
|
137
|
+
if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
|
138
|
+
theA == ':' || theA == '[' || theA == '!' ||
|
139
|
+
theA == '&' || theA == '|' || theA == '?' ||
|
140
|
+
theA == '{' || theA == '}' || theA == ';' ||
|
141
|
+
theA == '\n')) {
|
142
|
+
write_char(theA);
|
143
|
+
write_char(theB);
|
144
|
+
for (;;) {
|
145
|
+
theA = get();
|
146
|
+
if (theA == '[') {
|
147
|
+
for (;;) {
|
148
|
+
write_char(theA);
|
149
|
+
theA = get();
|
150
|
+
if (theA == ']') {
|
151
|
+
break;
|
152
|
+
}
|
153
|
+
if (theA == '\\') {
|
154
|
+
write_char(theA);
|
155
|
+
theA = get();
|
156
|
+
}
|
157
|
+
if (theA == '\0') {
|
158
|
+
rb_raise(rb_eStandardError, "unterminated set in regex literal");
|
159
|
+
}
|
160
|
+
}
|
161
|
+
} else if (theA == '/') {
|
162
|
+
break;
|
163
|
+
} else if (theA =='\\') {
|
164
|
+
write_char(theA);
|
165
|
+
theA = get();
|
166
|
+
}
|
167
|
+
if (theA == '\0') {
|
168
|
+
rb_raise(rb_eStandardError, "unterminated regex literal");
|
169
|
+
}
|
170
|
+
write_char(theA);
|
171
|
+
}
|
172
|
+
theB = next();
|
173
|
+
}
|
174
|
+
}
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
/* jsmin -- Copy the input to the output, deleting the characters which are
|
179
|
+
insignificant to JavaScript. Comments will be removed. Tabs will be
|
180
|
+
replaced with spaces. Carriage returns will be replaced with linefeeds.
|
181
|
+
Most spaces and linefeeds will be removed.
|
182
|
+
*/
|
183
|
+
|
184
|
+
static void
|
185
|
+
jsmin()
|
186
|
+
{
|
187
|
+
theA = '\n';
|
188
|
+
action(3);
|
189
|
+
while (theA != '\0') {
|
190
|
+
switch (theA) {
|
191
|
+
case ' ':
|
192
|
+
if (isAlphanum(theB)) {
|
193
|
+
action(1);
|
194
|
+
} else {
|
195
|
+
action(2);
|
196
|
+
}
|
197
|
+
break;
|
198
|
+
case '\n':
|
199
|
+
switch (theB) {
|
200
|
+
case '{':
|
201
|
+
case '[':
|
202
|
+
case '(':
|
203
|
+
case '+':
|
204
|
+
case '-':
|
205
|
+
action(1);
|
206
|
+
break;
|
207
|
+
case ' ':
|
208
|
+
action(3);
|
209
|
+
break;
|
210
|
+
default:
|
211
|
+
if (isAlphanum(theB)) {
|
212
|
+
action(1);
|
213
|
+
} else {
|
214
|
+
action(2);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
break;
|
218
|
+
default:
|
219
|
+
switch (theB) {
|
220
|
+
case ' ':
|
221
|
+
if (isAlphanum(theA)) {
|
222
|
+
action(1);
|
223
|
+
break;
|
224
|
+
}
|
225
|
+
action(3);
|
226
|
+
break;
|
227
|
+
case '\n':
|
228
|
+
switch (theA) {
|
229
|
+
case '}':
|
230
|
+
case ']':
|
231
|
+
case ')':
|
232
|
+
case '+':
|
233
|
+
case '-':
|
234
|
+
case '"':
|
235
|
+
case '\'':
|
236
|
+
case '`':
|
237
|
+
action(1);
|
238
|
+
break;
|
239
|
+
default:
|
240
|
+
if (isAlphanum(theA)) {
|
241
|
+
action(1);
|
242
|
+
} else {
|
243
|
+
action(3);
|
244
|
+
}
|
245
|
+
}
|
246
|
+
break;
|
247
|
+
default:
|
248
|
+
action(1);
|
249
|
+
break;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
}
|
253
|
+
write_char('\0');
|
254
|
+
}
|
255
|
+
|
256
|
+
|
257
|
+
static VALUE minify(VALUE self, VALUE _in_s) {
|
258
|
+
char *in_s = StringValuePtr(_in_s);
|
259
|
+
long in_length = strlen(in_s);
|
260
|
+
char out_s[in_length + 1];
|
261
|
+
|
262
|
+
#ifdef RUBY_19
|
263
|
+
VALUE prev_encoding = rb_funcall(_in_s, rb_intern("encoding"), 0);
|
264
|
+
#endif
|
265
|
+
|
266
|
+
current_in = in_s;
|
267
|
+
current_out = out_s;
|
268
|
+
jsmin();
|
269
|
+
|
270
|
+
#ifdef RUBY_19
|
271
|
+
return rb_funcall(rb_str_new2(out_s + 1), rb_intern("force_encoding"), 1, prev_encoding);
|
272
|
+
#else
|
273
|
+
return rb_str_new2(out_s + 1);
|
274
|
+
#endif
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
void Init_jsminc() {
|
279
|
+
VALUE c = rb_cObject;
|
280
|
+
c = rb_const_get(c, rb_intern("JSMinC"));
|
281
|
+
|
282
|
+
rb_define_singleton_method(c, "minify", (VALUE(*)(ANYARGS))minify, 1);
|
283
|
+
}
|
data/jsminc.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "jsminc/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "jsminc"
|
7
|
+
s.version = JSMinC::VERSION
|
8
|
+
s.authors = ["Ryan Fitzgerald"]
|
9
|
+
s.email = ["rwfitzge@gmail.com"]
|
10
|
+
s.homepage = "http://rynftz.gr/"
|
11
|
+
s.summary = %q{A fast JavaScript minifier written in C (by Douglas Crockford)}
|
12
|
+
s.description = %q{JSMinC is the original C version of JSMin, embedded in Ruby.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "jsminc"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.extensions = `git ls-files -- ext/**/extconf.rb`.split("\n")
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'rake-compiler'
|
23
|
+
end
|
data/lib/jsminc.rb
ADDED
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsminc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Fitzgerald
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake-compiler
|
16
|
+
requirement: &70118772320960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70118772320960
|
25
|
+
description: JSMinC is the original C version of JSMin, embedded in Ruby.
|
26
|
+
email:
|
27
|
+
- rwfitzge@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions:
|
30
|
+
- ext/jsminc/extconf.rb
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- Rakefile
|
37
|
+
- ext/jsminc/extconf.rb
|
38
|
+
- ext/jsminc/jsmic.c
|
39
|
+
- jsminc.gemspec
|
40
|
+
- lib/jsminc.rb
|
41
|
+
- lib/jsminc/version.rb
|
42
|
+
homepage: http://rynftz.gr/
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: jsminc
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: A fast JavaScript minifier written in C (by Douglas Crockford)
|
66
|
+
test_files: []
|