Skip to content

Commit fe4ed57

Browse files
committed
Add version selector
To make it easier to switch to documentation of other versions, add a select box to select other versions. For example, the versions can be defined in `.rdoc_options`: ```yaml version_roots: latest: https://docs.ruby-lang.org/en/ master: https://docs.ruby-lang.org/en/master/ 3.4: https://docs.ruby-lang.org/en/3.4/ ```
1 parent 9684f85 commit fe4ed57

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

lib/rdoc/generator/markup.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ def canonical_url
6767
end
6868
end
6969

70+
##
71+
# URL's to other versions for this object.
72+
73+
def version_urls
74+
options = @store.options
75+
if options.version_roots
76+
options.version_roots.map do |version, root|
77+
if path
78+
url = File.join(root, path.to_s)
79+
[version, url]
80+
else
81+
[version, root]
82+
end
83+
end
84+
end
85+
end
7086
end
7187

7288
class RDoc::CodeObject

lib/rdoc/generator/template/darkfish/_sidebar_navigation.rhtml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
22
<h2>
33
<a href="<%= rel_prefix %>/index.html" rel="home">Home</a>
4+
<% if version_urls = @options.version_roots %>
5+
<% version_urls = current.version_urls if defined?(current) %>
6+
<select id="version-select">
7+
<% version_urls.each do |version, url| %>
8+
<option value="<%= url %>"><%= version %></option>
9+
<% end %>
10+
</select>
11+
<% end %>
412
</h2>
513

614
<div id="table-of-contents-navigation">

lib/rdoc/generator/template/darkfish/js/darkfish.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ function showSource( e ) {
3333
}
3434
};
3535

36+
function hookVersionSelect() {
37+
var versionSelect = document.querySelector('#version-select');
38+
versionSelect.addEventListener('change', function (e) {
39+
window.location.href = e.target.value;
40+
});
41+
};
42+
3643
function hookSourceViews() {
3744
document.querySelectorAll('.method-source-toggle').forEach(function (codeObject) {
3845
codeObject.addEventListener('click', showSource);
@@ -113,6 +120,7 @@ function hookSidebar() {
113120
}
114121

115122
document.addEventListener('DOMContentLoaded', function() {
123+
hookVersionSelect();
116124
hookSourceViews();
117125
hookSearch();
118126
hookFocus();

lib/rdoc/options.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ class RDoc::Options
378378

379379
attr_accessor :canonical_root
380380

381+
##
382+
# The root URLs of other versions of the documentation
383+
384+
attr_accessor :version_roots
385+
381386
def initialize(loaded_options = nil) # :nodoc:
382387
init_ivars
383388
override loaded_options if loaded_options
@@ -435,6 +440,7 @@ def init_ivars # :nodoc:
435440
@class_module_path_prefix = nil
436441
@file_path_prefix = nil
437442
@canonical_root = nil
443+
@version_roots = nil
438444
end
439445

440446
def init_with(map) # :nodoc:
@@ -499,6 +505,7 @@ def override(map) # :nodoc:
499505
@autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
500506
@apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')
501507
@canonical_root = map['canonical_root'] if map.has_key?('canonical_root')
508+
@version_roots = map['version_roots'] if map.has_key?('version_roots')
502509

503510
@warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')
504511

test/rdoc/rdoc_generator_darkfish_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,39 @@ def test_canonical_url_for_rdoc_files
562562
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/CONTRIBUTING_rdoc.html">')
563563
end
564564

565+
def test_version_select_for_index
566+
@store.options.version_roots = @options.version_roots = {
567+
"master" => "https://docs.ruby-lang.org/en/master/",
568+
"3.4" => "https://docs.ruby-lang.org/en/3.4/"
569+
}
570+
@g.generate
571+
572+
content = File.binread("index.html")
573+
574+
assert_include(content, '<select id="version-select">')
575+
assert_include(content, '<option value="https://docs.ruby-lang.org/en/master/">master</option>')
576+
assert_include(content, '<option value="https://docs.ruby-lang.org/en/3.4/">3.4</option>')
577+
end
578+
579+
def test_version_select_for_classes
580+
top_level = @store.add_file("file.rb")
581+
top_level.add_class(@klass.class, @klass.name)
582+
inner = @klass.add_class(RDoc::NormalClass, "Inner")
583+
584+
@store.options.version_roots = @options.version_roots = {
585+
"master" => "https://docs.ruby-lang.org/en/master/",
586+
"3.4" => "https://docs.ruby-lang.org/en/3.4/"
587+
}
588+
@g.generate
589+
590+
content = File.binread("Klass/Inner.html")
591+
592+
assert_include(content, '<select id="version-select">')
593+
assert_include(content, '<option value="https://docs.ruby-lang.org/en/master/Klass/Inner.html">master</option>')
594+
assert_include(content, '<option value="https://docs.ruby-lang.org/en/3.4/Klass/Inner.html">3.4</option>')
595+
end
596+
597+
565598
##
566599
# Asserts that +filename+ has a link count greater than 1 if hard links to
567600
# @tmpdir are supported.

test/rdoc/rdoc_options_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def test_to_yaml
9090
'class_module_path_prefix' => nil,
9191
'file_path_prefix' => nil,
9292
'canonical_root' => nil,
93+
'versions' => nil,
9394
}
9495

9596
assert_equal expected, coder

0 commit comments

Comments
 (0)