Skip to content

Fix handling embdoc and unnecessary blank line in <pre> #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/rdoc/ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,18 @@ def lex_init()
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
|op, io|
@ltype = "="
res = ''
nil until getc == "\n"
res = op
until (ch = getc) == "\n" do
res << ch
end
res << ch

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

gets # consume =end
res << gets # consume =end

@ltype = nil
Token(TkRD_COMMENT, res)
Expand Down
12 changes: 10 additions & 2 deletions lib/rdoc/token_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@ def self.to_html token_stream
when RDoc::RubyToken::TkVal then 'ruby-value'
end

text = CGI.escapeHTML t.text
comment_with_nl = false
case t
when RDoc::RubyToken::TkRD_COMMENT, RDoc::RubyToken::TkHEREDOCEND
comment_with_nl = true if t.text =~ /\n$/
text = t.text.rstrip
else
text = t.text
end
text = CGI.escapeHTML text

if style then
"<span class=\"#{style}\">#{text}</span>"
"<span class=\"#{style}\">#{text}</span>#{"\n" if comment_with_nl}"
else
text
end
Expand Down
50 changes: 42 additions & 8 deletions test/test_rdoc_parser_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class C; end

comment = parser.collect_first_comment

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

def test_get_class_or_module
Expand Down Expand Up @@ -2513,8 +2513,8 @@ def blah()
expected = <<EXPTECTED
<span class="ruby-keyword">def</span> <span class="ruby-identifier">blah</span>()
<span class="ruby-identifier">&lt;&lt;~EOM</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>
<span class="ruby-value"></span><span class="ruby-identifier"> EOM
</span> <span class="ruby-keyword">end</span>
<span class="ruby-value"></span><span class="ruby-identifier"> EOM</span>
<span class="ruby-keyword">end</span>
EXPTECTED
expected = expected.rstrip

Expand All @@ -2528,6 +2528,40 @@ def blah()
assert_equal markup_code, expected
end

def test_parse_statements_embdoc_in_document
@filename = 'file.rb'
util_parser <<RUBY
class Foo
# doc
#
# =begin
# test embdoc
# =end
#
def blah
end
end
RUBY

expected = <<EXPTECTED
<p>doc

<pre class="ruby"><span class="ruby-comment">=begin
test embdoc
=end</span>
</pre>
EXPTECTED

@parser.scan

foo = @top_level.classes.first
assert_equal 'Foo', foo.full_name

blah = foo.method_list.first
markup_comment = blah.search_record[6]
assert_equal markup_comment, expected
end

def test_parse_require_dynamic_string
content = <<-RUBY
prefix = 'path'
Expand Down Expand Up @@ -2977,11 +3011,11 @@ def m() end

foo = @top_level.classes.first

assert_equal 'Foo comment', foo.comment.text
assert_equal "=begin rdoc\nFoo comment\n=end", foo.comment.text

m = foo.method_list.first

assert_equal 'm comment', m.comment.text
assert_equal "=begin\nm comment\n=end", m.comment.text
end

def test_scan_block_comment_nested # Issue #41
Expand All @@ -3003,7 +3037,7 @@ class Bar
foo = @top_level.modules.first

assert_equal 'Foo', foo.full_name
assert_equal 'findmeindoc', foo.comment.text
assert_equal "=begin rdoc\nfindmeindoc\n=end", foo.comment.text

bar = foo.classes.first

Expand Down Expand Up @@ -3050,12 +3084,12 @@ def lauren

foo = @top_level.classes.first

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

m = foo.method_list.first

assert_equal 'A nice girl', m.comment.text
assert_equal "=begin rdoc\nA nice girl\n=end", m.comment.text
end

def test_scan_class_nested_nodoc
Expand Down