Skip to content

Commit a11e6b9

Browse files
authored
Merge pull request #973 from unasuke/breadcrumb
Add breadcrumb list
2 parents 03f3110 + c6b4fbb commit a11e6b9

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

lib/rdoc/code_object/class_module.rb

+19
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,25 @@ def full_name
295295
end
296296
end
297297

298+
##
299+
# Return array of full_name splitted by +::+.
300+
301+
def nesting_namespaces
302+
@namespaces ||= full_name.split("::").reject(&:empty?)
303+
end
304+
305+
##
306+
# Return array of fully qualified nesting namespaces.
307+
#
308+
# For example, if full_name is +A::B::C+, this method returns <code>["A", "A::B", "A::B::C"]</code>
309+
310+
def fully_qualified_nesting_namespaces
311+
return nesting_namespaces if nesting_namespaces.length < 2
312+
@fqns ||= nesting_namespaces.inject([]) do |list, n|
313+
list << (list.empty? ? n : "#{list.last}::#{n}")
314+
end
315+
end
316+
298317
##
299318
# TODO: filter included items by #display?
300319

lib/rdoc/generator/darkfish.rb

+21
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ def generate_class klass, template_file = nil
353353
asset_rel_prefix = rel_prefix + @asset_rel_path
354354
svninfo = get_svninfo(current)
355355

356+
breadcrumb = generate_nesting_namespaces_breadcrumb(current, rel_prefix)
357+
356358
@title = "#{klass.type} #{klass.full_name} - #{@options.title}"
357359

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

822824
content << '</li></ul>'
823825
end
826+
827+
private
828+
829+
def nesting_namespaces_to_class_modules klass
830+
tree = {}
831+
832+
klass.nesting_namespaces.zip(klass.fully_qualified_nesting_namespaces) do |ns, fqns|
833+
tree[ns] = @store.classes_hash[fqns] || @store.modules_hash[fqns]
834+
end
835+
836+
tree
837+
end
838+
839+
def generate_nesting_namespaces_breadcrumb klass, rel_prefix
840+
nesting_namespaces_to_class_modules(klass).map do |namespace, class_module|
841+
path = class_module ? (rel_prefix + class_module.path).to_s : ""
842+
{ name: namespace, path: path, self: klass.full_name == class_module&.full_name }
843+
end
844+
end
824845
end

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

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
</nav>
1919

2020
<main role="main" aria-labelledby="<%=h klass.aref %>">
21+
<%# If nesting level is 1, breadcrumb list is not needed %>
22+
<% if breadcrumb.size > 1 %>
23+
<ol role="navigation" aria-label="breadcrumb" class="breadcrumb">
24+
<% breadcrumb.each do |namespace| %>
25+
<li>
26+
<% if namespace[:self] %>
27+
<span><%= namespace[:name] %></span>
28+
<% else %>
29+
<a href="<%= namespace[:path] %>"><%= namespace[:name] %></a><span>::</span>
30+
<% end %>
31+
</li>
32+
<% end %>
33+
</ol>
34+
<% end %>
35+
2136
<h1 id="<%=h klass.aref %>" class="anchor-link <%= klass.type %>">
2237
<%= klass.type %> <%= klass.full_name %>
2338
</h1>

lib/rdoc/generator/template/darkfish/css/rdoc.css

+13
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ nav h3,
199199
font-size: 1em;
200200
}
201201

202+
ol.breadcrumb {
203+
display: flex;
204+
205+
padding: 0;
206+
margin: 0 0 1em;
207+
}
208+
209+
ol.breadcrumb li {
210+
display: block;
211+
list-style: none;
212+
font-size: 125%;
213+
}
214+
202215
nav ul,
203216
nav dl,
204217
nav p {

test/rdoc/test_rdoc_class_module.rb

+22
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,28 @@ def test_update_extends_with_colons
15261526
assert_equal [a, c], @c1.extends
15271527
end
15281528

1529+
def test_nesting_namespaces
1530+
cm1 = RDoc::ClassModule.new "A"
1531+
assert_equal ["A"], cm1.nesting_namespaces
1532+
1533+
cm2 = RDoc::ClassModule.new "A::B"
1534+
assert_equal ["A", "B"], cm2.nesting_namespaces
1535+
1536+
cm3 = RDoc::ClassModule.new "::A::B::C"
1537+
assert_equal ["A", "B", "C"], cm3.nesting_namespaces
1538+
end
1539+
1540+
def test_fully_qualified_nesting_namespaces
1541+
cm1 = RDoc::ClassModule.new "A"
1542+
assert_equal ["A"], cm1.fully_qualified_nesting_namespaces
1543+
1544+
cm2 = RDoc::ClassModule.new "A::B"
1545+
assert_equal ["A", "A::B"], cm2.fully_qualified_nesting_namespaces
1546+
1547+
cm3 = RDoc::ClassModule.new "::A::B::C"
1548+
assert_equal ["A", "A::B", "A::B::C"], cm3.fully_qualified_nesting_namespaces
1549+
end
1550+
15291551
class TestRDocClassModuleMixins < XrefTestCase
15301552
def setup
15311553
super

0 commit comments

Comments
 (0)