jspp 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/jspp +27 -26
  2. data/lib/jspp.rb +17 -14
  3. data/spec/jspp_spec.rb +40 -6
  4. metadata +3 -3
data/bin/jspp CHANGED
@@ -1,33 +1,34 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # Usage: jspp input > output
4
+ #
5
+ # EXAMPLE
6
+ # main.js: /*> script.js */
7
+ #
8
+ # var style = "/*> style.css */";
9
+ #
10
+ # style.css: html, body {margin:0; padding:0}
11
+ # /*> http://nv.github.com/js-preprocessor/example/css/style-child.css */
12
+ # .my-style {background: #fffacc}
13
+ #
14
+ # style-child.css: .i-am-child {}
15
+ #
16
+ # script.js: var my_script;
17
+ #
18
+ # jspp example/main.js: var my_script;
19
+ #
20
+ # var style = "html, body {margin:0; padding:0}\\
21
+ # .i-am-child {}\\
22
+ # .my-style {background: #fffacc}"
23
+
3
24
  require 'jspp'
4
25
 
5
26
  if $stdin.tty? and ARGV.empty? or ARGV.delete'-h' or ARGV.delete'--help'
6
- puts <<-how_to_use
7
-
8
- JavaScript Preprocessor
9
-
10
- Usage: jspp input > output
11
-
12
- Example: main.js: /*> script.js */
13
-
14
- var style = "/*> style.css */";
15
-
16
- style.css: html, body {margin:0; padding:0}
17
- /*> http://nv.github.com/js-preprocessor/example/css/style-child.css */
18
- .my-style {background: #fffacc}
19
-
20
- style-child.css: .i-am-child {}
21
-
22
- script.js: var my_script;
23
-
24
- jspp example/main.js: var my_script;
25
-
26
- var style = "html, body {margin:0; padding:0}\\
27
- .i-am-child {}\\
28
- .my-style {background: #fffacc}"
29
- how_to_use
27
+ $stderr.puts File.read(__FILE__).grep(/^#[^!]/) {|line|
28
+ line.sub /^# ?/, ''
29
+ }.join
30
+ exit 1
30
31
  else
31
- DIR = File.dirname ARGV.first || '.'
32
- print JSPP(ARGF.read, DIR)
32
+ dir = File.dirname ARGV.first || '.'
33
+ print JSPP(ARGF.read, dir)
33
34
  end
@@ -2,19 +2,6 @@ class JSPP
2
2
 
3
3
  INCLUDE = %r{/\*>\s*(.+?)\s*\*/}
4
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
5
 
19
6
  def parse text='', base_dir='.'
20
7
  text.gsub!(INCLUDE_INSIDE_STRING) {
@@ -28,10 +15,26 @@ class JSPP
28
15
  }
29
16
  text
30
17
  end
18
+
19
+ def get_file path, base_dir='.'
20
+ if path.index'http://' or path.index'https://'
21
+ require 'open-uri'
22
+ file = open(path).read
23
+ dir = File.dirname path
24
+ else
25
+ full_path = File.join base_dir, path
26
+ file = File.read full_path
27
+ dir = File.dirname full_path
28
+ end
29
+ [file, dir]
30
+ end
31
+
31
32
  end
32
33
 
33
34
  module Kernel
34
- def JSPP text, base_dir='.'
35
+ def JSPP path
36
+ text = File.read path
37
+ base_dir = File.dirname path
35
38
  JSPP.new.parse text, base_dir
36
39
  end
37
40
  private :JSPP
@@ -1,13 +1,22 @@
1
1
  require 'helper'
2
2
 
3
3
  describe JSPP do
4
- describe '#get_file' do
5
- it 'fetches URI' do
6
- JSPP.new.get_file(PETS_URL).first.should == PETS
4
+
5
+ describe '::INCLUDE' do
6
+ it 'matches files' do
7
+ JSPP::INCLUDE.match('/*> path_to/my_file.js */')[1].should == 'path_to/my_file.js'
7
8
  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)
9
+ it 'matches URIs' do
10
+ JSPP::INCLUDE.match('/*> http://elv1s.ru/x/pets.txt */')[1].should == 'http://elv1s.ru/x/pets.txt'
11
+ end
12
+ end
13
+
14
+ describe '::INCLUDE_INSIDE_STRING' do
15
+ it 'matches local files inside strings' do
16
+ JSPP::INCLUDE_INSIDE_STRING.match('"/*> path_to/my_file.js */"')[1].should == 'path_to/my_file.js'
17
+ end
18
+ it 'matches URIs inside strings' do
19
+ JSPP::INCLUDE_INSIDE_STRING.match('"/*> http://elv1s.ru/x/pets.txt */"')[1].should == 'http://elv1s.ru/x/pets.txt'
11
20
  end
12
21
  end
13
22
 
@@ -27,4 +36,29 @@ var style = "html, body {margin:0; padding:0}\\
27
36
  JSPP.new.parse(PETS).should == PETS
28
37
  end
29
38
  end
39
+
40
+ describe '#get_file' do
41
+ it 'fetches URI' do
42
+ JSPP.new.get_file(PETS_URL).first.should == PETS
43
+ end
44
+ it 'reads file' do
45
+ main_js = EXAMPLE + '/main.js'
46
+ JSPP.new.get_file(main_js, '/').first.should == File.read(main_js)
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+
53
+ describe Kernel, '#JSPP' do
54
+ it 'do all the stuff' do
55
+ JSPP(EXAMPLE + '/main.js').should == <<-result
56
+ var my_script;
57
+
58
+ var style = "html, body {margin:0; padding:0}\\
59
+ .i-am-child {}\\
60
+ .my-style {background: #fffacc}\\
61
+ ";
62
+ result
63
+ end
30
64
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 0
8
7
  - 1
9
- version: 1.0.1
8
+ - 0
9
+ version: 1.1.0
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-20 00:00:00 +03:00
17
+ date: 2010-03-21 00:00:00 +03:00
18
18
  default_executable:
19
19
  dependencies: []
20
20