Skip to content

Commit

Permalink
Redesign "Constants" section
Browse files Browse the repository at this point in the history
This changes the appearance of the "Constants" section to match the
"Methods" sections.
  • Loading branch information
jonathanhefner committed Jan 4, 2024
1 parent 7e67807 commit addb4cd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Main (3.0.0.alpha)
* [#322](https://github.com/rails/sdoc/pull/322) Use `type="search"` for search input [@jonathanhefner](https://github.com/jonathanhefner)
* [#323](https://github.com/rails/sdoc/pull/323) Update favicon [@jonathanhefner](https://github.com/jonathanhefner)
* [#345](https://github.com/rails/sdoc/pull/345) Version explicit links to `api.rubyonrails.org` [@jonathanhefner](https://github.com/jonathanhefner)
* [#356](https://github.com/rails/sdoc/pull/356) Redesign "Constants" section [@jonathanhefner](https://github.com/jonathanhefner)

2.6.1
=====
Expand Down
31 changes: 13 additions & 18 deletions lib/rdoc/generator/template/rails/_context.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,22 @@

<%= description_for section %>

<!-- Constants -->
<% unless constants.empty? %>
<!-- Section constants -->
<h2 class="content__divider">Constants</h2>
<table border='0' cellpadding='5'>
<% constants.each do |const| %>
<tr valign='top'>
<td class="attr-name"><%= short_name_for const %></td>
<td>=</td>
<td class="attr-value"><%= h const.value %></td>
</tr>
<% if const.comment && !const.comment.empty? %>
<tr valign='top'>
<td>&nbsp;</td>
<td colspan="2" class="attr-desc"><%= const.description.strip %></td>
</tr>
<% end %>
<% end %>
</table>
<% constants.each do |constant| %>
<div class="constant">
<div class="constant__name">
<h3><%= short_name_for constant %></h3>
</div>
<%= description_for constant %>
<div class="constant__source">
<pre class="source-code"><code class="ruby"><%= h constant.value %></code></pre>
</div>
</div>
<% end %>
<% end %>


<% unless attributes.empty? %>
<!-- Section attributes -->
<h2 class="content__divider">Attributes</h2>
Expand Down Expand Up @@ -135,7 +130,7 @@
<% end %>
</summary>

<pre><code class="ruby"><%= source_code %></code></pre>
<pre class="source-code"><code class="ruby"><%= source_code %></code></pre>
</details>
<% elsif source_url %>
<p class="method__source"><%= link_to_external "GitHub", source_url %></p>
Expand Down
45 changes: 34 additions & 11 deletions lib/rdoc/generator/template/rails/resources/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,25 @@ th
font-style: italic;
}

.description pre, .method__source pre {
.description pre, pre.source-code {
padding: 0.5ch 1ch;
border-radius: 6px;
overflow-x: auto;

background: var(--code-bg);
}


:is(.description p, p.description) code {
background: var(--code-bg);
padding: 0 0.15em;
border-radius: 3px;
}

.description pre, :is(.description p, p.description) code {
background: var(--code-bg);
}

pre.source-code {
background: var(--source-code-bg);
}

@media (hover: hover) {
.description a:hover code {
box-shadow: 0 0 0 1px;
Expand Down Expand Up @@ -737,11 +741,35 @@ html {
border-bottom: 1px solid;
}

.content__section-title + .content__divider {
.content__section-title + .content__divider,
.content__divider + :is(*, div /* increase selector specificity */) {
margin-top: var(--space);
}


/*
* Constant
*/

.constant {
margin-top: var(--space-lg);
}

.constant__name {
padding-bottom: var(--space-xs);
border-bottom: 2px solid var(--code-bg);
}

.constant__name code {
font-size: 1rem;
font-weight: bold;
}

.constant__name + *, .constant__source {
margin-top: var(--space-sm);
}


/*
* Method
*/
Expand All @@ -750,10 +778,6 @@ html {
margin-top: var(--space-xl);
}

.content__divider + .method {
margin-top: var(--space);
}


.method__signature {
padding-bottom: var(--space-xs);
Expand Down Expand Up @@ -847,7 +871,6 @@ html {

.method__source pre {
margin-top: var(--space-xs);
background: var(--source-code-bg);
}


Expand Down
2 changes: 1 addition & 1 deletion lib/sdoc/postprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def unify_h1_headings!(document)
end

def highlight_code_blocks!(document)
document.css(".description pre > code, .method__source pre > code").each do |element|
document.css(".description pre > code, pre.source-code > code").each do |element|
code = element.inner_text
language = element.classes.include?("ruby") ? "ruby" : guess_code_language(code)
element.inner_html = highlight_code(code, language)
Expand Down
22 changes: 6 additions & 16 deletions spec/postprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
_(SDoc::Postprocessor.process(rendered)).must_include rendered
end

it "highlights code blocks" do
it "highlights description code blocks" do
rendered = <<~HTML
<div class="description">
<p>Ruby:</p>
Expand All @@ -173,25 +173,15 @@
_(SDoc::Postprocessor.process(rendered)).must_include expected
end

it "highlights method source code" do
it "highlights source code blocks" do
rendered = <<~HTML
<div class="method__source">
<pre><code class="ruby"><span class="ruby-comment"># highlighted by RDoc</span></code></pre>
</div>
<div class="method__source">
<pre><code class="ruby">DELETE FROM 'tricky_ruby'</code></pre>
</div>
<pre class="source-code"><code class="ruby"><span class="ruby-comment"># highlighted by RDoc</span></code></pre>
<pre class="source-code"><code class="ruby">DELETE FROM 'tricky_ruby'</code></pre>
HTML

expected = <<~HTML
<div class="method__source">
<pre><code class="ruby highlight">#{SDoc::Postprocessor.highlight_code("# highlighted by RDoc", "ruby")}</code></pre>
</div>
<div class="method__source">
<pre><code class="ruby highlight">#{SDoc::Postprocessor.highlight_code("DELETE FROM 'tricky_ruby'", "ruby")}</code></pre>
</div>
<pre class="source-code"><code class="ruby highlight">#{SDoc::Postprocessor.highlight_code("# highlighted by RDoc", "ruby")}</code></pre>
<pre class="source-code"><code class="ruby highlight">#{SDoc::Postprocessor.highlight_code("DELETE FROM 'tricky_ruby'", "ruby")}</code></pre>
HTML

_(SDoc::Postprocessor.process(rendered)).must_include expected
Expand Down

0 comments on commit addb4cd

Please sign in to comment.