Skip to content

Commit

Permalink
Style unstyled code ref links
Browse files Browse the repository at this point in the history
RDoc does not provide syntax to style manual code ref links with
`<code></code>`.  Wrapping code ref links in `<tt></tt>` or `++` does
not give the desired result.  For example,

  ```ruby
  # {<tt>Rails</tt>}[rdoc-ref:Rails]
  ```

is rendered as

  ```html
  {<code>Rails</code><a href="..."><code>}</code></a>
  ```

and

  ```ruby
  # <tt>{Rails}[rdoc-ref:Rails]</tt>
  ```

is rendered as

  ```html
  <code>{Rails}[rdoc-ref:Rails]</code>
  ```

This commit uses postprocessing to style unstyled code ref links that
look like code.  If the link text does not contain a space or if it
matches `/\S\(/` (e.g. `foo(bar, &block)`), it is assumed to be code,
and the text is then wrapped with `<code></code>`.

This complements 4837fea in that it
allows manual code ref links to single-word modules to be styled as if
they were autolinks.
  • Loading branch information
jonathanhefner committed Jul 31, 2023
1 parent ef39d46 commit 845aa5d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/sdoc/postprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def process(rendered)
rebase_urls!(document)
version_rails_guides_urls!(document)
unlink_unintentional_ref_links!(document)
style_ref_links!(document)
highlight_code_blocks!(document)

document.to_s
Expand Down Expand Up @@ -78,6 +79,15 @@ def unlink_unintentional_ref_links!(document)
end
end

def style_ref_links!(document)
document.css(".description a[href^='classes/']:has(> text():only-child)").each do |element|
text = element.inner_text
if !text.include?(" ") || text.match?(/\S\(/)
element.inner_html = "<code>#{element.inner_html}</code>"
end
end
end

def highlight_code_blocks!(document)
document.css(".description pre > code, .sourcecode pre > code").each do |element|
code = element.inner_text
Expand Down
36 changes: 36 additions & 0 deletions spec/postprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@
_(SDoc::Postprocessor.process(rendered)).must_include expected
end

it "styles unstyled code ref links in descriptions" do
rendered = <<~HTML
<base href="../" data-current-path="classes/Foo.html">
<div class="description">
<a href="/classes/Bar/Qux.html">Qux</a>
<a href="Bar/Qux.html">Qux</a>
<a href="#method-i-bar-3F.html">Foo#bar?(qux, &amp;block)</a>
<a href="#method-i-2A_bar-21.html">*_bar!</a>
<a href="https://example.com/Qux.html">Qux</a>
<a href="Bar/Qux.html">Not Code</a>
<a href="Bar/Qux.html">(also) not code</a>
</div>
<a href="/classes/Permalink.html">Permalink</a>
HTML

expected = <<~HTML
<div class="description">
<a href="classes/Bar/Qux.html"><code>Qux</code></a>
<a href="classes/Bar/Qux.html"><code>Qux</code></a>
<a href="classes/Foo.html#method-i-bar-3F.html"><code>Foo#bar?(qux, &amp;block)</code></a>
<a href="classes/Foo.html#method-i-2A_bar-21.html"><code>*_bar!</code></a>
<a href="https://example.com/Qux.html">Qux</a>
<a href="classes/Bar/Qux.html">Not Code</a>
<a href="classes/Bar/Qux.html">(also) not code</a>
</div>
<a href="classes/Permalink.html">Permalink</a>
HTML

_(SDoc::Postprocessor.process(rendered)).must_include expected
end

it "highlights code blocks" do
rendered = <<~HTML
<div class="description">
Expand Down

0 comments on commit 845aa5d

Please sign in to comment.