Skip to content

Commit 3d620eb

Browse files
committed
Enable break_on_newline extension by default for Markdown
Enable the existing break_on_newline extension in DEFAULT_EXTENSIONS so the Markdown parser converts soft line breaks to HardBreak objects. This produces visible <br> line breaks in HTML output, matching GFM rendering. The conversion happens in the Markdown parser's paragraph() method, which is the proper place for Markdown-specific behavior. The generic accept_paragraph in ToHtml is unchanged and has no Markdown-specific logic. Also remove the CJK-aware newline-to-space gsub from accept_paragraph that is no longer needed. The RDoc markup parser already handles newline joining at parse time in build_paragraph.
1 parent 958470d commit 3d620eb

File tree

5 files changed

+411
-365
lines changed

5 files changed

+411
-365
lines changed

lib/rdoc/markup/to_html.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,6 @@ def accept_block_quote(block_quote)
313313
def accept_paragraph(paragraph)
314314
@res << "\n<p>"
315315
text = paragraph.text @hard_break
316-
text = text.gsub(/(#{SPACE_SEPARATED_LETTER_CLASS})?\K\r?\n(?=(?(1)(#{SPACE_SEPARATED_LETTER_CLASS})?))/o) {
317-
defined?($2) && ' '
318-
}
319316
@res << to_html(text)
320317
@res << "</p>\n"
321318
end

test/rdoc/markup/to_html_test.rb

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def accept_paragraph_br
236236
end
237237

238238
def accept_paragraph_break
239-
assert_equal "\n<p>hello<br> world</p>\n", @to.res.join
239+
assert_equal "\n<p>hello<br>\nworld</p>\n", @to.res.join
240240
end
241241

242242
def accept_paragraph_i
@@ -411,43 +411,13 @@ def test_accept_heading_dedup_resets_on_start_accepting
411411
end
412412

413413
def test_accept_paragraph_newline
414-
hellos = ["hello", "\u{393 3b5 3b9 3ac} \u{3c3 3bf 3c5}"]
415-
worlds = ["world", "\u{3ba 3cc 3c3 3bc 3bf 3c2}"]
416-
ohayo, sekai = %W"\u{304a 306f 3088 3046} \u{4e16 754c}"
417-
418-
hellos.product(worlds) do |hello, world|
419-
@to.start_accepting
420-
@to.accept_paragraph para("#{hello}\n", "#{world}\n")
421-
assert_equal "\n<p>#{hello} #{world}</p>\n", @to.res.join
422-
end
423-
424-
hellos.each do |hello|
425-
@to.start_accepting
426-
@to.accept_paragraph para("#{hello}\n", "#{sekai}\n")
427-
assert_equal "\n<p>#{hello}#{sekai}</p>\n", @to.res.join
428-
end
429-
430-
worlds.each do |world|
431-
@to.start_accepting
432-
@to.accept_paragraph para("#{ohayo}\n", "#{world}\n")
433-
assert_equal "\n<p>#{ohayo}#{world}</p>\n", @to.res.join
434-
end
435-
436414
@to.start_accepting
437-
@to.accept_paragraph para("#{ohayo}\n", "#{sekai}\n")
438-
assert_equal "\n<p>#{ohayo}#{sekai}</p>\n", @to.res.join
415+
@to.accept_paragraph para("hello\n", "world\n")
416+
assert_equal "\n<p>hello\nworld\n</p>\n", @to.res.join
439417

440418
@to.start_accepting
441419
@to.accept_paragraph para("+hello+\n", "world\n")
442-
assert_equal "\n<p><code>hello</code> world</p>\n", @to.res.join
443-
444-
@to.start_accepting
445-
@to.accept_paragraph para("hello\n", "+world+\n")
446-
assert_equal "\n<p>hello <code>world</code></p>\n", @to.res.join
447-
448-
@to.start_accepting
449-
@to.accept_paragraph para("+hello+\n", "+world+\n")
450-
assert_equal "\n<p><code>hello</code> <code>world</code></p>\n", @to.res.join
420+
assert_equal "\n<p><code>hello</code>\nworld\n</p>\n", @to.res.join
451421
end
452422

453423
def test_accept_heading_output_decoration

test/rdoc/parser/changelog_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ def test_scan_git
421421
'Masataka Pocke Kuwabara', 'kuwabara@pocke.me', '2021-01-01 14:25:08 +0900',
422422
[head(4, 'Make args info for RubyVM::AST to available on endless method without parens'),
423423
head(5, 'Problem'),
424-
para("Arguments information is missing for endless method without parens.\n" +
425-
"For example:"),
424+
para("Arguments information is missing for endless method without parens.",
425+
hard_break, "For example:"),
426426
verb("# ok\n").tap {|v| v.format = :ruby},
427427
para('It causes an error if a program expects <code>args</code> node exists.'),
428428
head(5, 'Solution'),

test/rdoc/rdoc_markdown_test.rb

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_parse_block_quote
5656
expected =
5757
doc(
5858
block(
59-
para("this is\na block quote")))
59+
para("this is", hard_break, "a block quote")))
6060

6161
assert_equal expected, doc
6262
end
@@ -70,11 +70,22 @@ def test_parse_block_quote_continue
7070
expected =
7171
doc(
7272
block(
73-
para("this is\na block quote")))
73+
para("this is", hard_break, "a block quote")))
7474

7575
assert_equal expected, doc
7676
end
7777

78+
def test_parse_block_quote_continue_html
79+
doc = parse <<-BLOCK_QUOTE
80+
> this is
81+
a block quote
82+
BLOCK_QUOTE
83+
84+
html = doc.accept(RDoc::Markup::ToHtml.new)
85+
86+
assert_include html, "<p>this is<br>\na block quote</p>"
87+
end
88+
7889
def test_parse_block_quote_list
7990
doc = parse <<-BLOCK_QUOTE
8091
> text
@@ -104,7 +115,7 @@ def test_parse_block_quote_newline
104115
expected =
105116
doc(
106117
block(
107-
para("this is\na block quote")))
118+
para("this is", hard_break, "a block quote")))
108119

109120
assert_equal expected, doc
110121
end
@@ -120,12 +131,77 @@ def test_parse_block_quote_separate
120131
expected =
121132
doc(
122133
block(
123-
para("this is\na block quote"),
134+
para("this is", hard_break, "a block quote")),
135+
block(
124136
para("that continues")))
125137

126138
assert_equal expected, doc
127139
end
128140

141+
def test_parse_block_quote_no_lazy_continuation_for_list
142+
doc = parse <<-BLOCK_QUOTE
143+
> foo
144+
- bar
145+
BLOCK_QUOTE
146+
147+
expected =
148+
doc(
149+
block(
150+
para("foo")),
151+
list(:BULLET,
152+
item(nil, para("bar"))))
153+
154+
assert_equal expected, doc
155+
end
156+
157+
def test_parse_block_quote_no_lazy_continuation_for_ordered_list
158+
doc = parse <<-BLOCK_QUOTE
159+
> foo
160+
1. bar
161+
BLOCK_QUOTE
162+
163+
expected =
164+
doc(
165+
block(
166+
para("foo")),
167+
list(:NUMBER,
168+
item(nil, para("bar"))))
169+
170+
assert_equal expected, doc
171+
end
172+
173+
def test_parse_block_quote_no_lazy_continuation_for_heading
174+
doc = parse <<-BLOCK_QUOTE
175+
> foo
176+
# bar
177+
BLOCK_QUOTE
178+
179+
expected =
180+
doc(
181+
block(
182+
para("foo")),
183+
head(1, "bar"))
184+
185+
assert_equal expected, doc
186+
end
187+
188+
def test_parse_block_quote_no_lazy_continuation_for_code_fence
189+
doc = parse <<~BLOCK_QUOTE
190+
> foo
191+
```
192+
code
193+
```
194+
BLOCK_QUOTE
195+
196+
expected =
197+
doc(
198+
block(
199+
para("foo")),
200+
verb("code\n"))
201+
202+
assert_equal expected, doc
203+
end
204+
129205
def test_parse_char_entity
130206
doc = parse '&pi; &nn;'
131207

@@ -262,7 +338,7 @@ def test_parse_code_github
262338
assert_equal expected, parse(doc)
263339

264340
expected =
265-
doc(para("Example:\n<code>\n""code goes here\n</code>"))
341+
doc(para("Example:", hard_break, "<code>\n""code goes here\n</code>"))
266342

267343
assert_equal expected, parse(doc.sub(/^\n/, ''))
268344
end
@@ -296,7 +372,7 @@ def test_parse_code_github_format
296372
assert_equal expected, parse(doc)
297373

298374
expected =
299-
doc(para("Example:\n<code>ruby\n""code goes here\n</code>"))
375+
doc(para("Example:", hard_break, "<code>ruby\n""code goes here\n</code>"))
300376

301377
assert_equal expected, parse(doc.sub(/^\n/, ''))
302378
end
@@ -343,7 +419,7 @@ def test_parse_definition_list_indents
343419
item(%w[one], para("Indented one characters")),
344420
item(%w[two], para("Indented two characters")),
345421
item(%w[three], para("Indented three characters"))),
346-
para("four\n : Indented four characters"))
422+
para("four", hard_break, " : Indented four characters"))
347423

348424
assert_equal expected, doc
349425
end
@@ -392,9 +468,9 @@ def test_parse_definition_list_multi_line
392468
expected = doc(
393469
list(:NOTE,
394470
item(%w[one],
395-
para("This is a definition\nthat extends to two lines")),
471+
para("This is a definition", hard_break, "that extends to two lines")),
396472
item(%w[two],
397-
para("This is another definition\nthat also extends to two lines"))))
473+
para("This is another definition", hard_break, "that also extends to two lines"))))
398474

399475
assert_equal expected, doc
400476
end
@@ -430,8 +506,8 @@ def test_parse_definition_list_no
430506
MD
431507

432508
expected = doc(
433-
para("one\n: This is a definition"),
434-
para("two\n: This is another definition"))
509+
para("one", hard_break, ": This is a definition"),
510+
para("two", hard_break, ": This is another definition"))
435511

436512
assert_equal expected, doc
437513
end
@@ -779,7 +855,7 @@ def test_parse_list_bullet_multiline
779855

780856
expected = doc(
781857
list(:BULLET,
782-
item(nil, para("one\n two"))))
858+
item(nil, para("one", hard_break, " two"))))
783859

784860
assert_equal expected, doc
785861
end
@@ -832,7 +908,7 @@ def test_parse_list_bullet_nest_continue
832908
para("outer"),
833909
list(:BULLET,
834910
item(nil,
835-
para("inner\n continue inner")))),
911+
para("inner", hard_break, " continue inner")))),
836912
item(nil,
837913
para("outer 2"))))
838914

@@ -899,7 +975,7 @@ def test_parse_note_indent
899975
expected = doc(
900976
para("Some text.{*1}[rdoc-label:foottext-1:footmark-1]"),
901977
rule(1),
902-
para("{^1}[rdoc-label:footmark-1:foottext-1] With a footnote\n\nmore"))
978+
para("{^1}[rdoc-label:footmark-1:foottext-1] With a footnote", hard_break, "more"))
903979

904980
assert_equal expected, doc
905981
end
@@ -940,8 +1016,10 @@ def test_parse_note_multiple
9401016
MD
9411017

9421018
expected = doc(
943-
para("Some text{*1}[rdoc-label:foottext-1:footmark-1]\n" +
944-
"with inline notes{*2}[rdoc-label:foottext-2:footmark-2]\n" +
1019+
para("Some text{*1}[rdoc-label:foottext-1:footmark-1]",
1020+
hard_break,
1021+
"with inline notes{*2}[rdoc-label:foottext-2:footmark-2]",
1022+
hard_break,
9451023
"and an extra note.{*3}[rdoc-label:foottext-3:footmark-3]"),
9461024

9471025
rule(1),
@@ -1040,7 +1118,7 @@ def test_parse_paragraph_indent_three
10401118
def test_parse_paragraph_multiline
10411119
doc = parse "one\ntwo"
10421120

1043-
expected = doc(para("one\ntwo"))
1121+
expected = doc(para("one", hard_break, "two"))
10441122

10451123
assert_equal expected, doc
10461124
end

0 commit comments

Comments
 (0)