Skip to content

Commit 4b9daa0

Browse files
authored
Allow using backticks to quote text in RDoc markup too (#1551)
Users can now use both ``` ` ``` and `+` to make simple code blocks in RDoc markup. Using backticks to quote code has become a natural habit for many users, so it's common to see it being incorrectly used to contribute to Ruby documentation. But instead of "correcting" users' behaviour, we should just support it. It'll also allow us to partially migrate some RDoc markup to Markdown.
1 parent 9c69c42 commit 4b9daa0

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

doc/markup_reference/rdoc.rdoc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,17 +756,32 @@ Rendered HTML:
756756
<tt>Monofont passage containing _italics_ and *bold*.</tt>
757757

758758
A single word may be made monofont by a shorthand:
759-
prefixed and suffixed plus-signs.
759+
prefixed and suffixed plus-signs or backticks.
760760

761761
Example input:
762762

763763
+Monofont+ in a paragraph.
764764

765+
`Monofont` in a paragraph.
766+
765767
Rendered HTML:
766768

767769
>>>
768770
+Monofont+ in a paragraph.
769771

772+
`Monofont` in a paragraph.
773+
774+
Note: The shorthand (<tt>+...+</tt> and <tt>`...`</tt>) only works for simple content
775+
containing word characters, colons, periods, slashes, brackets, and hyphens.
776+
Content containing HTML tags or other markup delimiters will not be matched.
777+
For complex content, use the HTML tags <tt><tt></tt> or <tt><code></tt> instead:
778+
779+
<tt>+literal_plus+</tt> shows: +literal_plus+
780+
781+
<tt>`literal_backtick`</tt> shows: `literal_backtick`
782+
783+
<tt><code>content</code></tt> shows: <code>content</code>
784+
770785
==== Strikethrough
771786

772787
Text may be marked as strikethrough via HTML tag <tt><del></tt> or <tt><s></tt>.

lib/rdoc/markup/attribute_manager.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def initialize
8989
add_word_pair "*", "*", :BOLD, true
9090
add_word_pair "_", "_", :EM, true
9191
add_word_pair "+", "+", :TT, true
92+
add_word_pair "`", "`", :TT, true
9293

9394
add_html "em", :EM, true
9495
add_html "i", :EM, true
@@ -100,7 +101,7 @@ def initialize
100101

101102
@word_pair_chars = @matching_word_pairs.keys.join
102103

103-
# Matches a word pair delimiter (*, _, +) that is NOT already protected.
104+
# Matches a word pair delimiter (*, _, +, `) that is NOT already protected.
104105
# Used by #protect_code_markup to escape delimiters inside <code>/<tt> tags.
105106
@unprotected_word_pair_regexp = /([#{@word_pair_chars}])(?!#{PROTECT_ATTR})/
106107
end

test/rdoc/markup/attribute_manager_test.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,31 @@ def test_convert_attrs_ignores_tt_inside_tt
244244
assert_equal 'foo <CODE>+tt+</CODE> bar', output('foo <tt>+tt+</tt> bar')
245245
end
246246

247+
def test_backtick_basic
248+
assert_equal(["cat ", @tt_on, "and", @tt_off, " dog"],
249+
@am.flow("cat `and` dog"))
250+
251+
assert_equal(["cat ", @tt_on, "X::Y", @tt_off, " dog"],
252+
@am.flow("cat `X::Y` dog"))
253+
end
254+
255+
def test_backtick_output
256+
assert_equal 'cat <CODE>and</CODE> dog', output('cat `and` dog')
257+
assert_equal 'cat <CODE>X::Y</CODE> dog', output('cat `X::Y` dog')
258+
end
259+
260+
def test_convert_attrs_ignores_backtick_inside_code
261+
assert_equal 'foo <CODE>`text`</CODE> bar', output('foo <code>`text`</code> bar')
262+
end
263+
264+
def test_convert_attrs_ignores_backtick_inside_tt
265+
assert_equal 'foo <CODE>`text`</CODE> bar', output('foo <tt>`text`</tt> bar')
266+
end
267+
268+
def test_backtick_escaped
269+
assert_equal ['`text`'], @am.flow('\`text`')
270+
end
271+
247272
def test_convert_attrs_ignores_del_inside_code
248273
assert_equal 'foo <CODE><del>strike</del></CODE> bar', output('foo <code><del>strike</del></code> bar')
249274
end
@@ -367,7 +392,7 @@ def test_initial_html
367392
def test_initial_word_pairs
368393
word_pairs = @am.matching_word_pairs
369394
assert word_pairs.is_a?(Hash)
370-
assert_equal(3, word_pairs.size)
395+
assert_equal(4, word_pairs.size)
371396
end
372397

373398
def test_mask_protected_sequence

0 commit comments

Comments
 (0)