rubyscript 0.0.5
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 +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/lib/rubyscript.rb +6 -0
- data/lib/rubyscript/convert.rb +96 -0
- data/lib/rubyscript/version.rb +3 -0
- data/rubyscript.gemspec +19 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/rubyscript.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "ruby_parser"
|
2
|
+
|
3
|
+
module RubyScript
|
4
|
+
class Convert
|
5
|
+
def initialize(ruby)
|
6
|
+
@ruby = ruby
|
7
|
+
end
|
8
|
+
|
9
|
+
def convert
|
10
|
+
decipher(RubyParser.new.parse(@ruby))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def convert_anon parsed
|
15
|
+
send("convert_#{parsed[0]}", parsed)
|
16
|
+
end
|
17
|
+
|
18
|
+
def decipher parsed
|
19
|
+
convert_anon(parsed)
|
20
|
+
end
|
21
|
+
|
22
|
+
def convert_defn defn
|
23
|
+
func_name = defn[1]
|
24
|
+
func_args = convert_args(defn[2])
|
25
|
+
func_body = convert_anon(defn[3])
|
26
|
+
"var #{func_name} = function(#{func_args}) {\n" +
|
27
|
+
" #{func_body}" +
|
28
|
+
"};\n"
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_args args
|
32
|
+
# TODO: add support for default values
|
33
|
+
args.delete_at(0)
|
34
|
+
args.join(", ")
|
35
|
+
end
|
36
|
+
|
37
|
+
def convert_scope scope
|
38
|
+
convert_anon(scope[1])
|
39
|
+
end
|
40
|
+
|
41
|
+
def convert_block block
|
42
|
+
body = convert_anon(block[1])
|
43
|
+
"#{body};\n"
|
44
|
+
end
|
45
|
+
|
46
|
+
def convert_call call
|
47
|
+
call_name = call[2]
|
48
|
+
call_args = convert_arglist(call[3])
|
49
|
+
"#{call_name}(#{call_args})"
|
50
|
+
end
|
51
|
+
|
52
|
+
def convert_arglist arglist
|
53
|
+
arglist.delete_at(0)
|
54
|
+
args = ""
|
55
|
+
arglist.each_index do |i|
|
56
|
+
if (i == 0)
|
57
|
+
args << convert_anon(arglist[i])
|
58
|
+
else
|
59
|
+
args << ", #{convert_anon(arglist[i])}"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
args
|
63
|
+
end
|
64
|
+
|
65
|
+
# Data types
|
66
|
+
def convert_nil nil_arg
|
67
|
+
"null"
|
68
|
+
end
|
69
|
+
|
70
|
+
def convert_str str
|
71
|
+
"\"#{str[1]}\""
|
72
|
+
end
|
73
|
+
|
74
|
+
def convert_dstr dstr
|
75
|
+
dstr.delete_at(0)
|
76
|
+
string = "\"#{dstr[0]}"
|
77
|
+
dstr.delete_at(0)
|
78
|
+
dstr.each do |str|
|
79
|
+
if (str[0] == :str)
|
80
|
+
string << "#{str[1]}"
|
81
|
+
else
|
82
|
+
string << convert_anon(str)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
"#{string}\""
|
86
|
+
end
|
87
|
+
|
88
|
+
def convert_evstr evstr
|
89
|
+
"\" + #{convert_anon(evstr[1])} + \""
|
90
|
+
end
|
91
|
+
|
92
|
+
def convert_lvar lvar
|
93
|
+
lvar[1].to_s
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/rubyscript.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rubyscript/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rubyscript"
|
7
|
+
s.version = RubyScript::VERSION
|
8
|
+
s.authors = ["James Birtles"]
|
9
|
+
s.email = ["itsme@jamesbirtles.com"]
|
10
|
+
s.homepage = "http://jamesbirtles.org/rubyscript/"
|
11
|
+
s.summary = %q{Convert Ruby into JavaScript}
|
12
|
+
s.description = %q{RubyScript lets you write JavaScript code, in Ruby! It will convert Ruby code into JavaScript code for use on the web and elsewhere}
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_dependency('ruby_parser', '>= 2.2.0')
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyscript
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Birtles
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-28 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: ruby_parser
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 2.2.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
description: RubyScript lets you write JavaScript code, in Ruby! It will convert Ruby code into JavaScript code for use on the web and elsewhere
|
28
|
+
email:
|
29
|
+
- itsme@jamesbirtles.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- Rakefile
|
40
|
+
- lib/rubyscript.rb
|
41
|
+
- lib/rubyscript/convert.rb
|
42
|
+
- lib/rubyscript/version.rb
|
43
|
+
- rubyscript.gemspec
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://jamesbirtles.org/rubyscript/
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.6.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Convert Ruby into JavaScript
|
72
|
+
test_files: []
|
73
|
+
|