Skip to content

Commit ced2fa3

Browse files
committed
Fix handling embdoc
In syntax highlighting on documentation, like below, "=begin\n" and "=end" are trimmed. # document # # =begin # test embdoc # =end # def blah end It's sample code in documentation, so the trimming behavior is a bug. This commit fixes it. And unnecessary blank line behavior in <pre> block is removed.
1 parent e752c73 commit ced2fa3

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,18 @@ def lex_init()
450450
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
451451
|op, io|
452452
@ltype = "="
453-
res = ''
454-
nil until getc == "\n"
453+
res = op
454+
until (ch = getc) == "\n" do
455+
res << ch
456+
end
457+
res << ch
455458

456459
until ( peek_equal?("=end") && peek(4) =~ /\s/ ) do
457460
(ch = getc)
458461
res << ch
459462
end
460463

461-
gets # consume =end
464+
res << gets # consume =end
462465

463466
@ltype = nil
464467
Token(TkRD_COMMENT, res)

lib/rdoc/token_stream.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ def self.to_html token_stream
4444
when RDoc::RubyToken::TkVal then 'ruby-value'
4545
end
4646

47-
text = CGI.escapeHTML t.text
47+
comment_with_nl = false
48+
case t
49+
when RDoc::RubyToken::TkRD_COMMENT, RDoc::RubyToken::TkHEREDOCEND
50+
comment_with_nl = true if t.text =~ /\n$/
51+
text = t.text.rstrip
52+
else
53+
text = t.text
54+
end
55+
text = CGI.escapeHTML text
4856

4957
if style then
50-
"<span class=\"#{style}\">#{text}</span>"
58+
"<span class=\"#{style}\">#{text}</span>#{"\n" if comment_with_nl}"
5159
else
5260
text
5361
end

test/test_rdoc_parser_ruby.rb

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class C; end
7474

7575
comment = parser.collect_first_comment
7676

77-
assert_equal RDoc::Comment.new("first\n\n", @top_level), comment
77+
assert_equal RDoc::Comment.new("=begin\nfirst\n=end\n\n", @top_level), comment
7878
end
7979

8080
def test_get_class_or_module
@@ -2499,6 +2499,35 @@ def blah()
24992499
assert_equal markup_code, expected
25002500
end
25012501

2502+
def test_parse_statements_postfix_if_after_heredocbeg
2503+
@filename = 'file.rb'
2504+
util_parser <<RUBY
2505+
class Foo
2506+
def blah()
2507+
<<~EOM if true
2508+
EOM
2509+
end
2510+
end
2511+
RUBY
2512+
2513+
expected = <<EXPTECTED
2514+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">blah</span>()
2515+
<span class="ruby-identifier">&lt;&lt;~EOM</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>
2516+
<span class="ruby-value"></span><span class="ruby-identifier"> EOM</span>
2517+
<span class="ruby-keyword">end</span>
2518+
EXPTECTED
2519+
expected = expected.rstrip
2520+
2521+
@parser.scan
2522+
2523+
foo = @top_level.classes.first
2524+
assert_equal 'Foo', foo.full_name
2525+
2526+
blah = foo.method_list.first
2527+
markup_code = blah.markup_code.sub(/^.*\n/, '')
2528+
assert_equal markup_code, expected
2529+
end
2530+
25022531
def test_parse_require_dynamic_string
25032532
content = <<-RUBY
25042533
prefix = 'path'
@@ -2948,11 +2977,11 @@ def m() end
29482977

29492978
foo = @top_level.classes.first
29502979

2951-
assert_equal 'Foo comment', foo.comment.text
2980+
assert_equal "=begin rdoc\nFoo comment\n=end", foo.comment.text
29522981

29532982
m = foo.method_list.first
29542983

2955-
assert_equal 'm comment', m.comment.text
2984+
assert_equal "=begin\nm comment\n=end", m.comment.text
29562985
end
29572986

29582987
def test_scan_block_comment_nested # Issue #41
@@ -2974,7 +3003,7 @@ class Bar
29743003
foo = @top_level.modules.first
29753004

29763005
assert_equal 'Foo', foo.full_name
2977-
assert_equal 'findmeindoc', foo.comment.text
3006+
assert_equal "=begin rdoc\nfindmeindoc\n=end", foo.comment.text
29783007

29793008
bar = foo.classes.first
29803009

@@ -3021,12 +3050,12 @@ def lauren
30213050

30223051
foo = @top_level.classes.first
30233052

3024-
assert_equal "= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word",
3053+
assert_equal "=begin rdoc\n\n= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word\n\n=end",
30253054
foo.comment.text
30263055

30273056
m = foo.method_list.first
30283057

3029-
assert_equal 'A nice girl', m.comment.text
3058+
assert_equal "=begin rdoc\nA nice girl\n=end", m.comment.text
30303059
end
30313060

30323061
def test_scan_class_nested_nodoc

0 commit comments

Comments
 (0)