svenfuchs-ripper2ruby 0.0.1 → 0.0.2

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.
@@ -7,6 +7,8 @@ require 'core_ext/array/flush'
7
7
  require 'ripper/ruby_builder/token'
8
8
  require 'ripper/ruby_builder/stack'
9
9
 
10
+ require 'erb/stripper'
11
+
10
12
  Dir[File.dirname(__FILE__) + '/ruby_builder/events/*.rb'].each { |file| require file }
11
13
 
12
14
  # Ripper::RubyBuilder extends Ripper's SexpBuilder and builds a rich, object
@@ -25,10 +27,16 @@ class Ripper
25
27
  class ParseError < RuntimeError
26
28
  end
27
29
 
30
+ @@filters = [lambda { |src, file| file && File.extname(file) == '.erb' ? Erb::Stripper.new.to_ruby(src) : src }]
31
+
28
32
  class << self
29
33
  def build(src, filename = nil)
30
34
  new(src, filename).parse
31
35
  end
36
+
37
+ def filters
38
+ @@filters
39
+ end
32
40
  end
33
41
 
34
42
  NEWLINE = [:@nl, :@ignored_nl]
@@ -63,17 +71,27 @@ class Ripper
63
71
 
64
72
  def initialize(src, filename = nil, lineno = nil)
65
73
  @src = src ||= filename && File.read(filename) || ''
66
- @src.gsub!(/([\s\n]*)\Z/) { |s| @trailing_whitespace = Ruby::Whitespace.new(s) and nil }
67
74
 
68
75
  @filename = filename
69
76
  @stack = []
70
77
  @stack = Stack.new
71
78
  @string_stack = []
72
79
 
80
+ src.gsub!(/([\s\n]*)\Z/) { |s| @trailing_whitespace = Ruby::Whitespace.new(s) and nil }
81
+ src = filter(src, filename)
82
+
73
83
  super
84
+ rescue ArgumentError => e
85
+ p filename
86
+ raise e
74
87
  end
75
88
 
76
89
  protected
90
+ def filter(src, filename)
91
+ self.class.filters.each { |filter| src = filter.call(src, filename) }
92
+ src
93
+ end
94
+
77
95
  def position
78
96
  Ruby::Node::Position.new(lineno.to_i - 1, column)
79
97
  end
@@ -129,7 +147,7 @@ class Ripper
129
147
  def pop_assignment_operator(options = {})
130
148
  pop_token(*ASSIGN_OPERATORS, options)
131
149
  end
132
-
150
+
133
151
  def pop_end_data
134
152
  token = pop_token(:@__end__)
135
153
  token.token = Ruby::Node::Text::Clip.new(src, token.position).to_s if token # TODO clean up
@@ -157,12 +175,5 @@ class Ripper
157
175
  Ruby::ExecutableString.new(nil, build_token(token))
158
176
  end
159
177
  end
160
-
161
- # def extract_src(from, to)
162
- # lines = Ruby::Node::Text.split(src)
163
- # lines[from.row] = lines[from.row][from.col..-1] # || ''
164
- # lines[to.row] = lines[to.row][0, to.col]
165
- # lines[from.row..to.row].join
166
- # end
167
178
  end
168
179
  end
@@ -10,7 +10,7 @@ module Ruby
10
10
  attr_accessor :filename
11
11
  attr_writer :src
12
12
 
13
- def initialize(src, filename, statements, end_data)
13
+ def initialize(src, filename, statements = [], end_data = nil)
14
14
  self.src = src
15
15
  self.filename = filename if filename
16
16
  self.end_data = end_data if end_data
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class BuildSourceTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test original erb source should be still available to the node" do
7
+ src = "<html>\n<body>\n<%= t(:foo) -%>\n</body>\n</html>"
8
+ node = build(src, 'foo.erb')
9
+ assert_equal src, node.src
10
+ assert_equal 't(:foo)', node.select(Ruby::Call).first.src
11
+ assert_equal 0, node.select(Ruby::Call).first.context(:width => 1).index('<body>')
12
+ end
13
+
14
+ define_method :"test ruby 1.9 encodings are confusing" do
15
+ filename = File.dirname(__FILE__) + '/../fixtures/template.iso-8859-1.html.erb'
16
+ src = File.read(filename)
17
+
18
+ assert_equal 'UTF-8', src.encoding.name
19
+ assert_equal false, src.valid_encoding?
20
+
21
+ # src = File.open('iso-8859-1.rb', 'rb:binary') { |f| f.read }
22
+ # p src.encoding.name
23
+ # p src.valid_encoding?
24
+ # puts src
25
+
26
+ # src.set_encoding('iso-8859-1')
27
+ # p src.encoding.name
28
+ # p src.valid_encoding?
29
+ # p src
30
+
31
+ # src = "<html>\n<body><h1>überschrift</h1>\n<%= t(:foo) -%>\n</body>\n</html>"
32
+ # src.force_encoding("iso-8859-1")
33
+ # build(src, 'foo.erb')
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <h1>�berschrift</h1>
4
+ <%= t(:foo) %>
5
+ </body>
6
+ </html>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svenfuchs-ripper2ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs
@@ -29,7 +29,6 @@ files:
29
29
  - lib/erb/stripper.rb
30
30
  - lib/highlighters/ansi.rb
31
31
  - lib/ripper/event_log.rb
32
- - lib/ripper/ruby_builder.rb
33
32
  - lib/ripper/ruby_builder/buffer.rb
34
33
  - lib/ripper/ruby_builder/events/args.rb
35
34
  - lib/ripper/ruby_builder/events/array.rb
@@ -53,8 +52,8 @@ files:
53
52
  - lib/ripper/ruby_builder/queue.rb
54
53
  - lib/ripper/ruby_builder/stack.rb
55
54
  - lib/ripper/ruby_builder/token.rb
55
+ - lib/ripper/ruby_builder.rb
56
56
  - lib/ripper2ruby.rb
57
- - lib/ruby.rb
58
57
  - lib/ruby/aggregate.rb
59
58
  - lib/ruby/alternation/args.rb
60
59
  - lib/ruby/alternation/hash.rb
@@ -73,13 +72,13 @@ files:
73
72
  - lib/ruby/list.rb
74
73
  - lib/ruby/literal.rb
75
74
  - lib/ruby/method.rb
76
- - lib/ruby/node.rb
77
75
  - lib/ruby/node/composite.rb
78
76
  - lib/ruby/node/conversions.rb
79
77
  - lib/ruby/node/position.rb
80
78
  - lib/ruby/node/source.rb
81
79
  - lib/ruby/node/text.rb
82
80
  - lib/ruby/node/traversal.rb
81
+ - lib/ruby/node.rb
83
82
  - lib/ruby/operator.rb
84
83
  - lib/ruby/params.rb
85
84
  - lib/ruby/statements.rb
@@ -87,8 +86,7 @@ files:
87
86
  - lib/ruby/symbol.rb
88
87
  - lib/ruby/token.rb
89
88
  - lib/ruby/while.rb
90
- - MIT-LICENSE
91
- - README.markdown
89
+ - lib/ruby.rb
92
90
  has_rdoc: false
93
91
  homepage: http://github.com/svenfuchs/ripper2ruby
94
92
  post_install_message:
@@ -117,6 +115,7 @@ specification_version: 2
117
115
  summary: Ripper2Ruby builds an object-oriented representation of Ruby code that you can modify and recompile back to Ruby
118
116
  test_files:
119
117
  - test/all.rb
118
+ - test/builder/source_test.rb
120
119
  - test/builder/stack_test.rb
121
120
  - test/builder/text_test.rb
122
121
  - test/context_test.rb
@@ -126,10 +125,11 @@ test_files:
126
125
  - test/fixtures/source_2.rb
127
126
  - test/fixtures/stuff.rb
128
127
  - test/fixtures/template.html.erb
128
+ - test/fixtures/template.iso-8859-1.html.erb
129
129
  - test/fixtures/tmp.rb
130
- - test/libs.txt
131
130
  - test/lib_test.rb
132
131
  - test/lib_test_helper.rb
132
+ - test/libs.txt
133
133
  - test/nodes/args_test.rb
134
134
  - test/nodes/array_test.rb
135
135
  - test/nodes/assignment_test.rb
@@ -147,8 +147,8 @@ test_files:
147
147
  - test/nodes/literals_test.rb
148
148
  - test/nodes/method_test.rb
149
149
  - test/nodes/namespaces_test.rb
150
- - test/nodes/nodes_test.rb
151
150
  - test/nodes/node_test.rb
151
+ - test/nodes/nodes_test.rb
152
152
  - test/nodes/operator_test.rb
153
153
  - test/nodes/separators_test.rb
154
154
  - test/nodes/statements_test.rb
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 Sven Fuchs <svenfuchs@artweb-design.de>
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown DELETED
@@ -1,10 +0,0 @@
1
- Ripper2Ruby
2
- ===========
3
-
4
- Similar to ruby2ruby this library allows to parse Ruby code, modify and
5
- recompile it back to Ruby.
6
-
7
- Differences:
8
-
9
- * uses Ripper for parsing (shipped with Ruby 1.9)
10
- * produces a full object-oriented representation of the Ruby code