11# frozen_string_literal: true
2- require 'open-uri'
3- require 'nokogiri'
4- include Nokogiri
2+ require 'rbconfig'
3+ require 'find'
54
65require_relative '../rdoc'
76
87# A class to display Ruby HTML documentation.
98class RDoc ::WebRI
109
11- # Where the documentation lives.
12- ReleasesUrl = 'https://docs.ruby-lang.org/en/'
10+ def initialize ( target_name , options )
11+ ruby_exe_filepath = RbConfig . ruby
12+ ruby_installation_dirpath = File . dirname ( File . dirname ( ruby_exe_filepath ) )
13+ ri_filepaths = get_ri_filepaths ( ruby_installation_dirpath )
14+ html_filepaths = get_html_filepaths ( ruby_installation_dirpath )
15+ filepaths = { }
16+ missing_html = [ ]
17+ unused_ri = [ ]
18+ ri_filepaths . each do |ri_filepath |
19+ next if ri_filepath == 'cache.ri'
20+ filepath = ri_filepath . sub ( '.ri' , '.html' )
21+ html_filepath = case
22+ when filepath . match ( /-c\. html/ )
23+ dirname = File . dirname ( filepath )
24+ basename = File . basename ( filepath )
25+ name = basename . sub ( '-c.html' , '' )
26+ dirname + '.html'
27+ when filepath . match ( /-i\. html/ )
28+ 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'
34+ when File . basename ( filepath ) . match ( /^page-/ )
35+ filepath . sub ( 'page-' , '' )
36+ else
37+ raise filepath
38+ end
39+ unless html_filepaths . include? ( html_filepath )
40+ missing_html . push ( html_filepath )
41+ unused_ri . push ( ri_filepath )
42+ end
43+ filepaths [ ri_filepath ] = html_filepath
44+ end
45+ # puts missing_html.uniq
46+ # puts unused_ri
1347
48+ puts filepaths . keys . sort . take ( 200 )
49+ puts target_name
50+ return
1451
15- def initialize ( target_name , options )
16- release_name = get_release_name ( options [ :release ] )
17- entries = get_entries ( release_name )
18- # target_entries = entries[target_name]
1952 target_entries = [ ]
2053 entries . select do |name , value |
2154 if name . match ( Regexp . new ( target_name ) )
@@ -32,7 +65,7 @@ def initialize(target_name, options)
3265 end
3366 end
3467
35- class Entry
68+ class Entry
3669
3770 attr_accessor :type , :full_name , :href
3871
@@ -44,107 +77,28 @@ def initialize(type, full_name, href)
4477
4578 end
4679
47- def get_release_name ( requested_release_name )
48- if requested_release_name . nil?
49- puts "Selecting documentation release based on installed Ruby (#{ RUBY_VERSION } )."
50- requested_release_name = RUBY_VERSION
51- end
52- available_release_names = [ ]
53- html = URI . open ( ReleasesUrl )
54- @doc = Nokogiri ::HTML ( html )
55- link_eles = @doc . xpath ( "//a" )
56- link_eles . each do |link_ele |
57- text = link_ele . text
58- next if text . match ( 'outdated' )
59- release_name = text . sub ( 'Ruby ' , '' )
60- available_release_names . push ( release_name )
61- end
62- release_name = nil
63- if available_release_names . include? ( requested_release_name )
64- release_name = requested_release_name
65- else
66- available_release_names . each do |name |
67- if requested_release_name . start_with? ( name )
68- release_name = name
69- break
70- end
71- end
72- end
73- if release_name . nil?
74- puts "Could not find documentation for release '#{ requested_release_name } ': available releases:"
75- release_name = get_choice ( available_release_names )
76- end
77- puts "Selected documentation release #{ release_name } ."
78- release_name
79- end
80-
81- def get_entries ( release_name )
82- toc_url = File . join ( ReleasesUrl , release_name , 'table_of_contents.html' )
83- html = URI . open ( toc_url )
84- doc = Nokogiri ::HTML ( html )
85- entries = { }
86- %w[ file class module method ] . each do |type |
87- add_entries ( entries , doc , type )
88- end
89- entries
80+ def get_ri_filepaths ( ruby_installation_dirpath )
81+ # Directory containing filetree of .ri files installed by RI.
82+ ri_dirpath = File . join ( ruby_installation_dirpath , 'share' , 'ri' , RUBY_ENGINE_VERSION , 'system' )
83+ ri_filepaths = [ ]
84+ Find . find ( ri_dirpath ) . each do |path |
85+ next unless path . end_with? ( '.ri' )
86+ path . sub! ( ri_dirpath + '/' , '' )
87+ ri_filepaths . push ( path )
9088 end
91-
92- def add_entries ( entries , doc , type )
93- xpath = "//li[@class='#{ type } ']"
94- li_eles = doc . xpath ( xpath )
95- li_eles . each do |li_ele |
96- a_ele = li_ele . xpath ( './a' ) . first
97- short_name = a_ele . text
98- full_name = if type == 'method'
99- method_span_ele = li_ele . xpath ( './span' ) . first
100- class_name = method_span_ele . text
101- class_name + short_name
102- else
103- short_name
104- end
105- href = a_ele . attributes [ 'href' ] . value
106- entry = Entry . new ( type , full_name , href )
107- entries [ short_name ] ||= [ ]
108- entries [ short_name ] . push ( entry )
109- next unless type == 'method'
110- # We want additional entries for full name, bare name, and dot name.
111- bare_name = short_name . sub ( /^::/ , '' ) . sub ( /^#/ , '' )
112- dot_name = '.' + bare_name
113- [ full_name , bare_name , dot_name ] . each do |other_name |
114- entries [ other_name ] ||= [ ]
115- entries [ other_name ] . push ( entry )
116- end
117- end
89+ ri_filepaths
11890 end
11991
120- def get_target_url ( target_entries , release_name )
121- target_entry = nil
122- if target_entries . size == 1
123- target_entry = target_entries . first
124- else
125- sorted_target_entries = target_entries . sort_by { |entry | entry . full_name }
126- full_names = sorted_target_entries . map { |entry | "#{ entry . full_name } (#{ entry . type } )" }
127- index = get_choice_index ( full_names )
128- target_entry = sorted_target_entries [ index ]
92+ def get_html_filepaths ( ruby_installation_dirpath )
93+ # Directory containing filetree of .html files installed by RI.
94+ html_dirpath = File . join ( ruby_installation_dirpath , *%w[ share doc ruby html ] )
95+ filepaths = [ ]
96+ Find . find ( html_dirpath ) . each do |path |
97+ next unless path . end_with? ( '.html' )
98+ path . sub! ( html_dirpath + '/' , '' )
99+ filepaths . push ( path )
129100 end
130- File . join ( ReleasesUrl , release_name , target_entry . href ) . to_s
131- end
132-
133- def open_url ( target_url )
134- host_os = RbConfig ::CONFIG [ 'host_os' ]
135- executable_name = case host_os
136- when /linux|bsd/
137- 'xdg-open'
138- when /darwin/
139- 'open'
140- when /32$/
141- 'start'
142- else
143- message = "Unrecognized host OS: '#{ host_os } '."
144- raise RuntimeError . new ( message )
145- end
146- command = "#{ executable_name } #{ target_url } "
147- system ( command )
101+ filepaths
148102 end
149103
150104 def get_choice ( choices )
0 commit comments