-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport_rdoc_example.rb
More file actions
executable file
·48 lines (37 loc) · 1.27 KB
/
Copy pathimport_rdoc_example.rb
File metadata and controls
executable file
·48 lines (37 loc) · 1.27 KB
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
#!/usr/bin/env ruby
require 'open3'
def bitclust_path
File.expand_path('~/.bitclust/rubydoc')
end
def builtin_path
File.join(bitclust_path, 'refm', 'api', 'src', '_builtin')
end
def builtin_class_path(klass)
File.join(builtin_path, klass.gsub('::', '__'))
end
def add_example_for(sig)
klass, method = sig.split('#')
path = builtin_class_path(klass)
lines = File.readlines(path)
method_index = lines.index do |l|
l.start_with?("--- #{method}") && (l.start_with?("--- #{method}(") || l.start_with?("--- #{method} "))
end
unless method_index
puts "[SKIP] Entry not found: " + sig
return
end
n_line = lines.drop(method_index).drop_while { |l| l.start_with?('--- ') }.take_while { |l| !l.start_with?('---') }.size
o, _s = Open3.capture2('ri', '--no-pager', '--no-site', '--no-gems', '--no-home', sig)
desc = o.split(/^-+\n/).last
if lines[method_index..method_index+n_line].join.include?('@see')
puts "[SKIP] See also found:" + sig
return
end
examples = desc.each_line.chunk { |l| l.start_with?(' ') }.map {|(k, v)| "\n例:\n" + v.join if k }.compact
lines[method_index + n_line, 0] = examples.join
File.write(path, lines.join)
end
# prepare rdoc.txt by find_rdoc_examples
File.foreach('rdoc.txt') do |sig|
add_example_for(sig.chomp)
end