11# frozen_string_literal: true
22require 'rbconfig'
33require 'find'
4+ require 'cgi'
45
56require_relative '../rdoc'
67
@@ -12,71 +13,62 @@ def initialize(target_name, options)
1213 ruby_installation_dirpath = File . dirname ( File . dirname ( ruby_exe_filepath ) )
1314 ri_filepaths = get_ri_filepaths ( ruby_installation_dirpath )
1415 html_filepaths = get_html_filepaths ( ruby_installation_dirpath )
15- filepaths = { }
16+ target_urls = { }
1617 missing_html = [ ]
1718 unused_ri = [ ]
1819 ri_filepaths . each do |ri_filepath |
1920 next if ri_filepath == 'cache.ri'
2021 filepath = ri_filepath . sub ( '.ri' , '.html' )
21- html_filepath = case
22- when filepath . match ( /-c\. html/ )
22+ name , target_url = case
23+ when filepath . match ( /-c\. html/ ) # Class method.
2324 dirname = File . dirname ( filepath )
24- basename = File . basename ( filepath )
25- name = basename . sub ( '-c.html' , '' )
26- dirname + '.html'
27- when filepath . match ( /-i\. html/ )
25+ method_name = CGI . unescape ( File . basename ( filepath ) . sub ( '-c.html' , '' ) )
26+ target_url = dirname + '.html#method-c-' + method_name
27+ name = dirname . gsub ( '/' , '::' ) + '::' + method_name
28+ [ name , target_url ]
29+ when filepath . match ( /-i\. html/ ) # Instance method.
2830 dirname = File . dirname ( filepath )
29- basename = File . basename ( filepath )
30- name = basename . sub ( '-i.html' , '' )
31- dirname + '.html'
32- when filepath . match ( /\/ cdesc-/ )
33- File . dirname ( filepath ) + '.html'
31+ method_name = CGI . unescape ( File . basename ( filepath ) . sub ( '-i.html' , '' ) )
32+ target_url = dirname + '.html#method-i-' + method_name
33+ name = dirname . gsub ( '/' , '::' ) + '#' + method_name
34+ [ name , target_url ]
35+ when filepath . match ( /\/ cdesc-/ ) # Class.
36+ target_url = File . dirname ( filepath ) + '.html'
37+ name = target_url . gsub ( '/' , '::' ) . sub ( '.html' , '' )
38+ [ name , target_url ]
3439 when File . basename ( filepath ) . match ( /^page-/ )
35- filepath . sub ( 'page-' , '' )
40+ target_url = filepath . sub ( 'page-' , '' ) # File.
41+ name = target_url . sub ( '.html' , '' ) . sub ( /_rdoc$/ , '.rdoc' ) . sub ( /_md$/ , '.md' )
42+ [ name , target_url ]
3643 else
3744 raise filepath
3845 end
39- unless html_filepaths . include? ( html_filepath )
40- missing_html . push ( html_filepath )
46+ unless html_filepaths . include? ( target_url )
47+ missing_html . push ( target_url )
4148 unused_ri . push ( ri_filepath )
4249 end
43- filepaths [ ri_filepath ] = html_filepath
50+ target_urls [ name ] = target_url
4451 end
4552 # puts missing_html.uniq
4653 # puts unused_ri
4754
48- puts filepaths . keys . sort . take ( 200 )
49- puts target_name
50- return
51-
52- target_entries = [ ]
53- entries . select do |name , value |
55+ selected_urls = { }
56+ target_urls . select do |name , value |
5457 if name . match ( Regexp . new ( target_name ) )
55- value . each do |x |
56- target_entries << x
57- end
58+ selected_urls [ name ] = value
5859 end
5960 end
60- if target_entries . empty?
61+ case selected_urls . size
62+ when 0
6163 puts "No documentation found for #{ target_name } ."
64+ when 1
65+ url = selected_urls . first [ 1 ]
66+ open_url ( url )
6267 else
63- target_url = get_target_url ( target_entries , release_name )
64- open_url ( target_url )
68+ p get_choice ( selected_urls . keys )
6569 end
6670 end
6771
68- class Entry
69-
70- attr_accessor :type , :full_name , :href
71-
72- def initialize ( type , full_name , href )
73- self . type = type
74- self . full_name = full_name
75- self . href = href
76- end
77-
78- end
79-
8072def get_ri_filepaths ( ruby_installation_dirpath )
8173 # Directory containing filetree of .ri files installed by RI.
8274 ri_dirpath = File . join ( ruby_installation_dirpath , 'share' , 'ri' , RUBY_ENGINE_VERSION , 'system' )
@@ -121,4 +113,23 @@ def get_choice_index(choices)
121113 index
122114 end
123115
116+ def open_url ( target_url )
117+ host_os = RbConfig ::CONFIG [ 'host_os' ]
118+ executable_name = case host_os
119+ when /linux|bsd/
120+ 'xdg-open'
121+ when /darwin/
122+ 'open'
123+ when /32$/
124+ 'start'
125+ else
126+ message = "Unrecognized host OS: '#{ host_os } '."
127+ raise RuntimeError . new ( message )
128+ end
129+ command = "#{ executable_name } #{ target_url } "
130+ p command
131+ return
132+ system ( command )
133+ end
134+
124135end
0 commit comments