Skip to content

Add version selector #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/rdoc/generator/markup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ def canonical_url
end
end

##
# URL's to other versions for this object.

def version_urls
options = @store.options
if options.version_roots
options.version_roots.map do |version, root|
if path
url = File.join(root, path.to_s)
[version, url]
else
[version, root]
end
end
end
end
end

class RDoc::CodeObject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="<%= rel_prefix %>/index.html" rel="home">Home</a>
<% if version_urls = @options.version_roots %>
<% version_urls = current.version_urls if defined?(current) %>
<select id="version-select">
<% version_urls.each do |version, url| %>
<option value="<%= url %>"><%= version %></option>
<% end %>
</select>
<% end %>
</h2>

<div id="table-of-contents-navigation">
Expand Down
8 changes: 8 additions & 0 deletions lib/rdoc/generator/template/darkfish/js/darkfish.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function showSource( e ) {
}
};

function hookVersionSelect() {
var versionSelect = document.querySelector('#version-select');
versionSelect.addEventListener('change', function (e) {
window.location.href = e.target.value;
});
};

function hookSourceViews() {
document.querySelectorAll('.method-source-toggle').forEach(function (codeObject) {
codeObject.addEventListener('click', showSource);
Expand Down Expand Up @@ -113,6 +120,7 @@ function hookSidebar() {
}

document.addEventListener('DOMContentLoaded', function() {
hookVersionSelect();
hookSourceViews();
hookSearch();
hookFocus();
Expand Down
7 changes: 7 additions & 0 deletions lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ class RDoc::Options

attr_accessor :canonical_root

##
# The root URLs of other versions of the documentation

attr_accessor :version_roots

def initialize(loaded_options = nil) # :nodoc:
init_ivars
override loaded_options if loaded_options
Expand Down Expand Up @@ -435,6 +440,7 @@ def init_ivars # :nodoc:
@class_module_path_prefix = nil
@file_path_prefix = nil
@canonical_root = nil
@version_roots = nil
end

def init_with(map) # :nodoc:
Expand Down Expand Up @@ -499,6 +505,7 @@ def override(map) # :nodoc:
@autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
@apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')
@canonical_root = map['canonical_root'] if map.has_key?('canonical_root')
@version_roots = map['version_roots'] if map.has_key?('version_roots')

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

Expand Down
33 changes: 33 additions & 0 deletions test/rdoc/rdoc_generator_darkfish_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,39 @@ def test_canonical_url_for_rdoc_files
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/CONTRIBUTING_rdoc.html">')
end

def test_version_select_for_index
@store.options.version_roots = @options.version_roots = {
"master" => "https://docs.ruby-lang.org/en/master/",
"3.4" => "https://docs.ruby-lang.org/en/3.4/"
}
@g.generate

content = File.binread("index.html")

assert_include(content, '<select id="version-select">')
assert_include(content, '<option value="https://docs.ruby-lang.org/en/master/">master</option>')
assert_include(content, '<option value="https://docs.ruby-lang.org/en/3.4/">3.4</option>')
end

def test_version_select_for_classes
top_level = @store.add_file("file.rb")
top_level.add_class(@klass.class, @klass.name)
inner = @klass.add_class(RDoc::NormalClass, "Inner")

@store.options.version_roots = @options.version_roots = {
"master" => "https://docs.ruby-lang.org/en/master/",
"3.4" => "https://docs.ruby-lang.org/en/3.4/"
}
@g.generate

content = File.binread("Klass/Inner.html")

assert_include(content, '<select id="version-select">')
assert_include(content, '<option value="https://docs.ruby-lang.org/en/master/Klass/Inner.html">master</option>')
assert_include(content, '<option value="https://docs.ruby-lang.org/en/3.4/Klass/Inner.html">3.4</option>')
end


##
# Asserts that +filename+ has a link count greater than 1 if hard links to
# @tmpdir are supported.
Expand Down
1 change: 1 addition & 0 deletions test/rdoc/rdoc_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def test_to_yaml
'class_module_path_prefix' => nil,
'file_path_prefix' => nil,
'canonical_root' => nil,
'version_roots' => nil,
}

assert_equal expected, coder
Expand Down
Loading