Class: WebRI

Inherits:
Object
  • Object
show all
Defined in:
lib/webri.rb,
lib/webri/version.rb

Overview

A class to display Ruby online HTML documentation.

Defined Under Namespace

Classes: ClassEntry, Entry, FileEntry, InstanceMethodEntry, SingletonMethodEntry

Constant Summary collapse

DocSite =

Site of the official documentation.

'https://docs.ruby-lang.org/en/'
VERSION =
"1.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WebRI

Get the info from the Ruby doc site’s table of contents and build our @index_for_type.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webri.rb', line 35

def initialize(options = {})
  capture_options(options)
  set_doc_release
  get_toc_html
  build_indexes
  print_info if @info
  print @noreline
  if os_type == :linux && !@noreline
    repl_reline
  else
    repl_plain
  end
end

Instance Method Details

#build_indexesObject



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
# File 'lib/webri.rb', line 147

def build_indexes
  # Index for each type of entry.
  # Each index has a hash; key is name, value is array of URIs.
  @index_for_type = {
    class: {}, # Has both classes and modules.
    singleton_method: {},
    instance_method: {},
    page: {},
  }
  # Iterate over the lines of the TOC page.
  lines = @toc_html.split("\n")
  i = 0
  while i < lines.count
    item_line = lines[i]
    i += 1
    next unless item_line.match('<li class="(\w+)"')
    class_attr_value = $1 # Save for later.
    # We have a pair of lines such as:
    #     <li class="file">
    #       <a href="COPYING.html">COPYING</a>
    anchor_line = lines[i] # Second line of pair.
    # Consume anchor_line.
    i += 1
    # We capture variables thus:
    # - +type+ is the value of attribute 'class'.
    # - +path+ is the value of attribute 'href'.
    # - +full_name+ is the HTML text.
    type = class_attr_value
    _, path, rest = anchor_line.split('"')
    full_name = rest.split(/<|>/)[1]
    full_name = CGI.unescapeHTML(full_name)
    case type
    when 'class', 'module'
      index = @index_for_type[:class]
      if index.include?(full_name)
        entry = index[full_name]
      else
        entry = ClassEntry.new(full_name)
        index[full_name] = entry
      end
      entry.paths.push(path) unless entry.paths.include?(path)
    when 'file'
      index = @index_for_type[:page]
      if index.include?(full_name)
        entry = index[full_name]
      else
        entry = FileEntry.new(full_name)
        index[full_name] = entry
      end
      entry.paths.push(path) unless entry.paths.include?(path)
    when 'method'
      case anchor_line
      when /method-c-/
        index = @index_for_type[:singleton_method]
        if index.include?(full_name)
          entry = index[full_name]
        else
          entry = SingletonMethodEntry.new(full_name)
          index[full_name] = entry
        end
        entry.paths.push(path) unless entry.paths.include?(path)
      when /method-i-/
        index = @index_for_type[:instance_method]
        if index.include?(full_name)
          entry = index[full_name]
        else
          entry = InstanceMethodEntry.new(full_name)
          index[full_name] = entry
        end
        entry.paths.push(path) unless entry.paths.include?(path)
      else
        fail anchor_line
      end
    else
      fail class_attr_val
    end
  end
end

#capture_options(options) ⇒ Object



226
227
228
229
230
231
# File 'lib/webri.rb', line 226

def capture_options(options)
  @noop = options[:noop]
  @info = options[:info]
  @noreline = options[:noreline]
  @doc_release = options[:release]
end

#get_boolean_answer(question) ⇒ Object

Present question; return answer.



575
576
577
578
579
# File 'lib/webri.rb', line 575

def get_boolean_answer(question)
  print "#{question} (y or n):  "
  $stdout.flush
  $stdin.gets.match(/y/i) ? true : false
end

#get_choice(choices) ⇒ Object

Present choices; return choice.



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/webri.rb', line 550

def get_choice(choices)
  index = nil
  range = (0..choices.size - 1)
  until range.include?(index)
    choices.each_with_index do |choice, i|
      s = "%6d" % i
      puts "  #{s}:  #{choice}"
    end
    while true
      print "Type a number to choose, or Return to skip:  "
      $stdout.flush
      response = $stdin.gets
      case response
      when /(\d+)/
        return choices[$1.to_i]
      when "\n"
        return nil
      else

      end
    end
  end
end

#get_toc_htmlObject



233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/webri.rb', line 233

def get_toc_html
  # Construct the doc release; e.g., '3.4'.
  # Get the doc table of contents as a temp file.
  @toc_url = DocSite + @doc_release + '/table_of_contents.html'
  begin
    toc_file = URI.open(@toc_url)
    @toc_html = toc_file.read
  rescue Socket::ResolutionError => x
    message = "#{x.class}: #{x.message}\nPossibly not connected to internet."
    $stderr.puts(message)
    exit
  end
end

#open_page(name, target_uri) ⇒ Object

Open URL in browser.



588
589
590
591
# File 'lib/webri.rb', line 588

def open_page(name, target_uri)
  uri = URI.parse(File.join(DocSite, @doc_release, target_uri.to_s))
  open_uri(name, uri)
end

#open_readmeObject



581
582
583
584
585
# File 'lib/webri.rb', line 581

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



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'lib/webri.rb', line 620

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
    puts "Command: '#{command}'"
  else
    system(command)
  end
end

#opener_nameObject



606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/webri.rb', line 606

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_typeObject



593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/webri.rb', line 593

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


136
137
138
139
140
141
142
143
144
145
# File 'lib/webri.rb', line 136

def print_info
  puts "Ruby documentation release:  #{@doc_release}"
  puts "Ruby documentation URL:      #{@toc_url}"
  puts "Executable to open page:     #{opener_name}"
  puts "Names:"
  @index_for_type.each_pair do |type, items|
    puts format("  %5d %s names", items.count, type)
  end
  exit
end

#repl_plainObject

Read-evaluate-print loop, without Reline.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/webri.rb', line 49

def repl_plain # Read-evaluate-print loop, without Reline.
  while true
    $stdout.write('webri> ')
    $stdout.flush
    response = $stdin.gets.chomp
    exit if response == 'exit'
    next if response.empty?
    if response.split(' ').size > 1
      puts "One name at a time, please."
      next
    end
    show(response)
  end
end

#repl_relineObject

Read-evaluate-print loop, with Reline.



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
# File 'lib/webri.rb', line 64

def repl_reline # Read-evaluate-print loop, with Reline.
  begin
    stty_save = `stty -g`.chomp
  rescue
  end

  begin
    completion_words= []
    @index_for_type.each_pair do |type, index|
      if type == :page
        completion_words += index.keys.map {|name| 'ruby:' + name }
      else
        completion_words += index.keys
      end
    end
    Reline.completion_proc = proc { |word|
      completion_words
    }
    while line = Reline.readline("webri> ", true)
      case line.chomp
      when 'exit'
        exit 0
      when ''
        # NOOP
      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_releaseObject



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
# File 'lib/webri.rb', line 104

def set_doc_release
  supported_releases = []
  unsupported_releases = []
  master_release = nil
  io = URI.open('https://docs.ruby-lang.org/en/')
  lines = io.readlines
  lines.each do |line|
    next unless line.match(/<a/)
    doc = REXML::Document.new(line)
    _, release, end_of_support = doc.root.text.split(' ')
    break if release.start_with?('2')
    if end_of_support
      unsupported_releases.push(release)
    elsif release == 'master'
      master_release = release
    else
      supported_releases.push(release)
    end
  end
  all_releases = [master_release] + supported_releases + unsupported_releases
  if @doc_release
    unless all_releases.include?(@doc_release)
      puts "Unknown documentation release:  #{@doc_release}"
      puts "Available releases: #{all_releases.join(' ')}"
      exit
    end
  else
    a = RUBY_VERSION.split('.')
    @doc_release ||= a[0..1].join('.')
  end
end

#show(name) ⇒ Object

Show a page of Ruby documentation.



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
# File 'lib/webri.rb', line 320

def show(name)
  # Figure out what's asked for.
  case
  when name.match(/^[A-Z]/)
    show_class(name, @index_for_type[:class])
  when %w[fatal fata fat fa f].include?(name)
    show_class(name, @index_for_type[:class])
  when name.start_with?('ruby:')
    show_file(name, @index_for_type[:page])
  when name.start_with?('::')
    show_singleton_method(name, @index_for_type[:singleton_method])
  when name.start_with?('#')
    show_instance_method(name, @index_for_type[:instance_method])
  when name == '@help'
    show_help
  when name == '@readme'
    open_readme
  # when name.start_with?('.')
  #   show_method(name, @index_for_type[:singleton_method], @index_for_type[:instance_method])
  # when name.match(/^[a-z]/)
  #   show_method(name, @index_for_type[:singleton_method], @index_for_type[:instance_method])
  else
    puts "No documentation available for name '#{name}'."
  end
end

#show_class(name, class_index) ⇒ Object

Show class.



347
348
349
350
351
352
353
354
355
356
357
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
# File 'lib/webri.rb', line 347

def show_class(name, class_index)
  all_entries = @index_for_type[:class]
  all_choices = ClassEntry.choices(all_entries)
  # Find entries whose names that start with name.
  selected_entries = all_entries.select do |key, value|
    key.start_with?(name)
  end
  # Find paths for selected_choices
  selected_paths = []
  selected_entries.each_pair do |name, entry|
    entry.paths.each do |path|
      selected_paths.push(path)
    end
  end
  case selected_paths.size
  when 1
    selected_choices = ClassEntry.choices(selected_entries)
    choice = selected_choices.keys.first
    path = selected_choices.values.first
    puts "Found one class/module name starting with '#{name}'\n  #{choice}"
    full_name = ClassEntry.full_name_for_choice(choice)
    if name != full_name
      message = "Open page #{path}?"
      return unless get_boolean_answer(message)
    end
    path
  when 0
    puts "Found no class/module name starting with '#{name}'."
    message = "Show #{all_choices.size} class/module names?"
    return unless get_boolean_answer(message)
    choice_index = get_choice(all_choices.keys)
    return if choice_index.nil?
    path = all_choices[choice_index]
  else
    selected_choices = ClassEntry.choices(selected_entries)
    puts "Found #{selected_choices.size} class/module names starting with '#{name}'."
    message = "Show #{selected_choices.size} class/module names?'"
    return unless get_boolean_answer(message)
    key = get_choice(selected_choices.keys)
    return if key.nil?
    path = selected_choices[key]
  end
  uri = Entry.uri(path)
  open_page(name, uri)
end

#show_file(name, file_index) ⇒ Object

Show page.



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/webri.rb', line 394

def show_file(name, file_index)
  # Target page is a free-standing page such as 'COPYING'.
  name = name.sub(/^ruby:/, '') # Discard leading 'ruby:'
  all_entries = @index_for_type[:page]
  all_choices = FileEntry.choices(all_entries)
  # Find entries whose names that start with name.
  selected_entries = all_entries.select do |key, value|
    key.start_with?(name)
  end
  # Find paths for selected_choices
  selected_paths = []
  selected_entries.each_pair do |name, entry|
    entry.paths.each do |path|
      selected_paths.push(path)
    end
  end
  case selected_paths.size
  when 1
    selected_choices = FileEntry.choices(selected_entries)
    choice = selected_choices.keys.first
    path = selected_choices.values.first
    puts "Found one page name starting with '#{name}'\n  #{choice}"
    full_name = FileEntry.full_name_for_choice(choice)
    if name != full_name
      message = "Open page #{path}?"
      return unless get_boolean_answer(message)
    end
    path
  when 0
    puts "Found no page name starting with '#{name}'."
    message = "Show names of all #{all_choices.size} pages?"
    return unless get_boolean_answer(message)
    key = get_choice(all_choices.keys)
    return if key.nil?
    path = all_choices[key]
  else
    selected_choices = FileEntry.choices(selected_entries)
    puts "Found #{selected_choices.size} page names starting with '#{name}'."
    message = "Show #{selected_choices.size} names?'"
    return unless get_boolean_answer(message)
    key = get_choice(selected_choices.keys)
    return if key.nil?
    path = selected_choices[key]
  end
  uri = Entry.uri(path)
  open_page(name, uri)
end

#show_helpObject



540
541
542
543
# File 'lib/webri.rb', line 540

def show_help
  puts 'Showing help.'
  puts `ruby bin/webri --help`
end

#show_instance_method(name, instance_method_index) ⇒ Object

Show instance method.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/webri.rb', line 492

def show_instance_method(name, instance_method_index)
  # Target page is an instance method such as #to_s.
  all_entries = @index_for_type[:instance_method]
  all_choices = InstanceMethodEntry.choices(all_entries)
  # Find entries whose names that start with name.
  selected_entries = all_entries.select do |key, value|
    key.start_with?(name)
  end
  # Find paths for selected_choices
  selected_paths = []
  selected_entries.each_pair do |name, entry|
    entry.paths.each do |path|
      selected_paths.push(path)
    end
  end
  selected_choices = InstanceMethodEntry.choices(selected_entries)
  case selected_paths.size
  when 1
    full_name = selected_entries.keys.first
    path = selected_choices.values.first
    puts "Found one instance method name starting with '#{name}'\n  #{full_name}"
    if name != full_name
      uri = URI.parse(path)
      message = "Open page #{uri.path} at method #{full_name}?"
      return unless get_boolean_answer(message)
    end
    path
  when 0
    puts "Found no instance method name starting with '#{name}'."
    message = "Show names of all #{all_choices.size} instance methods?"
    return unless get_boolean_answer(message)
    choice = get_choice(all_choices.keys)
    return if choice.nil?
    path = all_choices[choice]
    full_name = InstanceMethodEntry.full_name_for_choice(choice)
  else
    puts "Found #{selected_paths.size} instance method names starting with '#{name}'."
    message = "Show #{selected_paths.size} names?'"
    return unless get_boolean_answer(message)
    choice = get_choice(selected_choices.keys)
    return if choice.nil?
    path = all_choices[choice]
    full_name = InstanceMethodEntry.full_name_for_choice(choice)
  end
  uri = Entry.uri(path)
  open_page(full_name, uri)
end

#show_readmeObject



545
546
547
# File 'lib/webri.rb', line 545

def show_readme
  open_readme
end

#show_singleton_method(name, singleton_method_index) ⇒ Object

Show singleton method.



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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/webri.rb', line 443

def show_singleton_method(name, singleton_method_index)
  # Target page is a singleton method such as ::new.
  all_entries = @index_for_type[:singleton_method]
  all_choices = SingletonMethodEntry.choices(all_entries)
  # Find entries whose names that start with name.
  selected_entries = all_entries.select do |key, value|
    key.start_with?(name)
  end
  # Find paths for selected_choices
  selected_paths = []
  selected_entries.each_pair do |name, entry|
    entry.paths.each do |path|
      selected_paths.push(path)
    end
  end
  selected_choices = SingletonMethodEntry.choices(selected_entries)
  case selected_paths.size
  when 1
    full_name = selected_entries.keys.first
    path = selected_choices.values.first
    puts "Found one singleton method name starting with '#{name}'\n  #{full_name}"
    if name != full_name
      uri = URI.parse(path)
      message = "Open page #{uri.path} at method #{full_name}?"
      return unless get_boolean_answer(message)
    end
    path
  when 0
    puts "Found no singleton method name starting with '#{name}'."
    message = "Show names of all #{all_choices.size} singleton methods?"
    return unless get_boolean_answer(message)
    choice = get_choice(all_choices.keys)
    return if choice.nil?
    full_name = SingletonMethodEntry.full_name_for_choice(choice)
    path = all_choices[choice]
  else
    puts "Found #{selected_paths.size} singleton method names starting with '#{name}'."
    message = "Show #{selected_paths.size} names?'"
    return unless get_boolean_answer(message)
    choice = get_choice(selected_choices.keys)
    return if choice.nil?
    full_name = SingletonMethodEntry.full_name_for_choice(choice)
    path = all_choices[choice]
  end
  uri = Entry.uri(path)
  open_page(full_name, uri)
end