Skip to content
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

Add breadcrumb list #973

Open
wants to merge 6 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/rdoc/class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,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
20 changes: 20 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ def generate_class klass, template_file = nil
asset_rel_prefix = rel_prefix + @asset_rel_path
svninfo = get_svninfo(current)

breadcrumb = generate_namespaces_breadcrumb(current, rel_prefix)

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

debug_msg " rendering #{out_file}"
Expand Down Expand Up @@ -783,4 +785,22 @@ def template_for file, page = true, klass = ERB
template
end

private

def namespaces_to_class_modules klass
tree = {}

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

tree
end

def generate_namespaces_breadcrumb klass, rel_prefix
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
14 changes: 14 additions & 0 deletions lib/rdoc/generator/template/darkfish/class.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
</div>
</nav>

<nav role="navigation" aria-label="breadcrumb" class="breadcrumb">
<ol>
<% 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>
</nav>

<main role="main" aria-labelledby="<%=h klass.aref %>">
<h1 id="<%=h klass.aref %>" class="<%= klass.type %>">
<%= klass.type %> <%= klass.full_name %>
Expand Down
22 changes: 22 additions & 0 deletions lib/rdoc/generator/template/darkfish/css/rdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,28 @@ nav h3,
color: #555;
}

nav.breadcrumb {
display: block;
float: unset;
width: unset;
height: unset;
border: none;
position: unset;
top: unset;
padding-top: 10px;
padding-left: 20px;
margin-bottom: 1em;
}

nav.breadcrumb ol {
display: inline-block;
}

nav.breadcrumb li {
display: inline-block;
font-size: 125%;
}

nav ul,
nav dl,
nav p {
Expand Down
21 changes: 21 additions & 0 deletions test/rdoc/test_rdoc_class_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1500,4 +1500,25 @@ 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
end