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

Replace source code toggler JavaScript with <details> #252

Merged
merged 1 commit into from
Jul 31, 2023
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
41 changes: 17 additions & 24 deletions lib/rdoc/generator/template/rails/_context.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -159,32 +159,25 @@
<% end %>

<% source_code, source_url = method_source_code_and_url(method) %>
<% if source_code || source_url %>
<div class="sourcecode">
<p class="source-link">
Source:

<% if source_code %>
<a href="javascript:toggleSource('<%= method.aref %>_source')" id="l_<%= method.aref %>_source">show</a>
<% end %>

<% if source_code && source_url %> | <% end %>

<% if source_code %>
<details class="sourcecode">
<summary>
<span class="label">Source code</span>
<% if source_url %>
<a href="<%= source_url %>" target="_blank" class="github_url">on GitHub</a>
<%= link_to_external "GitHub", source_url %>
<% end %>
</p>

<% if source_code %>
<div id="<%= method.aref %>_source" class="dyn-source">
<pre><code class="ruby"><%= source_code %></code></pre>
</div>
<% end %>
</div>
<% end %>
</div>
<% end %><%# methods.each %>
<% end %><%# visibilities.each %>
</summary>

<div class="dyn-source">
<pre><code class="ruby"><%= source_code %></code></pre>
</div>
</details>
<% elsif source_url %>
<p><%= link_to_external "GitHub", source_url %></p>
<% end %>
</div>
<% end %><%# methods.each %>
<% end %><%# visibilities.each %>
<% end %><%# context.methods_by_type %>
<% end %><%# context.each_section %>

Expand Down
53 changes: 42 additions & 11 deletions lib/rdoc/generator/template/rails/resources/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ body {
display: grid;
grid-template-rows: min-content min-content auto min-content;
grid-template-columns: 300px auto;

--link-color: #CC0000;
--link-hover-color: #990000;
--icon-color: #777777;
}

body {
Expand Down Expand Up @@ -61,12 +65,12 @@ body {
}

a:link, a:active, a:visited, a:hover {
color: #CC0000;
color: var(--link-color);
text-decoration: none;
}

a:hover {
color: #990000;
color: var(--link-hover-color);
text-decoration: underline;
}

Expand All @@ -78,6 +82,13 @@ h1 a, h2 a, .banner a {
color: #fff;
}

.external-link {
padding-left: 1.3em;
background-image: url('../i/external-link.svg');
background-repeat: no-repeat;
background-size: 1.1em;
}

p {
margin-bottom: 1em;
}
Expand Down Expand Up @@ -290,7 +301,6 @@ tt {
}

.dyn-source {
display: none;
background: #fffde8;
color: #000;
border: #ffe0bb dotted 1px;
Expand Down Expand Up @@ -343,19 +353,40 @@ tt {
top: 0;
}

.method .sourcecode p.source-link {
text-indent: 0em;
margin-top: 0.5em;
}

.method .aka {
margin-left: 1.2em;
font-style: italic;
}

.method .source-link
{
font-size: 0.85em;
.sourcecode {
width: fit-content; /* Reduce clickable area when collapsed */
}

.sourcecode[open] {
width: auto;
}

.sourcecode summary {
cursor: pointer;
}

.sourcecode summary::marker {
font-size: 1.25em;
color: var(--icon-color);
}

.sourcecode summary .label {
margin-left: -0.4em;
font-weight: bold;
color: var(--link-color);
}

.sourcecode summary .label:hover {
color: var(--link-hover-color);
}

.sourcecode summary .external-link {
margin-left: 1em;
}

@keyframes spotlight {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions lib/rdoc/generator/template/rails/resources/js/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
function toggleSource(id) {
var src = $('#' + id).toggle();
var isVisible = src.is(':visible');
$('#l_' + id).html(isVisible ? 'hide' : 'show');
}

window.spotlight = function(url) {
var hash = url.match(/#([^#]+)$/);
if (hash) {
Expand Down
8 changes: 8 additions & 0 deletions lib/sdoc/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def link_to(text, url, html_attributes = {})
%(<a href="#{h url}"#{attribute_string}>#{h text}</a>)
end

def link_to_external(text, url, html_attributes = {})
html_attributes = html_attributes.transform_keys(&:to_s)
html_attributes = { "target" => "_blank", "class" => nil }.merge(html_attributes)
html_attributes["class"] = [*html_attributes["class"], "external-link"].join(" ")

link_to(text, url, html_attributes)
end

def base_tag_for_context(context)
if context == :index
%(<base href="./" data-current-path=".">)
Expand Down
22 changes: 22 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ module Foo; class Bar; def qux; end; end; end
end
end

describe "#link_to_external" do
it "sets class='external-link' and target='_blank' by default" do
_(@helpers.link_to_external("foo", "bar")).
must_equal %(<a href="bar" target="_blank" class="external-link">foo</a>)
end

it "supports additional classes" do
_(@helpers.link_to_external("foo", "bar", class: "qux")).
must_equal %(<a href="bar" target="_blank" class="qux external-link">foo</a>)
end

it "supports overriding target" do
_(@helpers.link_to_external("foo", "bar", target: "_self")).
must_equal %(<a href="bar" target="_self" class="external-link">foo</a>)
end

it "supports additional attributes" do
_(@helpers.link_to_external("foo", "bar", "data-hoge": "fuga")).
must_equal %(<a href="bar" target="_blank" class="external-link" data-hoge="fuga">foo</a>)
end
end

describe "#base_tag_for_context" do
it "returns an idempotent <base> tag for the :index context" do
_(@helpers.base_tag_for_context(:index)).
Expand Down