From c65266437cb2b2960fe3d8af8c01156e2f88d905 Mon Sep 17 00:00:00 2001 From: Hartley McGuire Date: Wed, 6 Mar 2024 19:09:48 -0500 Subject: [PATCH] Fix ToMarkdown missing newlines for label-lists Previously, using ToMarkdown on a label-list would generate output that could not be reparsed by the RDoc::Markdown parser: ``` md = <<~MD apple : a red fruit banana : a yellow fruit MD doc = RDoc::Markdown.parse(md) doc # => [doc: [list: NOTE [item: ["apple"]; [para: "a red fruit"]], [item: ["banana"]; [para: "a yellow fruit"]]]] new_md = doc.accept(RDoc::Markup::ToMarkdown.new) new_md # => "apple\n: a red fruit\nbanana\n: a yellow fruit\n\n" new_doc = RDoc::Markdown.parse(new_md) new_doc # => [doc: [list: NOTE [item: ["apple"]; [para: "a red fruit\nbanana\n: a yellow fruit"]]]] ``` The issue is that the [PHP Markdown Extra spec][1] requires a newline after each definition list item, but ToMarkdown was not putting newlines between label-list items. This commit fixes the issue by properly appending a newline after each label-list item so that the output of ToMarkdown can be reparsed by RDoc::Markdown: ``` md = <<~MD apple : a red fruit banana : a yellow fruit MD doc = RDoc::Markdown.parse(mdoc) doc # => [doc: [list: NOTE [item: ["apple"]; [para: "a red fruit"]], [item: ["banana"]; [para: "a yellow fruit"]]]] new_md = doc.accept(RDoc::Markup::ToMarkdown.new) new_md # => "apple\n: a red fruit\n\nbanana\n: a yellow fruit\n\n" new_doc = RDoc::Markdown.parse(new_md) new_doc # => [doc: [list: NOTE [item: ["apple"]; [para: "a red fruit"]], [item: ["banana"]; [para: "a yellow fruit"]]]] ``` [1]: https://michelf.ca/projects/php-markdown/extra/#def-list --- lib/rdoc/markup/to_markdown.rb | 8 ++++---- test/rdoc/test_rdoc_markup_to_markdown.rb | 7 ++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/rdoc/markup/to_markdown.rb b/lib/rdoc/markup/to_markdown.rb index 5dd60e18f5..b915fab60b 100644 --- a/lib/rdoc/markup/to_markdown.rb +++ b/lib/rdoc/markup/to_markdown.rb @@ -45,8 +45,6 @@ def handle_regexp_HARD_BREAK target # Finishes consumption of `list` def accept_list_end list - @res << "\n" - super end @@ -60,6 +58,8 @@ def accept_list_item_end list_item when :NOTE, :LABEL then use_prefix + @res << "\n" + 4 else @list_index[-1] = @list_index.last.succ @@ -81,11 +81,11 @@ def accept_list_item_start list_item attributes(label).strip end.join "\n" - bullets << "\n:" + bullets << "\n" unless bullets.empty? @prefix = ' ' * @indent @indent += 4 - @prefix << bullets + (' ' * (@indent - 1)) + @prefix << bullets << ":" << (' ' * (@indent - 1)) else bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.' @prefix = (' ' * @indent) + bullet.ljust(4) diff --git a/test/rdoc/test_rdoc_markup_to_markdown.rb b/test/rdoc/test_rdoc_markup_to_markdown.rb index ff47364faa..92ed37bc50 100644 --- a/test/rdoc/test_rdoc_markup_to_markdown.rb +++ b/test/rdoc/test_rdoc_markup_to_markdown.rb @@ -69,7 +69,7 @@ def accept_list_item_end_bullet end def accept_list_item_end_label - assert_equal "cat\n: ", @to.res.join + assert_equal "cat\n: \n", @to.res.join assert_equal 0, @to.indent, 'indent' end @@ -79,7 +79,7 @@ def accept_list_item_end_lalpha end def accept_list_item_end_note - assert_equal "cat\n: ", @to.res.join + assert_equal "cat\n: \n", @to.res.join assert_equal 0, @to.indent, 'indent' end @@ -319,9 +319,7 @@ def list_nested expected = <<-EXPECTED * l1 * l1.1 - * l2 - EXPECTED assert_equal expected, @to.end_accepting @@ -343,7 +341,6 @@ def list_verbatim * second - EXPECTED assert_equal expected, @to.end_accepting