Module: Ruby2JS
- Defined in:
- lib/ruby2js.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/filter/jquery.rb,
lib/ruby2js/filter/return.rb,
lib/ruby2js/filter/angularrb.rb,
lib/ruby2js/filter/functions.rb
Defined Under Namespace
Modules: Filter, VERSION
Classes: Converter
Class Method Summary
collapse
Class Method Details
.convert(source, options = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/ruby2js.rb', line 16
def self.convert(source, options={})
if Proc === source
file,line = source.source_location
source = File.read(file).untaint
ast = find_block( parse(source), line )
elsif Parser::AST::Node === source
ast = source
source = ast.loc.expression.source_buffer.source
else
ast = parse( source )
end
filters = options[:filters] || Filter::DEFAULTS
unless filters.empty?
filter = Parser::AST::Processor
filters.reverse.each do |mod|
filter = Class.new(filter) {include mod}
end
ast = filter.new.process(ast)
end
ruby2js = Ruby2JS::Converter.new( ast )
ruby2js.binding = options[:binding]
if source.include? "\n"
ruby2js.enable_vertical_whitespace
lines = ruby2js.to_js.split("\n")
pre = ''
pending = false
blank = true
lines.each do |line|
if line.start_with? '}' or line.start_with? ']'
pre.sub!(/^ /,'')
line.sub!(/;$/,";\n")
pending = true
else
pending = false
end
line.sub! /^/, pre
if line.end_with? '{' or line.end_with? '['
pre += ' '
line.sub!(/^/,"\n") unless blank or pending
pending = true
end
blank = pending
end
lines.join("\n").gsub(/^ ( *(case.*|default):$)/, '\1')
else
ruby2js.to_js
end
end
|
.find_block(ast, line) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/ruby2js.rb', line 81
def self.find_block(ast, line)
if ast.type == :block and ast.loc.expression.line == line
return ast.children.last
end
ast.children.each do |child|
if Parser::AST::Node === child
block = find_block child, line
return block if block
end
end
nil
end
|
.parse(source) ⇒ Object
74
75
76
77
78
79
|
# File 'lib/ruby2js.rb', line 74
def self.parse(source)
buffer = Parser::Source::Buffer.new('__SOURCE__')
buffer.raw_source = source.encode('utf-8')
Parser::CurrentRuby.new.parse(buffer)
end
|