Skip to content

Commit

Permalink
[ruby/rdoc] Fix ToMarkdown missing newlines for label-lists
Browse files Browse the repository at this point in the history
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
  • Loading branch information
skipkayhil authored and matzbot committed Mar 8, 2024
1 parent e8f796e commit 4756eaf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
8 changes: 4 additions & 4 deletions lib/rdoc/markup/to_markdown.rb
Expand Up @@ -45,8 +45,6 @@ def handle_regexp_HARD_BREAK target
# Finishes consumption of `list`

def accept_list_end list
@res << "\n"

super
end

Expand All @@ -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
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions test/rdoc/test_rdoc_markup_to_markdown.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -319,9 +319,7 @@ def list_nested
expected = <<-EXPECTED
* l1
* l1.1
* l2
EXPECTED

assert_equal expected, @to.end_accepting
Expand All @@ -343,7 +341,6 @@ def list_verbatim
* second
EXPECTED

assert_equal expected, @to.end_accepting
Expand Down

0 comments on commit 4756eaf

Please sign in to comment.