Skip to content

Commit 71b01e3

Browse files
authored
Merge pull request #494 from aycabta/fix-handling-embdoc
Fix handling embdoc and unnecessary blank line in <pre>
2 parents 9789e6f + 16eda99 commit 71b01e3

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,15 +454,18 @@ def lex_init()
454454
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
455455
|op, io|
456456
@ltype = "="
457-
res = ''
458-
nil until getc == "\n"
457+
res = op
458+
until (ch = getc) == "\n" do
459+
res << ch
460+
end
461+
res << ch
459462

460463
until ( peek_equal?("=end") && peek(4) =~ /\s/ ) do
461464
(ch = getc)
462465
res << ch
463466
end
464467

465-
gets # consume =end
468+
res << gets # consume =end
466469

467470
@ltype = nil
468471
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: 42 additions & 8 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
@@ -2513,8 +2513,8 @@ def blah()
25132513
expected = <<EXPTECTED
25142514
<span class="ruby-keyword">def</span> <span class="ruby-identifier">blah</span>()
25152515
<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
2517-
</span> <span class="ruby-keyword">end</span>
2516+
<span class="ruby-value"></span><span class="ruby-identifier"> EOM</span>
2517+
<span class="ruby-keyword">end</span>
25182518
EXPTECTED
25192519
expected = expected.rstrip
25202520

@@ -2528,6 +2528,40 @@ def blah()
25282528
assert_equal markup_code, expected
25292529
end
25302530

2531+
def test_parse_statements_embdoc_in_document
2532+
@filename = 'file.rb'
2533+
util_parser <<RUBY
2534+
class Foo
2535+
# doc
2536+
#
2537+
# =begin
2538+
# test embdoc
2539+
# =end
2540+
#
2541+
def blah
2542+
end
2543+
end
2544+
RUBY
2545+
2546+
expected = <<EXPTECTED
2547+
<p>doc
2548+
2549+
<pre class="ruby"><span class="ruby-comment">=begin
2550+
test embdoc
2551+
=end</span>
2552+
</pre>
2553+
EXPTECTED
2554+
2555+
@parser.scan
2556+
2557+
foo = @top_level.classes.first
2558+
assert_equal 'Foo', foo.full_name
2559+
2560+
blah = foo.method_list.first
2561+
markup_comment = blah.search_record[6]
2562+
assert_equal markup_comment, expected
2563+
end
2564+
25312565
def test_parse_require_dynamic_string
25322566
content = <<-RUBY
25332567
prefix = 'path'
@@ -3002,11 +3036,11 @@ def m() end
30023036

30033037
foo = @top_level.classes.first
30043038

3005-
assert_equal 'Foo comment', foo.comment.text
3039+
assert_equal "=begin rdoc\nFoo comment\n=end", foo.comment.text
30063040

30073041
m = foo.method_list.first
30083042

3009-
assert_equal 'm comment', m.comment.text
3043+
assert_equal "=begin\nm comment\n=end", m.comment.text
30103044
end
30113045

30123046
def test_scan_block_comment_nested # Issue #41
@@ -3028,7 +3062,7 @@ class Bar
30283062
foo = @top_level.modules.first
30293063

30303064
assert_equal 'Foo', foo.full_name
3031-
assert_equal 'findmeindoc', foo.comment.text
3065+
assert_equal "=begin rdoc\nfindmeindoc\n=end", foo.comment.text
30323066

30333067
bar = foo.classes.first
30343068

@@ -3075,12 +3109,12 @@ def lauren
30753109

30763110
foo = @top_level.classes.first
30773111

3078-
assert_equal "= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word",
3112+
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",
30793113
foo.comment.text
30803114

30813115
m = foo.method_list.first
30823116

3083-
assert_equal 'A nice girl', m.comment.text
3117+
assert_equal "=begin rdoc\nA nice girl\n=end", m.comment.text
30843118
end
30853119

30863120
def test_scan_class_nested_nodoc

0 commit comments

Comments
 (0)