Skip to content

Commit 4756eaf

Browse files
skipkayhilmatzbot
authored andcommitted
[ruby/rdoc] 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 ruby/rdoc@c65266437c
1 parent e8f796e commit 4756eaf

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

lib/rdoc/markup/to_markdown.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ def handle_regexp_HARD_BREAK target
4545
# Finishes consumption of `list`
4646

4747
def accept_list_end list
48-
@res << "\n"
49-
5048
super
5149
end
5250

@@ -60,6 +58,8 @@ def accept_list_item_end list_item
6058
when :NOTE, :LABEL then
6159
use_prefix
6260

61+
@res << "\n"
62+
6363
4
6464
else
6565
@list_index[-1] = @list_index.last.succ
@@ -81,11 +81,11 @@ def accept_list_item_start list_item
8181
attributes(label).strip
8282
end.join "\n"
8383

84-
bullets << "\n:"
84+
bullets << "\n" unless bullets.empty?
8585

8686
@prefix = ' ' * @indent
8787
@indent += 4
88-
@prefix << bullets + (' ' * (@indent - 1))
88+
@prefix << bullets << ":" << (' ' * (@indent - 1))
8989
else
9090
bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.'
9191
@prefix = (' ' * @indent) + bullet.ljust(4)

test/rdoc/test_rdoc_markup_to_markdown.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def accept_list_item_end_bullet
6969
end
7070

7171
def accept_list_item_end_label
72-
assert_equal "cat\n: ", @to.res.join
72+
assert_equal "cat\n: \n", @to.res.join
7373
assert_equal 0, @to.indent, 'indent'
7474
end
7575

@@ -79,7 +79,7 @@ def accept_list_item_end_lalpha
7979
end
8080

8181
def accept_list_item_end_note
82-
assert_equal "cat\n: ", @to.res.join
82+
assert_equal "cat\n: \n", @to.res.join
8383
assert_equal 0, @to.indent, 'indent'
8484
end
8585

@@ -319,9 +319,7 @@ def list_nested
319319
expected = <<-EXPECTED
320320
* l1
321321
* l1.1
322-
323322
* l2
324-
325323
EXPECTED
326324

327325
assert_equal expected, @to.end_accepting
@@ -343,7 +341,6 @@ def list_verbatim
343341
344342
* second
345343
346-
347344
EXPECTED
348345

349346
assert_equal expected, @to.end_accepting

0 commit comments

Comments
 (0)