jspp 0.1.8 → 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/README.md +53 -0
- data/bin/jspp +8 -31
- data/example/css/style-child.css +1 -0
- data/example/css/style.css +3 -0
- data/example/main.js +3 -0
- data/example/script.js +1 -0
- data/lib/jspp.rb +38 -0
- data/spec/helper.rb +11 -0
- data/spec/jspp_spec.rb +30 -0
- metadata +12 -6
- data/test/jspp_test.rb +0 -30
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
JavaScript preprocessor
|
2
|
+
===============
|
3
|
+
Minimal preprocessing with `/*>*/`
|
4
|
+
|
5
|
+
|
6
|
+
Usage
|
7
|
+
----
|
8
|
+
`jspp input > output`
|
9
|
+
|
10
|
+
Example
|
11
|
+
----
|
12
|
+
<table><tr>
|
13
|
+
<td>main.js
|
14
|
+
<pre>/*> script.js */
|
15
|
+
|
16
|
+
var style = "/*> css/style.css */";</pre></td>
|
17
|
+
<td>css/style.css
|
18
|
+
<pre>html, body {margin:0; padding:0}
|
19
|
+
/*> http://nv.github.com/js-preprocessor/example/css/style-child.css */
|
20
|
+
.my-style {background: #fffacc}
|
21
|
+
</pre></td>
|
22
|
+
<td><br/>css/style-child.css
|
23
|
+
<pre>.i-am-child {}
|
24
|
+
</pre></td>
|
25
|
+
</tr><tr>
|
26
|
+
<td><br/>script.js
|
27
|
+
<pre>var my_script;</pre></td>
|
28
|
+
</tr></table>
|
29
|
+
|
30
|
+
$ jspp example/main.js
|
31
|
+
var my_script;
|
32
|
+
|
33
|
+
var style = "html, body {margin:0; padding:0}\
|
34
|
+
.i-am-child {}\
|
35
|
+
.my-style {background: #fffacc}"
|
36
|
+
|
37
|
+
JavaScript preprocessor in a wild:
|
38
|
+
[GitHub Live Preview](http://github.com/NV/github-live-preview).
|
39
|
+
|
40
|
+
Installation
|
41
|
+
----
|
42
|
+
[Ruby gem](http://rubygems.org/gems/jspp)
|
43
|
+
|
44
|
+
$ gem install jspp
|
45
|
+
|
46
|
+
Why reinvent the wheel?
|
47
|
+
----
|
48
|
+
[Sprockets](http://github.com/sstephenson/sprockets) can't include non-JS files.
|
49
|
+
JS preprocessor can.
|
50
|
+
|
51
|
+
[Juicer](http://github.com/cjohansen/juicer) can include one JS file to another via
|
52
|
+
`@depend` statement, but can't include CSS file to JS as multiline string.
|
53
|
+
JS preprocessor can do it via `"/*> some_file */"`.
|
data/bin/jspp
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
3
|
+
require 'jspp'
|
4
|
+
|
5
|
+
if $stdin.tty? and ARGV.empty? or ARGV.delete'-h' or ARGV.delete'--help'
|
6
|
+
puts <<-how_to_use
|
4
7
|
|
5
8
|
JavaScript Preprocessor
|
6
9
|
|
@@ -11,7 +14,7 @@ Example: main.js: /*> script.js */
|
|
11
14
|
var style = "/*> style.css */";
|
12
15
|
|
13
16
|
style.css: html, body {margin:0; padding:0}
|
14
|
-
/*> http://nv.github.com/js-preprocessor/example/style-child.css */
|
17
|
+
/*> http://nv.github.com/js-preprocessor/example/css/style-child.css */
|
15
18
|
.my-style {background: #fffacc}
|
16
19
|
|
17
20
|
style-child.css: .i-am-child {}
|
@@ -23,34 +26,8 @@ Example: main.js: /*> script.js */
|
|
23
26
|
var style = "html, body {margin:0; padding:0}\\
|
24
27
|
.i-am-child {}\\
|
25
28
|
.my-style {background: #fffacc}"
|
26
|
-
|
27
|
-
|
28
|
-
DIR = File.dirname ARGV.first || '.'
|
29
|
-
R_INCLUDE = %r{/\*>\s*(.+?)\s*\*/}
|
30
|
-
R_INCLUDE_INSIDE_STRING = %r{"#{R_INCLUDE.source}"}
|
31
|
-
|
32
|
-
|
33
|
-
def get_file path=''
|
34
|
-
if path.index'http://' or path.index'https://'
|
35
|
-
require 'open-uri'
|
36
|
-
open(path).read
|
37
|
-
else
|
38
|
-
File.read File.join(DIR, path)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def parse text=''
|
43
|
-
text.gsub!(R_INCLUDE_INSIDE_STRING) {
|
44
|
-
'"' << parse(get_file $1).gsub(/$/, '\\').chop << '"'
|
45
|
-
}
|
46
|
-
text.gsub!(R_INCLUDE) {
|
47
|
-
parse get_file $1
|
48
|
-
}
|
49
|
-
text
|
50
|
-
end
|
51
|
-
|
52
|
-
if $stdin.tty? and ARGV.empty? or ARGV.delete'-h' or ARGV.delete'--help'
|
53
|
-
puts HELP
|
29
|
+
how_to_use
|
54
30
|
else
|
55
|
-
|
31
|
+
DIR = File.dirname ARGV.first || '.'
|
32
|
+
print JSPP(ARGF.read, DIR)
|
56
33
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
.i-am-child {}
|
data/example/main.js
ADDED
data/example/script.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
var my_script;
|
data/lib/jspp.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class JSPP
|
2
|
+
|
3
|
+
INCLUDE = %r{/\*>\s*(.+?)\s*\*/}
|
4
|
+
INCLUDE_INSIDE_STRING = %r{"#{INCLUDE.source}"}
|
5
|
+
|
6
|
+
def get_file path, base_dir='.'
|
7
|
+
if path.index'http://' or path.index'https://'
|
8
|
+
require 'open-uri'
|
9
|
+
file = open(path).read
|
10
|
+
dir = File.dirname path
|
11
|
+
else
|
12
|
+
full_path = File.join base_dir, path
|
13
|
+
file = File.read full_path
|
14
|
+
dir = File.dirname full_path
|
15
|
+
end
|
16
|
+
[file, dir]
|
17
|
+
end
|
18
|
+
|
19
|
+
def parse text='', base_dir='.'
|
20
|
+
text.gsub!(INCLUDE_INSIDE_STRING) {
|
21
|
+
file, dir = get_file $1, base_dir
|
22
|
+
parsed = parse file, dir
|
23
|
+
'"' << parsed.gsub(/$/, '\\').chop << '"'
|
24
|
+
}
|
25
|
+
text.gsub!(INCLUDE) {
|
26
|
+
file, dir = get_file $1, base_dir
|
27
|
+
parse file, dir
|
28
|
+
}
|
29
|
+
text
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Kernel
|
34
|
+
def JSPP text, base_dir='.'
|
35
|
+
JSPP.new.parse text, base_dir
|
36
|
+
end
|
37
|
+
private :JSPP
|
38
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec'
|
2
|
+
DIR = File.dirname __FILE__
|
3
|
+
require DIR + '/../lib/jspp'
|
4
|
+
require 'fakeweb'
|
5
|
+
|
6
|
+
PETS = "Cat, dog, turtle, hamster, parrot\n"
|
7
|
+
PETS_URL = 'http://elv1s.ru/x/pets.txt'
|
8
|
+
FakeWeb.register_uri :get, PETS_URL, :body => PETS
|
9
|
+
EXAMPLE = DIR + '/../example'
|
10
|
+
CHILD_CSS = File.read(EXAMPLE + '/css/style-child.css')
|
11
|
+
FakeWeb.register_uri :get, 'http://nv.github.com/js-preprocessor/example/css/style-child.css', :body => CHILD_CSS
|
data/spec/jspp_spec.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe JSPP do
|
4
|
+
describe '#get_file' do
|
5
|
+
it 'fetches URI' do
|
6
|
+
JSPP.new.get_file(PETS_URL).first.should == PETS
|
7
|
+
end
|
8
|
+
it 'reads file' do
|
9
|
+
main_js = EXAMPLE + '/main.js'
|
10
|
+
JSPP.new.get_file(main_js, '/').first.should == File.read(main_js)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#parse' do
|
15
|
+
it 'include files' do
|
16
|
+
file = File.read(EXAMPLE + '/main.js')
|
17
|
+
JSPP.new.parse(file, EXAMPLE).should == <<-result
|
18
|
+
var my_script;
|
19
|
+
|
20
|
+
var style = "html, body {margin:0; padding:0}\\
|
21
|
+
.i-am-child {}\\
|
22
|
+
.my-style {background: #fffacc}\\
|
23
|
+
";
|
24
|
+
result
|
25
|
+
end
|
26
|
+
it 'returns same text if no /*> file */ was found' do
|
27
|
+
JSPP.new.parse(PETS).should == PETS
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: jspp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
|
9
|
-
version: 0.1.8
|
9
|
+
version: 1.0.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nikita Vasilyev
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-20 00:00:00 +03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -27,8 +27,9 @@ extensions: []
|
|
27
27
|
extra_rdoc_files: []
|
28
28
|
|
29
29
|
files:
|
30
|
+
- lib/jspp.rb
|
30
31
|
- bin/jspp
|
31
|
-
-
|
32
|
+
- README.md
|
32
33
|
has_rdoc: true
|
33
34
|
homepage: http://github.com/NV/js-preprocessor
|
34
35
|
licenses: []
|
@@ -37,7 +38,7 @@ post_install_message:
|
|
37
38
|
rdoc_options: []
|
38
39
|
|
39
40
|
require_paths:
|
40
|
-
-
|
41
|
+
- lib
|
41
42
|
required_ruby_version: !ruby/object:Gem::Requirement
|
42
43
|
requirements:
|
43
44
|
- - ">="
|
@@ -60,4 +61,9 @@ signing_key:
|
|
60
61
|
specification_version: 3
|
61
62
|
summary: JavaScript preprocessor
|
62
63
|
test_files:
|
63
|
-
-
|
64
|
+
- spec/helper.rb
|
65
|
+
- spec/jspp_spec.rb
|
66
|
+
- example/css/style-child.css
|
67
|
+
- example/css/style.css
|
68
|
+
- example/main.js
|
69
|
+
- example/script.js
|
data/test/jspp_test.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
|
3
|
-
class JSPreprocessorTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
DIR = File.dirname __FILE__
|
6
|
-
EXAMPLE = File.join DIR, '..', 'example', ''
|
7
|
-
JSPP = File.join DIR, '..', 'bin', 'jspp'
|
8
|
-
|
9
|
-
def jspp(name='')
|
10
|
-
`#{JSPP} #{EXAMPLE}#{name}`
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_examples
|
14
|
-
assert_equal File.read("#{EXAMPLE}style-child.css"), jspp('style-child.css')
|
15
|
-
assert_equal <<-multiline, jspp('style.css')
|
16
|
-
html, body {margin:0; padding:0}
|
17
|
-
.i-am-child {}
|
18
|
-
.my-style {background: #fffacc}
|
19
|
-
multiline
|
20
|
-
assert_equal File.read("#{EXAMPLE}script.js"), jspp('script.js')
|
21
|
-
assert_equal <<-multiline, jspp('main.js')
|
22
|
-
var my_script;
|
23
|
-
|
24
|
-
var style = "html, body {margin:0; padding:0}\\
|
25
|
-
.i-am-child {}\\
|
26
|
-
.my-style {background: #fffacc}\\
|
27
|
-
";
|
28
|
-
multiline
|
29
|
-
end
|
30
|
-
end
|