Module: Ruby2JS
- Defined in:
- lib/ruby2js/filter/jquery.rb,
lib/ruby2js.rb,
lib/ruby2js/rails.rb,
lib/ruby2js/version.rb,
lib/ruby2js/converter.rb,
lib/ruby2js/converter/if.rb,
lib/ruby2js/converter/arg.rb,
lib/ruby2js/converter/def.rb,
lib/ruby2js/converter/for.rb,
lib/ruby2js/converter/nil.rb,
lib/ruby2js/converter/sym.rb,
lib/ruby2js/converter/var.rb,
lib/ruby2js/filter/return.rb,
lib/ruby2js/filter/strict.rb,
lib/ruby2js/converter/args.rb,
lib/ruby2js/converter/case.rb,
lib/ruby2js/converter/cvar.rb,
lib/ruby2js/converter/defs.rb,
lib/ruby2js/converter/dstr.rb,
lib/ruby2js/converter/hash.rb,
lib/ruby2js/converter/ivar.rb,
lib/ruby2js/converter/next.rb,
lib/ruby2js/converter/self.rb,
lib/ruby2js/converter/send.rb,
lib/ruby2js/converter/xstr.rb,
lib/ruby2js/converter/array.rb,
lib/ruby2js/converter/begin.rb,
lib/ruby2js/converter/block.rb,
lib/ruby2js/converter/break.rb,
lib/ruby2js/converter/casgn.rb,
lib/ruby2js/converter/class.rb,
lib/ruby2js/converter/const.rb,
lib/ruby2js/converter/masgn.rb,
lib/ruby2js/converter/super.rb,
lib/ruby2js/converter/undef.rb,
lib/ruby2js/converter/until.rb,
lib/ruby2js/converter/vasgn.rb,
lib/ruby2js/converter/while.rb,
lib/ruby2js/converter/cvasgn.rb,
lib/ruby2js/converter/ivasgn.rb,
lib/ruby2js/converter/module.rb,
lib/ruby2js/converter/opasgn.rb,
lib/ruby2js/converter/regexp.rb,
lib/ruby2js/converter/return.rb,
lib/ruby2js/filter/angularrb.rb,
lib/ruby2js/filter/functions.rb,
lib/ruby2js/converter/boolean.rb,
lib/ruby2js/converter/defined.rb,
lib/ruby2js/converter/kwbegin.rb,
lib/ruby2js/converter/literal.rb,
lib/ruby2js/converter/logical.rb,
lib/ruby2js/filter/underscore.rb,
lib/ruby2js/converter/blockpass.rb,
lib/ruby2js/converter/prototype.rb,
lib/ruby2js/converter/untilpost.rb,
lib/ruby2js/converter/whilepost.rb,
lib/ruby2js/filter/angular-route.rb,
lib/ruby2js/filter/angular-resource.rb
Overview
Defined Under Namespace
Modules: Filter, Rails, VERSION
Classes: Converter
Class Method Summary
collapse
Class Method Details
.convert(source, options = {}) ⇒ Object
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/ruby2js.rb', line 48
def self.convert(source, options={})
if Proc === source
file,line = source.source_location
source = File.read(file.dup.untaint).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 = Filter::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]
ruby2js.ivars = options[:ivars]
if ruby2js.binding and not ruby2js.ivars
ruby2js.ivars = ruby2js.binding.eval \
'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
end
ruby2js.width = options[:width] if options[:width]
if source.include? "\n"
ruby2js.enable_vertical_whitespace
lines = ruby2js.to_js.split("\n")
pre = ''
pending = false
blank = true
lines.each do |line|
next if line.empty?
if ')}]'.include? line[0]
pre.sub!(/^ /,'')
line.sub!(/([,;])$/,"\\1\n")
pending = true
else
pending = false
end
line.sub! /^/, pre
if '({['.include? line[-1]
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/ruby2js.rb', line 122
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
115
116
117
118
119
120
|
# File 'lib/ruby2js.rb', line 115
def self.parse(source)
buffer = Parser::Source::Buffer.new('__SOURCE__')
buffer.raw_source = source.encode('utf-8')
Parser::CurrentRuby.new.parse(buffer)
end
|