Skip to content
Merged
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
17 changes: 17 additions & 0 deletions lib/rdoc/generator/aliki.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'uri'

##
# Aliki theme for RDoc documentation
#
Expand Down Expand Up @@ -115,6 +117,21 @@ def write_search_index
File.write search_index_path, "var search_data = #{JSON.generate(data)};"
end

##
# Resolves a URL for use in templates. Absolute URLs are returned unchanged.
# Relative URLs are prefixed with rel_prefix to ensure they resolve correctly from any page.

def resolve_url(rel_prefix, url)
uri = URI.parse(url)
if uri.absolute?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a URI ever not be absolute? I thought parsing a URI for a relative path always failed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you can create URI with a wide range of strings:

irb(main):005> URI.parse("/foo")
=> #<URI::Generic /foo>
irb(main):006> URI("/foo")
=> #<URI::Generic /foo>
irb(main):007> URI("foo")
=> #<URI::Generic foo>
irb(main):008> URI("foo").absolute?
=> false
irb(main):009> URI("/foo").absolute?
=> false
irb(main):010> URI("foobar?baz>").absolute?
=> false

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I think the limitation is with file URIs.

We use URIs for files so often for tooling stuff I forget there are other URIs 😅

url
else
"#{rel_prefix}/#{url}"
end
rescue URI::InvalidURIError
"#{rel_prefix}/#{url}"
end

private

def build_class_module_entry(klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/generator/template/aliki/_footer.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<h3><%= h column_title %></h3>
<ul>
<% links.each do |text, url| %>
<li><a href="<%= h url %>"><%= h text %></a></li>
<li><a href="<%= h resolve_url(rel_prefix, url) %>"><%= h text %></a></li>
<% end %>
</ul>
</div>
Expand Down
Loading