This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
code.rb
262 lines (220 loc) · 6.7 KB
/
code.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
require 'nokogiri'
require 'ruby_parser'
require 'processor'
require 'net/http'
# TODO: SSL support
# require 'net/https'
Net::HTTPResponse.class_eval do
def http?
content_type == 'text/html' or content_type == 'application/xhtml+xml'
end
end
module ExplainRuby
module FromUrl
def from_url(url)
code = if raw_url = get_raw_url(url)
get_raw_body(raw_url)
else
response = get_http_response(url)
if response.html?
extract_code_from_html(response.body)
else
response.body
end
end
new(code, raw_url || url)
end
def extract_code_from_html(doc)
doc = Nokogiri::HTML doc unless doc.respond_to? :search
codes = doc.search('pre').map { |p| p.inner_text }
codes.detect { |text| text !~ /^[\s\d]+$/ }
end
def get_http_response(url)
response = Net::HTTP.get_response URI.parse(url)
response.error! unless Net::HTTPSuccess === response
response
end
def get_html_document(url)
response = get_http_response(url)
response.error! unless response.html?
Nokogiri::HTML response.body
end
def get_raw_body(url)
response = get_http_response(url)
response.error! if response.html?
response.body
end
def get_raw_url(url)
case url
when %r{^(https?://pastie.org)/pastes/(\w+)$}
url = "#{$1}/#{$2}.txt"
when %r{^(http://github.com/[^/]+/[^/]+)/blob/(.+)$}
url = "#{$1}/raw/#{$2}"
when %r{^(https?://gist.github.com)/\w+$}
prefix = $1
doc = get_html_document(url)
if raw_link = doc.at('.file[id$=".rb"] .actions a[href^="/raw/"]')
raw_url = raw_link['href']
raw_url = prefix + raw_url unless raw_url.index('http') == 0
raw_url
end
else
nil
end
end
end
class Code
extend FromUrl
def initialize(code, url = nil)
raise ArgumentError, "no code given" if code.nil? or code.empty?
@code = code.to_s
@url = url.nil?? nil : url.to_s
@reconstructed_code = nil
@explained_code = nil
@sexp = nil
end
def to_s
@explained_code ||= process
end
def process
insert_explanations reconstruct_code
end
def parse
@sexp ||= self.class.ruby2sexp(@code, @url)
end
# delegate pretty printing to sexp
def pretty_print(io)
parse.pretty_print(io)
end
def reconstruct_code
@reconstructed_code ||= self.class.ruby2ruby(parse, @url)
end
EXPLANATIONS_PATH = File.expand_path('../explanations', __FILE__)
FIXTURES_PATH = File.expand_path('../fixtures', __FILE__)
def self.get_explanation(name)
begin
File.read(EXPLANATIONS_PATH + "/#{name}.md").strip
rescue Errno::ENOENT
"(No explanation for #{name})"
end
end
def insert_explanations(code)
code.gsub(/^\s*>>(.+)/) {
names = $1.split
names.map { |name| self.class.get_explanation(name) }.join("\n\n").gsub(/^/, '>')
}
end
def self.ruby2ruby(input, filename = nil)
filename = input.path if filename.nil? and input.respond_to? :path
input = ruby2sexp(input, filename) unless ::Sexp === input
ruby2ruby = Processor.new
ruby2ruby.process input
end
def self.ruby2sexp(io, filename = io.path)
code = io.respond_to?(:read) ? io.read : io.to_s
parser = ::RubyParser.new
parser.process(code, filename)
end
def self.from_test_fixture(name)
File.open(FIXTURES_PATH + "/#{name}.rb") do |file|
new(file.read, file.path)
end
end
end
end
if $0 == __FILE__
require 'spec/autorun'
require 'pp'
body_code, body_no_code, body_gist, body_gist_no_ruby = DATA.read.split('===')
describe ExplainRuby::Code do
describe ".get_raw_url" do
it "translates pastie" do
url = described_class.get_raw_url 'http://pastie.org/pastes/1234'
url.should == 'http://pastie.org/1234.txt'
end
it "translates github" do
url = described_class.get_raw_url 'http://github.com/mislav/nibbler/blob/master/lib/nibbler/json.rb'
url.should == 'http://github.com/mislav/nibbler/raw/master/lib/nibbler/json.rb'
end
it "translates gist" do
gist_url = 'http://gist.github.com/540757'
described_class.should_receive(:get_html_document).
with(gist_url).and_return(Nokogiri::HTML(body_gist))
url = described_class.get_raw_url gist_url
url.should == 'http://gist.github.com/raw/540/455/paperclip_defaults.rb'
end
it "fails for gist without a ruby file" do
gist_url = 'http://gist.github.com/540757'
described_class.should_receive(:get_html_document).
with(gist_url).and_return(Nokogiri::HTML(body_gist_no_ruby))
url = described_class.get_raw_url gist_url
url.should be_nil
end
it "fails at unknown" do
url = described_class.get_raw_url 'http://example.com'
url.should be_nil
end
end
describe ".extract_code_from_html" do
it "skips line numbers" do
code = described_class.extract_code_from_html body_code
code.should == "def foo\n bar\nend"
end
it "returns nil when no code" do
code = described_class.extract_code_from_html body_no_code
code.should be_nil
end
end
describe "#reconstruct_code" do
it "inserts explanation markers" do
code = described_class.new("class Klass < Main; end")
code.reconstruct_code.should == ">> class class_inheritance\nclass Klass < Main\nend"
end
it "doesn't insert same marker twice" do
code = described_class.new("def foo() 1 end; def bar() 2 end")
code.reconstruct_code.should == ">> method\ndef foo\n 1\nend\n\ndef bar\n 2\nend\n"
end
end
it "delegates pretty printing to sexp" do
code = described_class.new("class Klass; end")
code.pretty_inspect.should == "s(:class, :Klass, nil, s(:scope))\n"
end
end
end
__END__
<body>
<pre>1
2
3</pre>
<pre>def foo
bar
end</pre>
</body>
===
<body>
<p>No code here</p>
</body>
===
<body>
<div class="file" id="notes.md">
<div class="actions">
<a href="/moo">moo</a>
<a href="/raw/540/455/notes.md">raw</a>
</div>
</div>
<div class="file" id="file_paperclip_defaults.rb">
<div class="actions">
<a href="/moo">moo</a>
<a href="/raw/540/455/paperclip_defaults.rb">raw</a>
</div>
</div>
</body>
===
<body>
<div class="file" id="notes.md">
<div class="actions">
<a href="/moo">moo</a>
<a href="/raw/540/455/notes.md">raw</a>
</div>
</div>
</body>