Skip to content

Commit c4a58cb

Browse files
authored
Fix Markdown strikethrough (~~text~~) not rendering in HTML output (#1541)
The Markdown parser correctly parsed `~~text~~` syntax but the `AttributeManager` did not recognize the resulting formats for HTML conversion: - Single words: `~~word~~` -> `~word~` (not registered as word pair) - Multi-word: `~~words here~~` -> `<s>words here</s>` (tag not registered) Add support for strikethrough formatting: - Register `~` as word pair for `:STRIKE` attribute - Register `<s>`, `<del>`, and `<strike>` HTML tags for `:STRIKE` - Add `:STRIKE` -> `<del>` tag mapping in `ToHtml` All strikethrough input formats now render as `<del>text</del>` in HTML.
1 parent b810f16 commit c4a58cb

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

lib/rdoc/markup/attribute_manager.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ def initialize
8989
add_word_pair "*", "*", :BOLD, true
9090
add_word_pair "_", "_", :EM, true
9191
add_word_pair "+", "+", :TT, true
92+
add_word_pair "~", "~", :STRIKE, true
9293

9394
add_html "em", :EM, true
9495
add_html "i", :EM, true
9596
add_html "b", :BOLD, true
9697
add_html "tt", :TT, true
9798
add_html "code", :TT, true
99+
add_html "s", :STRIKE, true
100+
add_html "del", :STRIKE, true
98101

99102
@word_pair_chars = @matching_word_pairs.keys.join
100103

lib/rdoc/markup/to_html.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,10 @@ def html_list_name(list_type, open_tag)
419419
# Maps attributes to HTML tags
420420

421421
def init_tags
422-
add_tag :BOLD, "<strong>", "</strong>"
423-
add_tag :TT, "<code>", "</code>"
424-
add_tag :EM, "<em>", "</em>"
422+
add_tag :BOLD, "<strong>", "</strong>"
423+
add_tag :TT, "<code>", "</code>"
424+
add_tag :EM, "<em>", "</em>"
425+
add_tag :STRIKE, "<del>", "</del>"
425426
end
426427

427428
##

test/rdoc/markup/attribute_manager_test.rb

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ def setup
1717
@em_on = @am.changed_attribute_by_name([], [:EM])
1818
@em_off = @am.changed_attribute_by_name([:EM], [])
1919

20+
@strike_on = @am.changed_attribute_by_name([], [:STRIKE])
21+
@strike_off = @am.changed_attribute_by_name([:STRIKE], [])
22+
2023
@bold_em_on = @am.changed_attribute_by_name([], [:BOLD] | [:EM])
2124
@bold_em_off = @am.changed_attribute_by_name([:BOLD] | [:EM], [])
2225

@@ -54,7 +57,7 @@ def test_adding
5457
def test_add_html_tag
5558
@am.add_html("Test", :TEST)
5659
tags = @am.html_tags
57-
assert_equal(6, tags.size)
60+
assert_equal(8, tags.size)
5861
assert(tags.has_key?("test"))
5962
end
6063

@@ -164,6 +167,40 @@ def test_bold_html_escaped
164167
assert_equal ['cat <b>dog</b>'], @am.flow('cat \<b>dog</b>')
165168
end
166169

170+
def test_strike
171+
assert_equal [@strike_on, 'strike', @strike_off],
172+
@am.flow("~strike~")
173+
174+
assert_equal [@strike_on, 'Strike:', @strike_off],
175+
@am.flow("~Strike:~")
176+
177+
assert_equal ["cat ", @strike_on, "and", @strike_off, " dog"],
178+
@am.flow("cat ~and~ dog")
179+
end
180+
181+
def test_strike_html_escaped
182+
assert_equal ['cat <s>dog</s>'], @am.flow('cat \<s>dog</s>')
183+
assert_equal ['cat <del>dog</del>'], @am.flow('cat \<del>dog</del>')
184+
end
185+
186+
def test_html_like_strike
187+
assert_equal ["cat ", @strike_on, "dog", @strike_off],
188+
@am.flow("cat <s>dog</s>")
189+
end
190+
191+
def test_html_like_strike_del
192+
assert_equal ["cat ", @strike_on, "dog", @strike_off],
193+
@am.flow("cat <del>dog</del>")
194+
end
195+
196+
def test_convert_attrs_ignores_strike_inside_code
197+
assert_equal 'foo <CODE>~strike~</CODE> bar', output('foo <code>~strike~</code> bar')
198+
end
199+
200+
def test_convert_attrs_ignores_strike_inside_tt
201+
assert_equal 'foo <CODE>~strike~</CODE> bar', output('foo <tt>~strike~</tt> bar')
202+
end
203+
167204
def test_combined
168205
assert_equal(["cat ", @em_on, "and", @em_off, " ", @bold_on, "dog", @bold_off],
169206
@am.flow("cat _and_ *dog*"))
@@ -331,13 +368,13 @@ def test_html_like_teletype_em_bold_SGML
331368
def test_initial_html
332369
html_tags = @am.html_tags
333370
assert html_tags.is_a?(Hash)
334-
assert_equal(5, html_tags.size)
371+
assert_equal(7, html_tags.size)
335372
end
336373

337374
def test_initial_word_pairs
338375
word_pairs = @am.matching_word_pairs
339376
assert word_pairs.is_a?(Hash)
340-
assert_equal(3, word_pairs.size)
377+
assert_equal(4, word_pairs.size)
341378
end
342379

343380
def test_mask_protected_sequence

test/rdoc/rdoc_markdown_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,34 @@ def test_parse_strike_tilde_no
11141114
assert_equal expected, doc
11151115
end
11161116

1117+
def test_strike_to_html_single_word
1118+
doc = parse "This is ~~strikethrough~~ text.\n"
1119+
html = @to_html.convert doc
1120+
1121+
assert_match %r{<del>strikethrough</del>}, html
1122+
end
1123+
1124+
def test_strike_to_html_multiple_words
1125+
doc = parse "This is ~~multiple words~~ text.\n"
1126+
html = @to_html.convert doc
1127+
1128+
assert_match %r{<del>multiple words</del>}, html
1129+
end
1130+
1131+
def test_strike_to_html_with_s_tag
1132+
doc = parse "This has <s>deleted</s> text.\n"
1133+
html = @to_html.convert doc
1134+
1135+
assert_match %r{<del>deleted</del>}, html
1136+
end
1137+
1138+
def test_strike_to_html_with_del_tag
1139+
doc = parse "This has <del>deleted</del> text.\n"
1140+
html = @to_html.convert doc
1141+
1142+
assert_match %r{<del>deleted</del>}, html
1143+
end
1144+
11171145
def test_parse_style
11181146
@parser.css = true
11191147

0 commit comments

Comments
 (0)