Skip to content

Commit c6c51aa

Browse files
committed
Fix ToRdoc generating incorrect {label,name}-lists
Previously, trying to round-trip label-list and name-lists with the ToRdoc converter was not possible: ```ruby doc = <<~RDOC foo :: bar :: hi RDOC markup = RDoc::Markup.parse(doc) markup # => [doc: [list: NOTE [item: ["foo ", "bar"]; [para: "hi"]]]] rt = RDoc::Markup::ToRdoc.new.convert(markup) rt # => "foo\nbar:\n hi\n\n" rt_markup = RDoc::Markup.parse(rt) rt_markup # => [doc: [para: "foo ", "bar:"], [verb: "hi\n"]] ``` This commit addresses the issue by fixing ToRdoc to generate output that can be properly reparsed by RDoc. ToRdoc tests additionally needed to be updated for the new output. The old implementation of `accept_list_item_start` was copied to ToBs because those tests did not pass with the new changes and I am unfamiliar with the `backspace` format. After: ```ruby doc = <<~RDOC foo :: bar :: hi RDOC markup = RDoc::Markup.parse(doc) markup # => [doc: [list: NOTE [item: ["foo ", "bar"]; [para: "hi"]]]] rt = RDoc::Markup::ToRdoc.new.convert(markup) rt # => "foo::\nbar::\n hi\n\n" rt_markup = RDoc::Markup.parse(rt) rt_markup # => [doc: [list: NOTE [item: ["foo", "bar"]; [para: "hi"], blankline]]] ```
1 parent 4275958 commit c6c51aa

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

lib/rdoc/markup/to_bs.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,31 @@ def accept_heading heading
4040
@res << "\n"
4141
end
4242

43+
##
44+
# Prepares the visitor for consuming +list_item+
45+
46+
def accept_list_item_start list_item
47+
type = @list_type.last
48+
49+
case type
50+
when :NOTE, :LABEL then
51+
bullets = Array(list_item.label).map do |label|
52+
attributes(label).strip
53+
end.join "\n"
54+
55+
bullets << ":\n" unless bullets.empty?
56+
57+
@prefix = ' ' * @indent
58+
@indent += 2
59+
@prefix << bullets + (' ' * @indent)
60+
else
61+
bullet = type == :BULLET ? '*' : @list_index.last.to_s + '.'
62+
@prefix = (' ' * @indent) + bullet.ljust(bullet.length + 1)
63+
width = bullet.length + 1
64+
@indent += width
65+
end
66+
end
67+
4368
##
4469
# Turns on or off regexp handling for +convert_string+
4570

lib/rdoc/markup/to_rdoc.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,19 @@ def accept_list_item_start list_item
145145

146146
case type
147147
when :NOTE, :LABEL then
148-
bullets = Array(list_item.label).map do |label|
148+
stripped_labels = Array(list_item.label).map do |label|
149149
attributes(label).strip
150-
end.join "\n"
150+
end
151+
152+
bullets = case type
153+
when :NOTE
154+
stripped_labels.map { |b| "#{b}::" }
155+
when :LABEL
156+
stripped_labels.map { |b| "[#{b}]" }
157+
end
151158

152-
bullets << ":\n" unless bullets.empty?
159+
bullets = bullets.join("\n")
160+
bullets << "\n" unless stripped_labels.empty?
153161

154162
@prefix = ' ' * @indent
155163
@indent += 2

test/rdoc/test_rdoc_markup_to_rdoc.rb

Lines changed: 9 additions & 9 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", @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", @to.res.join
8383
assert_equal 0, @to.indent, 'indent'
8484
end
8585

@@ -100,7 +100,7 @@ def accept_list_item_start_bullet
100100

101101
def accept_list_item_start_label
102102
assert_equal [""], @to.res
103-
assert_equal "cat:\n ", @to.prefix
103+
assert_equal "[cat]\n ", @to.prefix
104104

105105
assert_equal 2, @to.indent
106106
end
@@ -115,7 +115,7 @@ def accept_list_item_start_lalpha
115115

116116
def accept_list_item_start_note
117117
assert_equal [""], @to.res
118-
assert_equal "cat:\n ", @to.prefix
118+
assert_equal "cat::\n ", @to.prefix
119119

120120
assert_equal 2, @to.indent
121121
end
@@ -243,16 +243,16 @@ def accept_heading_suppressed_crossref
243243
end
244244

245245
def accept_list_item_start_note_2
246-
assert_equal "<tt>teletype</tt>:\n teletype description\n\n", @to.res.join
246+
assert_equal "<tt>teletype</tt>::\n teletype description\n\n", @to.res.join
247247
end
248248

249249
def accept_list_item_start_note_multi_description
250-
assert_equal "label:\n description one\n\n description two\n\n",
250+
assert_equal "label::\n description one\n\n description two\n\n",
251251
@to.res.join
252252
end
253253

254254
def accept_list_item_start_note_multi_label
255-
assert_equal "one\ntwo:\n two headers\n\n", @to.res.join
255+
assert_equal "one::\ntwo::\n two headers\n\n", @to.res.join
256256
end
257257

258258
def accept_paragraph_b
@@ -355,8 +355,8 @@ def test_convert_list_note
355355
NOTE_LIST
356356

357357
expected = <<-EXPECTED
358-
foo
359-
bar:
358+
foo::
359+
bar::
360360
hi
361361
362362
EXPECTED

0 commit comments

Comments
 (0)