Skip to content

Commit 63f798a

Browse files
committed
Add experimental feature to insert a blank line if a line of a paragraph ends in two spaces
1 parent 297cd5a commit 63f798a

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

History.rdoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
* "Top" link in section headers is no longer inside the heading element.
7474
* RDoc avoids printing some warnings unless run with `rdoc --verbose`. For
7575
Rails issue #1646.
76+
* Finishing a paragraph with two or more spaces will result in a line break.
77+
This feature is experimental and may be modified or removed.
7678

7779
* Bug fixes
7880
* Markup defined by RDoc::Markup#add_special inside a <tt><tt></tt> is no

lib/rdoc/markup/parser.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ def build_paragraph margin
203203

204204
if type == :TEXT && column == margin then
205205
paragraph << data
206+
207+
if peek_token[0] == :BREAK then
208+
break
209+
end
210+
206211
skip :NEWLINE
207212
else
208213
unget
@@ -268,7 +273,7 @@ def build_verbatim margin
268273
peek_column ||= column + width
269274
indent = peek_column - column - width
270275
line << ' ' * indent
271-
when :TEXT then
276+
when :BREAK, :TEXT then
272277
line << data
273278
else # *LIST_TOKENS
274279
list_marker = case type
@@ -322,7 +327,12 @@ def parse parent, indent = 0
322327
until @tokens.empty? do
323328
type, data, column, = get
324329

325-
if type == :NEWLINE then
330+
case type
331+
when :BREAK then
332+
parent << RDoc::Markup::BlankLine.new
333+
skip :NEWLINE, false
334+
next
335+
when :NEWLINE then
326336
# trailing newlines are skipped below, so this is a blank line
327337
parent << RDoc::Markup::BlankLine.new
328338
skip :NEWLINE, false
@@ -458,8 +468,15 @@ def tokenize input
458468
when s.scan(/(.*?)::( +|\r?$)/) then
459469
[:NOTE, s[1], *token_pos(pos)]
460470
# anything else: :TEXT
461-
else s.scan(/.*/)
462-
[:TEXT, s.matched.sub(/\r$/, ''), *token_pos(pos)]
471+
else s.scan(/(.*?)( )?\r?$/)
472+
token = [:TEXT, s[1], *token_pos(pos)]
473+
474+
if s[2] then
475+
@tokens << token
476+
[:BREAK, s[2], *token_pos(pos + s[1].length)]
477+
else
478+
token
479+
end
463480
end
464481
end
465482

test/test_rdoc_markup_parser.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,17 @@ def test_parse_lalpha_utf_8
441441
assert_equal expected, @RMP.parse(str).parts
442442
end
443443

444+
def test_parse_line_break
445+
str = "now is\nthe time \nfor all"
446+
447+
expected = [
448+
@RM::Paragraph.new('now is', 'the time'),
449+
@RM::BlankLine.new,
450+
@RM::Paragraph.new('for all')]
451+
452+
assert_equal expected, @RMP.parse(str).parts
453+
end
454+
444455
def test_parse_list_list_1
445456
str = <<-STR
446457
10. para 1
@@ -1233,6 +1244,53 @@ def test_tokenize_lalpha
12331244
assert_equal expected, @RMP.tokenize(str)
12341245
end
12351246

1247+
def test_tokenize_line_break
1248+
str = "now is\nthe time \nfor all\n"
1249+
1250+
expected = [
1251+
[:TEXT, 'now is', 0, 0],
1252+
[:NEWLINE, "\n", 6, 0],
1253+
[:TEXT, 'the time', 0, 1],
1254+
[:BREAK, " ", 8, 1],
1255+
[:NEWLINE, "\n", 10, 1],
1256+
[:TEXT, 'for all', 0, 2],
1257+
[:NEWLINE, "\n", 7, 2],
1258+
]
1259+
1260+
assert_equal expected, @RMP.tokenize(str)
1261+
end
1262+
1263+
def test_tokenize_line_break_long
1264+
str = "now is\nthe time \nfor all\n"
1265+
1266+
expected = [
1267+
[:TEXT, 'now is', 0, 0],
1268+
[:NEWLINE, "\n", 6, 0],
1269+
[:TEXT, 'the time ', 0, 1],
1270+
[:BREAK, ' ', 9, 1],
1271+
[:NEWLINE, "\n", 11, 1],
1272+
[:TEXT, 'for all', 0, 2],
1273+
[:NEWLINE, "\n", 7, 2],
1274+
]
1275+
1276+
assert_equal expected, @RMP.tokenize(str)
1277+
end
1278+
1279+
def test_tokenize_line_break_no_short
1280+
str = "now is\nthe time \nfor all\n"
1281+
1282+
expected = [
1283+
[:TEXT, 'now is', 0, 0],
1284+
[:NEWLINE, "\n", 6, 0],
1285+
[:TEXT, 'the time ', 0, 1],
1286+
[:NEWLINE, "\n", 9, 1],
1287+
[:TEXT, 'for all', 0, 2],
1288+
[:NEWLINE, "\n", 7, 2],
1289+
]
1290+
1291+
assert_equal expected, @RMP.tokenize(str)
1292+
end
1293+
12361294
def test_tokenize_note
12371295
str = <<-STR
12381296
cat:: l1

0 commit comments

Comments
 (0)