Skip to content

Commit

Permalink
Add "Example" label to example code blocks
Browse files Browse the repository at this point in the history
The label should help users distinguish example code blocks from other
code blocks, such as method source code.

It's only applied to Ruby code examples.
  • Loading branch information
st0012 committed Sep 12, 2024
1 parent 403c0de commit 8cf49cb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
18 changes: 18 additions & 0 deletions lib/rdoc/generator/template/darkfish/css/rdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,24 @@ main .method-source-code.active-menu {
max-height: 100vh;
}

main .method-example::before {
content: "Example";
color: var(--secondary-color);
border-bottom: 1px solid var(--border-color);
display: block;
padding: 10px 0;
font-size: 0.9em;
margin-bottom: 10px;
}

main .method-example {
margin: 0;
padding: 0px 16px 16px 16px;
overflow-x: auto;
border-radius: 4px;
border-color: var(--border-color);
}

main .method-description .method-calls-super {
color: var(--text-color);
font-weight: bold;
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/markup/to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def accept_verbatim verbatim
content = if verbatim.ruby? or parseable? text then
begin
tokens = RDoc::Parser::RipperStateLex.parse text
klass = ' class="ruby"'
klass = ' class="ruby method-example"'

result = RDoc::TokenStream.to_html tokens
result = result + "\n" unless "\n" == result[-1]
Expand Down
18 changes: 9 additions & 9 deletions test/rdoc/test_rdoc_markup_to_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def accept_rule
end

def accept_verbatim
assert_equal "\n<pre class=\"ruby\"><span class=\"ruby-identifier\">hi</span>\n <span class=\"ruby-identifier\">world</span>\n</pre>\n", @to.res.join
assert_equal "\n<pre class=\"ruby method-example\"><span class=\"ruby-identifier\">hi</span>\n <span class=\"ruby-identifier\">world</span>\n</pre>\n", @to.res.join
end

def end_accepting
Expand Down Expand Up @@ -459,7 +459,7 @@ def test_accept_verbatim_parseable

expected = <<-EXPECTED
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<pre class="ruby method-example"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<span class="ruby-keyword">end</span>
</pre>
EXPECTED
Expand Down Expand Up @@ -491,7 +491,7 @@ def test_accept_verbatim_nl_after_backslash

expected = <<-EXPECTED
<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">first_flag_var</span> <span class="ruby-keyword">and</span> \\
<pre class="ruby method-example"><span class="ruby-identifier">a</span> = <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">first_flag_var</span> <span class="ruby-keyword">and</span> \\
<span class="ruby-identifier">this_is_flag_var</span>
</pre>
EXPECTED
Expand Down Expand Up @@ -549,7 +549,7 @@ def bar

expected = <<-'EXPECTED'
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
<pre class="ruby method-example"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
[
<span class="ruby-string">&#39;\\&#39;</span>,
<span class="ruby-string">&#39;\&#39;&#39;</span>,
Expand Down Expand Up @@ -599,7 +599,7 @@ def bar

expected = <<-'EXPECTED'
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
<pre class="ruby method-example"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">foo</span>
[
<span class="ruby-string">`\\`</span>,
<span class="ruby-string">`\&#39;\&quot;\``</span>,
Expand All @@ -626,7 +626,7 @@ def test_accept_verbatim_ruby

expected = <<-EXPECTED
<pre class="ruby"><span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
<pre class="ruby method-example"><span class="ruby-value">1</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
</pre>
EXPECTED

Expand All @@ -645,7 +645,7 @@ def test_accept_verbatim_redefinable_operators

expected = <<-EXPECTED
<pre class="ruby">
<pre class="ruby method-example">
EXPECTED
expected = expected.rstrip

Expand Down Expand Up @@ -880,8 +880,8 @@ def test_list_verbatim_2
<ul><li>
<p>one</p>
<pre class=\"ruby\"><span class=\"ruby-identifier\">verb1</span>
<span class=\"ruby-identifier\">verb2</span>
<pre class="ruby method-example"><span class="ruby-identifier">verb1</span>
<span class="ruby-identifier">verb2</span>
</pre>
</li><li>
<p>two</p>
Expand Down
12 changes: 6 additions & 6 deletions test/rdoc/test_rdoc_markup_to_html_snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def accept_rule
end

def accept_verbatim
assert_equal "\n<pre class=\"ruby\"><span class=\"ruby-identifier\">hi</span>\n <span class=\"ruby-identifier\">world</span>\n</pre>\n", @to.res.join
assert_equal "\n<pre class=\"ruby method-example\"><span class=\"ruby-identifier\">hi</span>\n <span class=\"ruby-identifier\">world</span>\n</pre>\n", @to.res.join
assert_equal 10, @to.characters
end

Expand Down Expand Up @@ -404,7 +404,7 @@ def test_accept_verbatim_ruby

expected = <<-EXPECTED
<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<pre class="ruby method-example"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<span class="ruby-keyword">end</span>
</pre>
EXPECTED
Expand Down Expand Up @@ -586,8 +586,8 @@ def test_convert_limit_verbatim_multiline
expected = <<-EXPECTED
<p>Look for directives in a normal comment block:
<pre class=\"ruby\"><span class=\"ruby-comment\"># :stopdoc:</span>
<span class=\"ruby-comment\">#{inner}</span>
<pre class="ruby method-example"><span class="ruby-comment"># :stopdoc:</span>
<span class="ruby-comment">#{inner}</span>
</pre>
EXPECTED

Expand Down Expand Up @@ -664,8 +664,8 @@ def test_list_verbatim_2
expected = <<-EXPECTED
<p>one
<pre class=\"ruby\"><span class=\"ruby-identifier\">verb1</span>
<span class=\"ruby-identifier\">verb2</span>
<pre class="ruby method-example"><span class="ruby-identifier">verb1</span>
<span class="ruby-identifier">verb2</span>
</pre>
<p>two
Expand Down
2 changes: 1 addition & 1 deletion test/rdoc/test_rdoc_parser_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2984,7 +2984,7 @@ def blah
expected = <<EXPECTED
<p>doc
<pre class="ruby"><span class="ruby-comment">=begin
<pre class="ruby method-example"><span class="ruby-comment">=begin
test embdoc
=end</span>
</pre>
Expand Down

0 comments on commit 8cf49cb

Please sign in to comment.