Class: WebRI
- Inherits:
-
Object
show all
- Defined in:
- lib/webri.rb,
lib/webri/version.rb
Overview
TODO: Support favorites.
TODO: Support recents.
TODO: Support direct in REPL; e.g., Array.sort.
TODO: Support direct from command-line.
TODO: Support partial on command-line, into REPL.
TODO: Support alternate character for '#' on command-line. ('3'?)
TODO: Token begins with character, period, colon, or hashmark;
anything else could signal a special op.
A class to display Ruby online HTML documentation.
Defined Under Namespace
Classes: ClassEntry, Entry, FileEntry, InstanceMethodEntry, SingletonMethodEntry
Constant Summary
collapse
- DOC_SITE =
Site of the official documentation.
'https://docs.ruby-lang.org/en/'
- CLASS =
'class/module'
- SINGLETON =
'singleton method'
- INSTANCE =
'instance method'
- FILE =
'file'
- ANSI_COLOR =
{
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
bright_black: 90,
bright_red: 91,
bright_green: 92,
bright_yellow: 93,
bright_blue: 94,
bright_magenta: 95,
bright_cyan: 96,
bright_white: 97,
}
- VERSION =
"2.2.2"
- HELP =
<<EOT
There are four types of #{WebRI.variable('name')}, as determined by prefixes:
|------------------|----------------|--------------------|
| Type | Prefix | Example |
|------------------|----------------|--------------------|
| Class/module | Capital letter | #{WebRI.tokenq('Array')} |
| Singleton method | #{WebRI.tokenq('::')} | #{WebRI.tokenq('::new')} |
| Instance method | #{WebRI.tokenq('#')} | #{WebRI.tokenq('#inspect')} |
| Ruby file | #{WebRI.tokenq('ruby:')} | #{WebRI.tokenq('ruby:syntax_rdoc')} |
|------------------|----------------|--------------------|
Note: On the command-line, the instance method prefix should be escaped as #{WebRI.string('\\#')} if your shell requires it.
Name handling:
- If #{WebRI.variable('name')} is exactly a name of its type (but not the beginning of other such names),
#{WebRI.webri} opens the page for that name.
Examples: #{WebRI.tokenq('Array')}, #{WebRI.tokenq('::trap')}, #{WebRI.tokenq('#xmlschema')}, #{WebRI.tokenq('ruby:syntax_rdoc')}.
- If #{WebRI.variable('name')} is the beginning of exactly one name of its type,
#{WebRI.webri} asks whether to open the page for that name.
Examples: #{WebRI.tokenq('Arra')}, #{WebRI.tokenq('::tra')}, #{WebRI.tokenq('#xmlschem')}, #{WebRI.tokenq('ruby:syntax_')}.
- If #{WebRI.variable('name')} is the beginning of multiple names of its type,
#{WebRI.webri} displays those names and lets you choose.
Examples: #{WebRI.tokenq('A')}, #{WebRI.tokenq('::t')}, #{WebRI.tokenq('#')}, #{WebRI.tokenq('ruby:s')}.
- If #{WebRI.variable('name')} is not a valid name of its type,
#{WebRI.webri} asks whether show such names.
Examples: #{WebRI.tokenq('Xyzzy')}, #{WebRI.tokenq('::xyzzy')}, #{WebRI.tokenq('#xyzzy')}, #{WebRI.tokenq('ruby:xyzzy')}.
- If #{WebRI.variable('name')} is not valid at all (i.e., does not start with any of the prefixes above),
#{WebRI.webri} prints an error message.
Examples: #{WebRI.tokenq('nosuch')}, #{WebRI.tokenq('$foo')}, #{WebRI.tokenq('%Bar')}.
EOT
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#capture_options(options) ⇒ Object
-
#do_name(name) ⇒ Object
-
#get_boolean_answer(question) ⇒ Object
Present question; return answer.
-
#get_choice(situation, choices, type) ⇒ Object
-
#get_choice_(choices, required: false) ⇒ Object
Present choices; return choice.
-
#help(response) ⇒ Object
-
#help_main ⇒ Object
-
#initialize(name = nil, options = {}) ⇒ WebRI
constructor
-
#make_groups ⇒ Object
-
#open_readme ⇒ Object
-
#open_uri(name, target_uri) ⇒ Object
-
#opener_name ⇒ Object
-
#os_type ⇒ Object
-
#print_info ⇒ Object
-
#repl_plain ⇒ Object
Read-evaluate-print loop, without Reline.
-
#repl_reline ⇒ Object
Read-evaluate-print loop, with Reline.
-
#set_doc_release(release_name) ⇒ Object
-
#show(name) ⇒ Object
Show a page of Ruby documentation.
-
#show_class(partial_name) ⇒ Object
Show web page for selected class name.
-
#show_file(partial_name) ⇒ Object
Show web page for selected file name.
-
#show_help ⇒ Object
-
#show_instance_method(partial_name) ⇒ Object
Show web page for instance method name.
-
#show_readme ⇒ Object
-
#show_singleton_method(partial_name) ⇒ Object
Show web page for singleton method name.
-
#show_web_page(name, href) ⇒ Object
-
#show_web_page_for_file_or_class(partial_name, href_for_name, type) ⇒ Object
Show web page for selected file or class name.
-
#show_web_page_for_method(partial_name, href_for_name, type) ⇒ Object
Show web page for selected method name.
Constructor Details
#initialize(name = nil, options = {}) ⇒ WebRI
Returns a new instance of WebRI.
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/webri.rb', line 66
def initialize(name = nil, options = {})
capture_options(options)
self.release_name = set_doc_release(@release_name)
data_file_path = File.expand_path("../data/#{self.release_name}.json", __dir__)
json = open(data_file_path).read
@data = JSON.parse(json, create_additions: true)
make_groups
print_info if @info
if name
do_name(name)
elsif os_type == :linux && !@noreline
repl_reline
else
repl_plain
end
end
|
Instance Attribute Details
#href_for_class_name ⇒ Object
Returns the value of attribute href_for_class_name.
60
61
62
|
# File 'lib/webri.rb', line 60
def href_for_class_name
@href_for_class_name
end
|
#href_for_file_name ⇒ Object
Returns the value of attribute href_for_file_name.
60
61
62
|
# File 'lib/webri.rb', line 60
def href_for_file_name
@href_for_file_name
end
|
#href_for_instance_method_name ⇒ Object
Returns the value of attribute href_for_instance_method_name.
60
61
62
|
# File 'lib/webri.rb', line 60
def href_for_instance_method_name
@href_for_instance_method_name
end
|
#href_for_singleton_method_name ⇒ Object
Returns the value of attribute href_for_singleton_method_name.
60
61
62
|
# File 'lib/webri.rb', line 60
def href_for_singleton_method_name
@href_for_singleton_method_name
end
|
#release_name ⇒ Object
Returns the value of attribute release_name.
60
61
62
|
# File 'lib/webri.rb', line 60
def release_name
@release_name
end
|
Class Method Details
.ansi_color(s, color) ⇒ Object
601
602
603
604
|
# File 'lib/webri.rb', line 601
def self.ansi_color(s, color)
"\e[#{ANSI_COLOR[color]}m#{s}\e[0m"
end
|
.check_release_name(release_name) ⇒ Object
548
549
550
551
552
553
554
555
|
# File 'lib/webri.rb', line 548
def self.check_release_name(release_name)
release_names = Scraper.release_names
unless release_names.include?(release_name)
message = "Release must be one of #{release_names.inspect}, not #{release_name.inspect}."
puts message
exit 1
end
end
|
.class_name(s) ⇒ Object
650
651
652
|
# File 'lib/webri.rb', line 650
def self.class_name(s)
self.ansi_color("#{s}", :bright_blue)
end
|
.file_name(s) ⇒ Object
638
639
640
|
# File 'lib/webri.rb', line 638
def self.file_name(s)
self.ansi_color("#{s}", :red)
end
|
.get_webri_root_dir ⇒ Object
538
539
540
541
542
543
544
545
546
|
# File 'lib/webri.rb', line 538
def self.get_webri_root_dir
webri_root_dir = `git rev-parse --show-toplevel`.chomp
if $?.success? && File.basename(webri_root_dir) == 'webri'
return webri_root_dir
end
message = "Current working directory must be in a webri project, not #{Dir.pwd}."
puts message
exit
end
|
.instance_method_name(s) ⇒ Object
646
647
648
|
# File 'lib/webri.rb', line 646
def self.instance_method_name(s)
self.ansi_color("#{s}", :cyan)
end
|
.prompt ⇒ Object
51
52
53
|
# File 'lib/webri.rb', line 51
def self.prompt
"(Type #{WebRI.tokenq('?')} for help, #{WebRI.tokenq('exit')} to exit) #{self.webri}> "
end
|
.singleton_method_name(s) ⇒ Object
642
643
644
|
# File 'lib/webri.rb', line 642
def self.singleton_method_name(s)
self.ansi_color("#{s}", :magenta)
end
|
.string(s) ⇒ Object
614
615
616
|
# File 'lib/webri.rb', line 614
def self.string(s)
self.ansi_color("'#{s}'", :green)
end
|
.token(s) ⇒ Object
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
# File 'lib/webri.rb', line 618
def self.token(s)
color = case s
when /^[A-Z]/
:bright_blue
when /^::/
:bright_yellow
when /^#/
:bright_green
when /^ruby:/
:bright_red
else
:bright_cyan
end
self.ansi_color("#{s}", color)
end
|
.variable(s) ⇒ Object
610
611
612
|
# File 'lib/webri.rb', line 610
def self.variable(s)
self.ansi_color(s, :yellow)
end
|
.webri ⇒ Object
606
607
608
|
# File 'lib/webri.rb', line 606
def self.webri
self.ansi_color('webri', :blue)
end
|
Instance Method Details
#capture_options(options) ⇒ Object
198
199
200
201
202
203
|
# File 'lib/webri.rb', line 198
def capture_options(options)
@noop = options[:noop]
@info = options[:info]
@noreline = options[:noreline]
@release_name = options[:release_name]
end
|
#do_name(name) ⇒ Object
84
85
86
|
# File 'lib/webri.rb', line 84
def do_name(name)
show(name)
end
|
#get_boolean_answer(question) ⇒ Object
Present question; return answer.
473
474
475
476
477
|
# File 'lib/webri.rb', line 473
def get_boolean_answer(question)
print "#{question} (y or n): "
$stdout.flush
$stdin.gets.match(/y/i) ? true : false
end
|
#get_choice(situation, choices, type) ⇒ Object
305
306
307
308
309
310
311
312
313
|
# File 'lib/webri.rb', line 305
def get_choice(situation, choices, type)
puts situation
count = choices.size
if count > 20
message = "Show #{count} #{type} names?"
return nil unless get_boolean_answer(message)
end
get_choice_(choices)
end
|
#get_choice_(choices, required: false) ⇒ Object
Present choices; return choice.
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/webri.rb', line 424
def get_choice_(choices, required: false)
lines = []
index = nil
range = (0..choices.size - 1)
until range.include?(index)
choices.each_with_index do |choice, i|
s = "%6d" % i
token = WebRI.token(choice)
lines << " #{s}: #{token}"
end
while true
message = if required
'Type a number to choose: '
else
'Type a number to choose, or Return to skip: '
end
lines << message
s = lines.join("\n")
require "mkmf"
=
ENV["PAGER"] ||
if find_executable("less")
'less -RFX'
else
'more'
end
begin
IO.popen(, "w") do |io|
io.puts s
rescue Errno::EPIPE
puts message
end
end
response = $stdin.gets
case response
when /(\d+)/
index = $1.to_i
return choices[index] if index < choices.size
when "\n"
return nil unless required
else
end
end
end
end
|
#help(response) ⇒ Object
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
# File 'lib/webri.rb', line 557
def help(response)
puts WebRI::HELP
return
puts <<HELP
Type:
- #{WebRI.tokenq('exit')} to exit #{WebRI.webri}.
- #{CLASS.capitalize} name (full or partial) to see #{CLASS} names:
- #{WebRI.tokenq('Array')} (full name, not the start of other names).
- #{WebRI.tokenq('Ar')} (partial name).
- #{SINGLETON.capitalize} name (full or partial) to see #{SINGLETON} names:
- #{WebRI.tokenq('::tanh')} (full name, not the start of other names).
- #{WebRI.tokenq('::ta')} (partial name).
- #{INSTANCE.capitalize} name (full or partial) to see #{INSTANCE} names:
- #{WebRI.tokenq('#query=')} (full name, not the start of other names).
- #{WebRI.tokenq('#qu')} (partial name).
- #{FILE.capitalize}name (full or partial) to see #{FILE} names:
- #{WebRI.tokenq('ruby:syntax_rdoc')} (full name, not the start of other names).
- #{WebRI.tokenq('ruby:syntax')} (partial name).
HELP
end
|
#help_main ⇒ Object
578
579
580
|
# File 'lib/webri.rb', line 578
def help_main
end
|
#make_groups ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/webri.rb', line 88
def make_groups
self.href_for_class_name = {}
self.href_for_file_name = {}
self.href_for_singleton_method_name = {}
self.href_for_instance_method_name = {}
@data['hrefs_for_name'].group_by do |name, hrefs|
case
when name.start_with?('ruby:')
self.href_for_file_name[name] = hrefs.first
when name.start_with?('#')
self.href_for_instance_method_name[name] = hrefs
when name.start_with?('::')
self.href_for_singleton_method_name[name] = hrefs
else
self.href_for_class_name[name] = hrefs.first
end
end
end
|
#open_readme ⇒ Object
479
480
481
482
483
|
# File 'lib/webri.rb', line 479
def open_readme
url = 'https://github.com/BurdetteLamar/webri/blob/main/README.md'
uri = URI.parse(url)
open_uri('README',uri)
end
|
#open_uri(name, target_uri) ⇒ Object
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
# File 'lib/webri.rb', line 519
def open_uri(name, target_uri)
full_url = target_uri.to_s
url, fragment = full_url.split('#')
message = "Opening web page #{url}"
if fragment
message += " at method #{name}"
end
message += '.'
puts message
command = "#{opener_name} #{full_url}"
if @noop
else
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
end
end
end
|
#opener_name ⇒ Object
505
506
507
508
509
510
511
512
513
514
515
516
517
|
# File 'lib/webri.rb', line 505
def opener_name
case os_type
when :linux
'xdg-open'
when :windows
'start'
when :macos
'open'
else
message = "No opener name for #{os_type}"
raise RuntimeError(message)
end
end
|
#os_type ⇒ Object
492
493
494
495
496
497
498
499
500
501
502
503
|
# File 'lib/webri.rb', line 492
def os_type
case RbConfig::CONFIG['host_os']
when /linux|bsd|arch/
:linux
when /darwin/
:macos
when /mswin|windows|32/
:windows
else
:unknown
end
end
|
#print_info ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/webri.rb', line 177
def print_info
puts "Ruby documentation:"
puts " Release: #{release_name}"
puts " Site: #{DOC_SITE}"
puts " Snapshot: #{@data['timestamp']}"
puts "Names:"
puts format(" %5d %s", href_for_file_name.size, 'Files')
puts format(" %5d %s", href_for_class_name.size, 'Classes and modules')
count = 0
href_for_singleton_method_name.each_pair do |name, href_for_name|
count += href_for_name.size
end
puts format(" %5d %s", count, 'Singleton methods')
count = 0
href_for_instance_method_name.each_pair do |name, href_for_name|
count += href_for_name.size
end
puts format(" %5d %s", count, 'Instance methods')
exit
end
|
#repl_plain ⇒ Object
Read-evaluate-print loop, without Reline.
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/webri.rb', line 107
def repl_plain while true
$stdout.write(WebRI.prompt)
$stdout.flush
response = $stdin.gets.chomp
exit if response == 'exit'
next if response.empty?
if response.start_with?('?')
help(response)
next
end
if response.split(' ').size > 1
puts "One name at a time, please."
next
end
show(response)
end
end
|
#repl_reline ⇒ Object
Read-evaluate-print loop, with Reline.
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
|
# File 'lib/webri.rb', line 126
def repl_reline begin
stty_save = `stty -g`.chomp
rescue
end
begin
completion_words= @data['hrefs_for_name'].keys.sort
Reline.completion_proc = proc { |word|
completion_words
}
while line = Reline.readline(WebRI.prompt, true)
case line.chomp
when 'exit'
exit 0
when ''
else
if line.split(' ').size > 1
puts "One name at a time, please."
next
end
show(line)
end
end
rescue Interrupt
puts '^C'
`stty #{stty_save}` if stty_save
exit 0
end
puts
end
|
#set_doc_release(release_name) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/webri.rb', line 159
def set_doc_release(release_name)
unless release_name
s = RUBY_VERSION.split('.')
release_name ||= s[0..1].join('.')
puts "Documentation release defaulting to #{release_name} (the Ruby version you're running)."
release_name
end
release_names = Scraper.release_names
unless release_names.include?(release_name)
puts "Found no documentation release #{release_name}."
puts "Releases:"
release_name = get_choice_(release_names, required: true)
end
release_name
end
|
#show(name) ⇒ Object
Show a page of Ruby documentation.
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/webri.rb', line 278
def show(name)
case
when name.match(/^[A-Z]/)
show_class(name)
when %w[fatal fata fat fa f].include?(name)
show_class(name)
when name.start_with?('ruby:')
show_file(name)
when name.start_with?('::')
show_singleton_method(name)
when name.start_with?('#')
show_instance_method(name)
when name == '@help'
show_help
when name == '@readme'
open_readme
else
puts "No documentation available for name '#{name}'."
end
end
|
#show_class(partial_name) ⇒ Object
Show web page for selected class name.
348
349
350
|
# File 'lib/webri.rb', line 348
def show_class(partial_name)
show_web_page_for_file_or_class(partial_name, href_for_class_name, CLASS)
end
|
#show_file(partial_name) ⇒ Object
Show web page for selected file name.
353
354
355
|
# File 'lib/webri.rb', line 353
def show_file(partial_name)
show_web_page_for_file_or_class(partial_name, href_for_file_name, FILE)
end
|
#show_help ⇒ Object
414
415
416
417
|
# File 'lib/webri.rb', line 414
def show_help
puts 'Showing help.'
puts `ruby exe/webri --help`
end
|
#show_instance_method(partial_name) ⇒ Object
Show web page for instance method name.
410
411
412
|
# File 'lib/webri.rb', line 410
def show_instance_method(partial_name)
show_web_page_for_method(partial_name, href_for_instance_method_name, INSTANCE)
end
|
#show_readme ⇒ Object
419
420
421
|
# File 'lib/webri.rb', line 419
def show_readme
open_readme
end
|
#show_singleton_method(partial_name) ⇒ Object
Show web page for singleton method name.
405
406
407
|
# File 'lib/webri.rb', line 405
def show_singleton_method(partial_name)
show_web_page_for_method(partial_name, href_for_singleton_method_name, SINGLETON)
end
|
#show_web_page(name, href) ⇒ Object
486
487
488
489
490
|
# File 'lib/webri.rb', line 486
def show_web_page(name, href)
href.gsub!('::', '/')
uri = URI.parse(File.join(DOC_SITE, release_name, href))
open_uri(name, uri)
end
|
#show_web_page_for_file_or_class(partial_name, href_for_name, type) ⇒ Object
Show web page for selected file or class name.
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
# File 'lib/webri.rb', line 316
def show_web_page_for_file_or_class(partial_name, href_for_name, type)
selected_names = href_for_name.keys.select do |name|
name.start_with?(partial_name)
end
count = selected_names.size
selected_name =
case count
when 0
situation = "Found no #{type} name starting with '#{partial_name}'."
selected_name = get_choice(situation, href_for_name.keys, type)
return if selected_name.nil?
selected_name
when 1
full_name = selected_names.first
puts "Found one #{type} name starting with '#{partial_name}': #{full_name}"
if partial_name != full_name
message = "Open web page #{full_name}?"
return unless get_boolean_answer(message)
end
full_name
else
situation = "Found #{count} #{type} names starting with '#{partial_name}'."
selected_name = get_choice(situation, selected_names, type)
return if selected_name.nil?
selected_name
end
href = href_for_name[selected_name]
show_web_page(selected_name, href)
end
|
#show_web_page_for_method(partial_name, href_for_name, type) ⇒ Object
Show web page for selected method name.
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/webri.rb', line 358
def show_web_page_for_method(partial_name, href_for_name, type)
selected_names = href_for_name.keys.select do |name|
name.start_with?(partial_name)
end
count = selected_names.size
selected_name =
case count
when 0
situation = "Found no #{type} name starting with '#{partial_name}'."
selected_name = get_choice(situation, href_for_name, type)
return if selected_name.nil?
selected_name
when 1
full_name = selected_names.first
puts "Found one #{type} name starting with '#{partial_name}': #{full_name}"
if partial_name != full_name
message = "Open web page #{full_name}?"
return unless get_boolean_answer(message)
end
full_name
else
situation = "Found #{count} #{type} names starting with '#{partial_name}'."
selected_name = get_choice(situation, selected_names, type)
return if selected_name.nil?
selected_name
end
qualified_names = []
@data['classes_for_method'][selected_name].each do |class_name|
qualified_names << "#{class_name}#{selected_name}"
end
count = qualified_names.size
if count == 1
puts "Found one #{CLASS} that has method '#{selected_name}'."
qualified_name = qualified_names.first
else
situation = "Found #{count} #{type} names that have method '#{selected_name}'."
qualified_name = get_choice(situation, qualified_names, type)
return if qualified_name.nil?
end
method_href = href_for_name[selected_name]
class_name = qualified_name.sub(selected_name, '')
href = "#{class_name}.html#{method_href}"
show_web_page(selected_name, href)
end
|