Skip to content

Commit

Permalink
Merge pull request #973 from unasuke/breadcrumb
Browse files Browse the repository at this point in the history
Add breadcrumb list
  • Loading branch information
st0012 authored Feb 16, 2025
2 parents 03f3110 + c6b4fbb commit a11e6b9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/rdoc/code_object/class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,25 @@ def full_name
end
end

##
# Return array of full_name splitted by +::+.

def nesting_namespaces
@namespaces ||= full_name.split("::").reject(&:empty?)
end

##
# Return array of fully qualified nesting namespaces.
#
# For example, if full_name is +A::B::C+, this method returns <code>["A", "A::B", "A::B::C"]</code>

def fully_qualified_nesting_namespaces
return nesting_namespaces if nesting_namespaces.length < 2
@fqns ||= nesting_namespaces.inject([]) do |list, n|
list << (list.empty? ? n : "#{list.last}::#{n}")
end
end

##
# TODO: filter included items by #display?

Expand Down
21 changes: 21 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ def generate_class klass, template_file = nil
asset_rel_prefix = rel_prefix + @asset_rel_path
svninfo = get_svninfo(current)

breadcrumb = generate_nesting_namespaces_breadcrumb(current, rel_prefix)

@title = "#{klass.type} #{klass.full_name} - #{@options.title}"

debug_msg " rendering #{out_file}"
Expand Down Expand Up @@ -821,4 +823,23 @@ def generate_ancestor_list(ancestors, klass)

content << '</li></ul>'
end

private

def nesting_namespaces_to_class_modules klass
tree = {}

klass.nesting_namespaces.zip(klass.fully_qualified_nesting_namespaces) do |ns, fqns|
tree[ns] = @store.classes_hash[fqns] || @store.modules_hash[fqns]
end

tree
end

def generate_nesting_namespaces_breadcrumb klass, rel_prefix
nesting_namespaces_to_class_modules(klass).map do |namespace, class_module|
path = class_module ? (rel_prefix + class_module.path).to_s : ""
{ name: namespace, path: path, self: klass.full_name == class_module&.full_name }
end
end
end
15 changes: 15 additions & 0 deletions lib/rdoc/generator/template/darkfish/class.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
</nav>

<main role="main" aria-labelledby="<%=h klass.aref %>">
<%# If nesting level is 1, breadcrumb list is not needed %>
<% if breadcrumb.size > 1 %>
<ol role="navigation" aria-label="breadcrumb" class="breadcrumb">
<% breadcrumb.each do |namespace| %>
<li>
<% if namespace[:self] %>
<span><%= namespace[:name] %></span>
<% else %>
<a href="<%= namespace[:path] %>"><%= namespace[:name] %></a><span>::</span>
<% end %>
</li>
<% end %>
</ol>
<% end %>

<h1 id="<%=h klass.aref %>" class="anchor-link <%= klass.type %>">
<%= klass.type %> <%= klass.full_name %>
</h1>
Expand Down
13 changes: 13 additions & 0 deletions lib/rdoc/generator/template/darkfish/css/rdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ nav h3,
font-size: 1em;
}

ol.breadcrumb {
display: flex;

padding: 0;
margin: 0 0 1em;
}

ol.breadcrumb li {
display: block;
list-style: none;
font-size: 125%;
}

nav ul,
nav dl,
nav p {
Expand Down
22 changes: 22 additions & 0 deletions test/rdoc/test_rdoc_class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,28 @@ def test_update_extends_with_colons
assert_equal [a, c], @c1.extends
end

def test_nesting_namespaces
cm1 = RDoc::ClassModule.new "A"
assert_equal ["A"], cm1.nesting_namespaces

cm2 = RDoc::ClassModule.new "A::B"
assert_equal ["A", "B"], cm2.nesting_namespaces

cm3 = RDoc::ClassModule.new "::A::B::C"
assert_equal ["A", "B", "C"], cm3.nesting_namespaces
end

def test_fully_qualified_nesting_namespaces
cm1 = RDoc::ClassModule.new "A"
assert_equal ["A"], cm1.fully_qualified_nesting_namespaces

cm2 = RDoc::ClassModule.new "A::B"
assert_equal ["A", "A::B"], cm2.fully_qualified_nesting_namespaces

cm3 = RDoc::ClassModule.new "::A::B::C"
assert_equal ["A", "A::B", "A::B::C"], cm3.fully_qualified_nesting_namespaces
end

class TestRDocClassModuleMixins < XrefTestCase
def setup
super
Expand Down

0 comments on commit a11e6b9

Please sign in to comment.