js2 0.0.10 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/Manifest +42 -0
  2. data/README.md +65 -0
  3. data/Rakefile +19 -35
  4. data/bin/js2 +80 -66
  5. data/config/js2.yml +2 -0
  6. data/js2.gemspec +33 -0
  7. data/lib/js2/{haml_parser.rb → parser/haml.rb} +2 -2
  8. data/lib/js2/{haml_engine.rb → parser/haml_engine.rb} +1 -1
  9. data/lib/js2/parser/lexer.rb +37 -0
  10. data/lib/js2/{parser.rb → parser/tokenizer.rb} +157 -143
  11. data/lib/js2/{replace.rb → ragel/helper.rb} +16 -5
  12. data/lib/js2/ragel/tokenizer.rl +561 -0
  13. data/lib/js2/{tokenizer.rl.erb → ragel/tokenizer.rl.erb} +12 -19
  14. data/lib/js2/standard/factory.rb +289 -0
  15. data/lib/js2/standard/node.rb +75 -0
  16. data/lib/js2/util/compilation.rb +77 -0
  17. data/lib/js2/util/config.rb +84 -0
  18. data/lib/js2/util/exec.rb +34 -0
  19. data/lib/js2/util/file_handler.rb +73 -0
  20. data/lib/js2/{js2bootstrap.js2 → util/js2bootstrap.js2} +12 -68
  21. data/lib/js2/util/processor.rb +88 -0
  22. data/lib/js2/util/rdoc.rb +35 -0
  23. data/lib/js2/{sel_decorator.rb → util/sel_decorator.rb} +11 -1
  24. data/lib/js2.rb +22 -45
  25. data/test/compiled/bar.js +3 -0
  26. data/test/compiled/basic.comp.js +31 -0
  27. data/test/compiled/basic.js +27 -0
  28. data/test/compiled/foo.js +3 -0
  29. data/test/fixtures/bar.js2 +3 -0
  30. data/test/fixtures/basic.js2 +27 -0
  31. data/test/fixtures/basic.js2.haml +4 -0
  32. data/test/fixtures/basic.js2.yml +5 -0
  33. data/test/fixtures/curry.js2 +5 -0
  34. data/test/fixtures/foo.js2 +3 -0
  35. data/test/fixtures/member.js2 +14 -0
  36. data/test/fixtures/private.js2 +5 -0
  37. data/test/fixtures/property.js2 +4 -0
  38. data/test/test_helper.rb +25 -0
  39. data/test/test_js2.rb +43 -0
  40. data/wiki/features.md +106 -0
  41. data/wiki/installation.md +53 -0
  42. metadata +89 -83
  43. data/Changelog +0 -33
  44. data/History.txt +0 -4
  45. data/Manifest.txt +0 -35
  46. data/PostInstall.txt +0 -7
  47. data/README +0 -69
  48. data/README.rdoc +0 -69
  49. data/README.txt +0 -69
  50. data/examples/js2.yml +0 -8
  51. data/examples/test.yml +0 -5
  52. data/lib/javascript/sel_marker.js2 +0 -150
  53. data/lib/javascript/test.js2 +0 -73
  54. data/lib/js2/config.rb +0 -39
  55. data/lib/js2/daemon.rb +0 -35
  56. data/lib/js2/file_handler.rb +0 -91
  57. data/lib/js2/foo.js2.haml +0 -3
  58. data/lib/js2/js2.js +0 -110
  59. data/lib/js2/processor.rb +0 -112
  60. data/lib/js2/test/selenium.rb +0 -119
  61. data/lib/js2/test/selenium_element.rb +0 -234
  62. data/lib/js2/test/selenium_helper.rb +0 -27
  63. data/lib/js2/tree.rb +0 -351
  64. data/lib/js2/universe.rb +0 -123
  65. data/lib/tasks/js2.rake +0 -9
  66. data/website/index.txt +0 -86
  67. /data/{LICENSE → lib/js2/standard/class_node.rb} +0 -0
@@ -1,150 +0,0 @@
1
- class JS2.SelMarker {
2
-
3
- function initialize () {
4
- this.scopes = {};
5
- this.children = {};
6
- this.selectors = {};
7
- this.normalized = false;
8
-
9
- // keep track of all the handlers
10
- JS2.SEL_TRACKER.idCount = JS2.SEL_TRACKER.idCount || 0;
11
- JS2.SEL_TRACKER.idCount++;
12
- JS2.SEL_TRACKER[JS2.SEL_TRACKER.idCount] = this;
13
- }
14
-
15
- function newChild (namespace) {
16
- if (! this.children[namespace]) this.children[namespace] = new JS2.SelMarker();
17
- return this.children[namespace];
18
- }
19
-
20
- function addVal (scope, key, val, selector) {
21
- this.normalized = false;
22
- if (! this.scopes[scope]) {
23
- this.scopes[scope] = {};
24
- this.selectors[scope] = {};
25
- }
26
- this.scopes[scope][key] = val;
27
- this.selectors[scope][key] = selector;
28
- }
29
-
30
- function appendVal (scope, key, val, selector) {
31
- this.normalized = false;
32
- if (! this.scopes[scope]) {
33
- this.scopes[scope] = {};
34
- this.selectors[scope] = {};
35
- }
36
- if (! this.scopes[scope][key]) {
37
- this.scopes[scope][key] = [];
38
- this.selectors[scope][key] = [];
39
- }
40
- this.scopes[scope][key].push(val);
41
- this.selectors[scope][key].push(selector);
42
- }
43
-
44
- function getRawVal (scope, key) {
45
- this.normalize();
46
- return this.scopes[scope][key];
47
- }
48
-
49
- function getVal (scope, key, idx) {
50
- this.normalize();
51
- if (idx === undefined) {
52
- var val = this.scopes[scope][key];
53
- var sel = this.selectors[scope][key];
54
- if (val.find && sel) val = val.find(sel);
55
- return (val.jquery && val.length == 1) ? val[0] : val;
56
- } else {
57
- var val = this.scopes[scope][key];
58
- var sel = this.selectors[scope][key];
59
- if (val.find && sel) val = val.find(sel);
60
- return val[idx];
61
- }
62
- }
63
-
64
- function getJQ (scope, key, idx) {
65
- idx = idx || 0;
66
- return $(this.getVal(scope, key, idx));
67
- }
68
-
69
- function toJson () {
70
- return JSON.stringify(this.toHash());
71
- }
72
-
73
- function toHash () {
74
- this.normalize();
75
- var ret = {
76
- 'children': {}
77
- };
78
-
79
- for (var name in this.scopes) {
80
- var scope = this.scopes[name];
81
- ret[name] = {};
82
- for (var key in scope) {
83
- var val = scope[key];
84
- if (val.length == 1) {
85
- ret[name][key] = 'Scalar';
86
- } else {
87
- ret[name][key] = 'Array';
88
- }
89
- }
90
- }
91
-
92
- for (var namespace in this.children) {
93
- var childMarker = this.children[namespace];
94
- ret.children[namespace] = childMarker.toHash();
95
- }
96
-
97
- return ret;
98
- }
99
-
100
- function normalize () {
101
- // TODO
102
- this.normalized = true;
103
- }
104
-
105
- function getRealClassScope (klass) {
106
- if (klass.main && klass.main.factoryId) {
107
- var namespace = klass.main['class'].className.replace(/\.\w+$/, '');
108
- var classname = klass['class'].className.match(/\.\w+$/)[0];
109
- return namespace + classname;
110
- } else {
111
- if (!klass.className) return klass['class'].className;
112
- return klass.className;
113
- }
114
- }
115
-
116
- private
117
-
118
- function normalizeVal (val) {
119
- var ret = [];
120
- if (typeof val == 'object') {
121
- if (val.jquery) {
122
- foreach (var e in val) {
123
- if (! e) continue;
124
- ret.push(e);
125
- }
126
- } else if (val instanceof Array) {
127
- foreach (var v in val) {
128
- if (! v) continue;
129
- var norms = this.normalizeVal(v);
130
- foreach (var norm in norms) {
131
- if (! norm) continue;
132
- ret.push(norm);
133
- }
134
- }
135
- }
136
- }
137
-
138
- return ret;
139
- }
140
-
141
- }
142
-
143
- JS2.SEL_EVENTS = {};
144
- JS2.SEL_EVENTS.LEFT_BUTTON = $.browser.msie ? 1 : 0;
145
- JS2.SEL_EVENTS.LEFT_MOUSE_UP = $.Event('mouseup');
146
- JS2.SEL_EVENTS.LEFT_MOUSE_UP.button = JS2.SEL_EVENTS.LEFT_BUTTON;
147
- JS2.SEL_EVENTS.LEFT_MOUSE_DOWN = $.Event('mousedown');
148
- JS2.SEL_EVENTS.LEFT_MOUSE_DOWN.button = JS2.SEL_EVENTS.LEFT_BUTTON;
149
- JS2.SEL_TRACKER = {};
150
- JS2.SEL_MARKER = new JS2.SelMarker();
@@ -1,73 +0,0 @@
1
- class JS2.Test {
2
- function initialize () {
3
- this.store = {};
4
- this.namespaces = {};
5
- }
6
-
7
- function setNamespace (name) {
8
- this.namespaces[name] = this.namespaces[name] || {};
9
- this.switchNamespace(name);
10
- }
11
-
12
- function switchNamespace (name) {
13
- if (! this.namespaces[name]) this.setNamespace(name);
14
- this.store = this.namespaces[name];
15
- }
16
-
17
- function addEle (klass, key, ele) {
18
- if (! this.store[klass]) this.store[klass] = {};
19
-
20
- var klassStore = this.store[klass];
21
- if (! klassStore[key]) klassStore[key] = {};
22
-
23
- klassStore[key] = ele;
24
- }
25
-
26
- function pushEle (klass, key, ele) {
27
- if (! this.store[klass]) this.store[klass] = {};
28
- if (! this.store[klass][key]) this.store[klass][key] = [];
29
- if (ele.length && ele.length > 1) {
30
- foreach (var e in ele) {
31
- this.store[klass][key].push(ele);
32
- }
33
- } else {
34
- this.store[klass][key].push(ele)
35
- }
36
- }
37
-
38
- function getEle (klass, key, n) {
39
- if (! this.store[klass]) return false;
40
- var ele = this.store[klass][key];
41
-
42
- if (! ele) return false;
43
-
44
- n = n || 0;
45
-
46
- if (ele.length > 0) {
47
- return ele[n];
48
- } else {
49
- return ele;
50
- }
51
- }
52
-
53
- function setEle (klass, key) {
54
- var ele = this.currentEle;
55
- if (ele) {
56
- ele.setAttribute(this.currentID);
57
- }
58
-
59
- var newEle = this.store[klass][key];
60
- if (! newEle) return false;
61
-
62
- if (newEle.jquery) {
63
- newEle = newEle[0];
64
- }
65
-
66
- this.currentID = newEle.getAttribute('id');
67
- newEle.setAttribute('id', 'JS2_TEST_ELEMENT');
68
-
69
- return newEle.nodeName;
70
- }
71
- }
72
-
73
- JS2.TEST = new JS2.Test();
data/lib/js2/config.rb DELETED
@@ -1,39 +0,0 @@
1
- require 'yaml'
2
-
3
- class JS2::Config
4
- DEFAULTS = {
5
- :framework => 'jquery',
6
- :highlight => true,
7
- :js2_dir => './js2',
8
- :haml_engine_class => false,
9
- :reference_dir => nil,
10
- :write_dir => './public/javascripts',
11
- :test_mode => false,
12
- :js2_haml_dir => './js2',
13
- :haml_dir => './app/views',
14
- :selenium => { },
15
- :vars => { }
16
- }
17
-
18
- attr_accessor *DEFAULTS.keys
19
-
20
- def initialize (hash = nil)
21
- @vars = Hash.new
22
-
23
- DEFAULTS.each_pair do |k,v|
24
- self.send(k.to_s + '=', v)
25
- end
26
-
27
- load_hash(hash) if hash
28
- end
29
-
30
- def load_hash(hash)
31
- DEFAULTS.keys.each do |k|
32
- k = k.to_s
33
- self.send(k + '=', hash[k]) if hash.has_key?(k)
34
- end
35
-
36
- self.js2_dir = hash[:read_dir] if hash[:read_dir]
37
- end
38
- end
39
-
data/lib/js2/daemon.rb DELETED
@@ -1,35 +0,0 @@
1
- class JS2::Daemon
2
- attr_accessor :haml_parser, :decorator, :parser, :universe, :haml_engine
3
- INTERVAL = 1
4
-
5
- def initialize (processor)
6
- @processor = processor
7
- end
8
-
9
- def run (n=nil)
10
- n ||= -1
11
- i = 0
12
- while i != n
13
- i += 1
14
-
15
- if check_files
16
- yield if block_given?
17
- else
18
- sleep INTERVAL
19
- end
20
- end
21
- end
22
-
23
-
24
- private
25
-
26
- def check_files
27
- unless @processor.fh.need_update!
28
- return false
29
- end
30
- @processor.write_files
31
- rescue Exception => e
32
- print e.to_s
33
- print "Error in processing files"
34
- end
35
- end
@@ -1,91 +0,0 @@
1
- class JS2::FileHandler
2
- attr_accessor :read_dir, :write_dir
3
-
4
- def initialize (params = {})
5
- @read_dir = params[:read_dir]
6
- @write_dir = params[:write_dir] || @read_dir
7
- @haml_dir = params[:haml_dir] || @read_dir
8
- @view_dir = params[:view_dir] || @read_dir
9
- @yml_dir = params[:read_dir]
10
-
11
- puts self.inspect
12
- @mtimes = Hash.new
13
- end
14
-
15
- def get_view_files
16
- return [] unless @view_dir
17
- return Dir.glob(@view_dir + '/**/*.haml')
18
- end
19
-
20
- def get_haml_files
21
- return [] unless @haml_dir
22
- return Dir.glob(@haml_dir + '/**/*/*.js2.haml') + Dir.glob(@haml_dir + '/*.js2.haml')
23
- end
24
-
25
- def get_yml_files
26
- return [] unless @yml_dir
27
- return Dir.glob(@yml_dir + '/**/*/*.js2.yml') + Dir.glob(@yml_dir + '/*.js2.yml')
28
- end
29
-
30
- def get_files
31
- return Dir.glob(@read_dir + '/**/*/*.js2') + Dir.glob(@read_dir + '/*.js2')
32
- end
33
-
34
- def write_file (filename, str, force = false)
35
- to_write = get_js_filename(filename, force)
36
- FileUtils.mkpath(File.dirname(to_write))
37
- File.open(to_write, 'w') do |f|
38
- f << str
39
- end
40
- end
41
-
42
- def get_js_filename (filename, force = false)
43
- to_write = filename.sub(/^#{@read_dir}/, @write_dir).sub(/\.js2$/, '.js')
44
-
45
- if force
46
- to_write = @write_dir + '/' + filename
47
- end
48
- return to_write
49
- end
50
-
51
- def write_compilation (filename, compilation)
52
- to_write = filename.sub(/^#{@read_dir}/, @write_dir).sub(/\.js2$/, '.comp.js')
53
- str = ""
54
-
55
- compilation.uniq.each do |f|
56
- read_file = f.sub(/^#{@read_dir}/, @write_dir).sub(/\.js2$/, '.js')
57
- str << File.read(read_file) + "\n"
58
- end
59
-
60
- FileUtils.mkpath(File.dirname(to_write))
61
- File.open(to_write, 'w') do |f|
62
- f << str
63
- end
64
- end
65
-
66
- def need_update!
67
- ret = false
68
- did = Hash.new
69
-
70
- files = self.get_files + self.get_haml_files + self.get_yml_files
71
-
72
- files.each do |file|
73
- mtime = File.mtime(file)
74
- if @mtimes[file] != mtime
75
- @mtimes[file] = mtime
76
- ret = true
77
- end
78
- did[file] = mtime
79
- end
80
-
81
- @mtimes.keys.each do |file|
82
- ret = true unless did[file]
83
- end
84
-
85
- @mtimes = did
86
- return ret
87
- rescue
88
- return true
89
- end
90
-
91
- end
data/lib/js2/foo.js2.haml DELETED
@@ -1,3 +0,0 @@
1
- Foo
2
- main
3
- %div
data/lib/js2/js2.js DELETED
@@ -1,110 +0,0 @@
1
- (function (scope) {
2
- if (scope.JS2) return;
3
-
4
- var JS2 = {};
5
- scope.JS2 = JS2;
6
-
7
- var ooUtils = {
8
- extends: function (par) {
9
- this.oo.parent = par;
10
- this.oo.prime.prototype = par.oo.instance;
11
- this.prototype = new this.oo.prime();
12
- },
13
-
14
- include: function (mod) {
15
- var hash = mod.prototype;
16
- var members = this.oo.members;
17
- for (var k in hash) {
18
- if (! members[k]) this.oo.prime.prototype[k] = newFly[k];
19
- }
20
- },
21
-
22
- member: function (name, member) {
23
- this.oo.members[name] = true;
24
- this.oo.prime.prototype[name] = member;
25
- },
26
-
27
- staticMember: function (name, member) {
28
- this[name] = member;
29
- },
30
-
31
- setHTMLCache: function (hash) {
32
- // create temp class
33
- var tempClass = function () {};
34
-
35
- // look for super htmlCache
36
- var par = this.oo.parent;
37
- if (par) {
38
- var parCache = par.oo.instance.htmlCache;
39
- if (parCache) tempClass.prototype = parCache;
40
- }
41
-
42
- var htmlCache = new tempClass();
43
- for (var k in hash) tempObj[k] = hash[k];
44
- this.oo('member', 'htmlCache', htmlCache);
45
- },
46
-
47
- super: function (member) {
48
- return this.oo.parent.instance[member];
49
- },
50
-
51
- property: function (names) {
52
- for (var i=0; i<names.length; i++) {
53
- var name = names[i];
54
- var getter = 'get' + name.charAt(0).toUpperCase() + a.substr(1);
55
- var setter = 'set' + name.charAt(0).toUpperCase() + a.substr(1);
56
- if (!this.oo.members[getter]) this.oo('member', getter, function () { return this[name] });
57
- if (!this.oo.members[setter]) this.oo('member', setter, function (val) { return this[name] = val });
58
- }
59
- }
60
-
61
- };
62
-
63
- // ROOT Object
64
- (function (JS2) {
65
- JS2.Object = function () {};
66
- JS2.Object.meta = { instance: new JS2.Object(), methods: {} };
67
- JS2.Object.prototype.initialize = function () { return function () {} };
68
- JS2.Object.prototype.super = function (member) { return this.prototype.prototype[member]; };
69
- })(JS2);
70
-
71
- function createClass (name, par) {
72
- var K = function () { this.initialize.apply(this, arguments); };
73
-
74
- var prime = function () {};
75
- K.prototype = new prime();
76
- K.prototype.class = K;
77
- K.prototype.initialize = prime; // hack
78
-
79
- K.oo = function (method, param1, param2) {
80
- ooUtils[method].apply(K, [ param1, param2 ]);
81
- };
82
-
83
- K.oo.prime = prime;
84
- K.oo.includes = [];
85
- K.oo.instance = new K();
86
- K.oo.members = {};
87
-
88
- return K;
89
- }
90
-
91
- function createNamespace (space, currentScope) {
92
- var currentScope = currentScope || scope;
93
-
94
- var splitted = space.split('.');
95
- var name = [];
96
- while (splitted.length) {
97
- var part = splitted.shift();
98
- name.push(part);
99
- if (! currentScope[part]) currentScope[part] = createClass();
100
- currentScope = currentScope[part];
101
- currentScope.name = name.join('.');
102
- }
103
- }
104
-
105
- JS2.OO = {};
106
- JS2.OO.createClass = createNamespace;
107
-
108
-
109
- })(window);
110
-
data/lib/js2/processor.rb DELETED
@@ -1,112 +0,0 @@
1
- require 'yaml'
2
-
3
- class JS2::Processor
4
- attr_accessor :fh, :haml_engine
5
-
6
- # ----------------------------------------------------------------------------
7
- # Rails support
8
- # ----------------------------------------------------------------------------
9
- def self.from_rails
10
- hash = YAML.load_file("#{RAILS_ROOT}/config/js2.yml")[RAILS_ENV]
11
- config = JS2::Config.new(hash)
12
- return self.get(config)
13
- end
14
-
15
- def self.daemon_from_rails
16
- processor = self.from_rails
17
- return JS2::Daemon.new(processor)
18
- end
19
-
20
- # ----------------------------------------------------------------------------
21
- # yaml support
22
- # ----------------------------------------------------------------------------
23
- def self.from_yaml(file, env = nil)
24
- hash = YAML.load_file(file)
25
- hash = hash[env] if env
26
-
27
- config = JS2::Config.new(hash)
28
- return self.get(config)
29
- end
30
-
31
- def self.daemon_from_yaml(file, env = nil)
32
- p = self.from_yaml(file, env)
33
- return JS2::Daemon.new(p)
34
- end
35
-
36
- # ----------------------------------------------------------------------------
37
- # legacy
38
- # ----------------------------------------------------------------------------
39
- def self.from_file (file, env = nil)
40
- hash = YAML.load_file(file)
41
- hash = hash[env] if env
42
- config = JS2::Config.new(hash)
43
- return self.get(config)
44
- end
45
-
46
- def self.processor_from_file (file, env = nil)
47
- return self.from_file(file, env)
48
- end
49
-
50
- def self.daemon_from_file (file, env = nil)
51
- p = self.from_file(file, env)
52
- return JS2::Daemon.new(p)
53
- end
54
-
55
- def self.from_hash (hash)
56
- config = JS2::Config.new(hash)
57
- return self.get(config)
58
- end
59
-
60
- def self.daemon_from_hash (hash)
61
- p = self.from_hash(hash)
62
- return p
63
- end
64
-
65
-
66
- def self.get (config = nil)
67
- # get config file
68
- config ||= JS2::Config.new
69
-
70
- # haml engine
71
- haml_engine_class = config.haml_engine_class ?
72
- config.haml_engine_class.constantize :
73
- JS2::HamlEngine
74
-
75
- haml_engine = haml_engine_class.new
76
-
77
- # file handler
78
- js2_dir = config.js2_dir
79
- write_dir = config.write_dir
80
- js2_haml_dir = config.js2_haml_dir
81
- haml_dir = config.haml_dir
82
-
83
- file_handler = JS2::FileHandler.new(
84
- :read_dir => js2_dir,
85
- :write_dir => write_dir,
86
- :haml_dir => js2_haml_dir,
87
- :view_dir => haml_dir
88
- )
89
-
90
- return self.new(
91
- :test_mode => config.test_mode,
92
- :reference_dir => config.reference_dir,
93
- :haml_engine => haml_engine,
94
- :file_handler => file_handler,
95
- :vars => config.vars
96
- )
97
- end
98
-
99
- def initialize (params)
100
- @haml_engine = params[:haml_engine]
101
- @fh = params[:file_handler]
102
- @reference_dir = params[:reference_dir]
103
- @test_mode = params[:test_mode]
104
- @vars = params[:vars]
105
- end
106
-
107
- def write_files
108
- universe = JS2::Universe.new(@fh, @haml_engine)
109
- universe.write
110
- end
111
-
112
- end