diff --git a/lib/rocco.rb b/lib/rocco.rb index a1786aa..1c0aaed 100644 --- a/lib/rocco.rb +++ b/lib/rocco.rb @@ -300,6 +300,9 @@ class Rocco if not @options[:comment_chars][:multi].nil? block_comment_start = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*$") block_comment_end = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$") + block_comment_one_liner = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$") + block_comment_start_with = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*(.*?)$") + block_comment_end_with = Regexp.new("\\s*(.*?)\\s*#{Regexp.escape(@options[:comment_chars][:multi][:end])}\\s*$") if @options[:comment_chars][:multi][:middle] block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?") end @@ -309,10 +312,15 @@ class Rocco end lines.each do |line| # If we're currently in a comment block, check whether the line matches - # the _end_ of a comment block. + # the _end_ of a comment block or the _end_ of a comment block with a + # comment. if in_comment_block if block_comment_end && line.match( block_comment_end ) in_comment_block = false + elsif block_comment_end_with && line.match( block_comment_end_with ) + in_comment_block = false + docs << line.match( block_comment_end_with ).captures.first. + sub( block_comment_mid || '', '' ) else docs << line.sub( block_comment_mid || '', '' ) end @@ -331,12 +339,25 @@ class Rocco if heredoc_start && line.match( heredoc_start ) in_heredoc = $1 code << line + elsif block_comment_one_liner && line.match( block_comment_one_liner ) + if code.any? + sections << [docs, code] + docs, code = [], [] + end + docs << line.match( block_comment_one_liner ).captures.first elsif block_comment_start && line.match( block_comment_start ) in_comment_block = true if code.any? sections << [docs, code] docs, code = [], [] end + elsif block_comment_start_with && line.match( block_comment_start_with ) + in_comment_block = true + if code.any? + sections << [docs, code] + docs, code = [], [] + end + docs << line.match( block_comment_start_with ).captures.first elsif single_line_comment && line.match( single_line_comment ) if code.any? sections << [docs, code] diff --git a/test/test_block_comment_styles.rb b/test/test_block_comment_styles.rb new file mode 100644 index 0000000..8fb55d5 --- /dev/null +++ b/test/test_block_comment_styles.rb @@ -0,0 +1,64 @@ +require File.expand_path('../helper', __FILE__) + +class RoccoBlockCommentTest < Test::Unit::TestCase + def test_one_liner + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/** Comment 1 */\ndef codeblock\nend\n" ) + ) + end + + def test_block_start_with_comment + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/** Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" ) + ) + end + + def test_block_end_with_comment + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/**\n * Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) + ) + end + + def test_block_end_with_comment_and_middle + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/**\n * Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) + ) + end + + def test_block_start_with_comment_and_end_with_comment + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/** Comment 1a\n Comment 1b */\ndef codeblock\nend\n" ) + ) + end + + def test_block_start_with_comment_and_end_with_comment_and_middle + r = Rocco.new( 'test', '', { :language => "c" } ) { "" } # Generate throwaway instance so I can test `parse` + assert_equal( + [ + [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] + ], + r.parse( "/** Comment 1a\n * Comment 1b */\ndef codeblock\nend\n" ) + ) + end + +end diff --git a/test/test_heredoc.rb b/test/test_heredoc.rb index a6b8399..bc38096 100644 --- a/test/test_heredoc.rb +++ b/test/test_heredoc.rb @@ -1,6 +1,6 @@ require File.expand_path('../helper', __FILE__) -class RoccoBlockCommentTest < Test::Unit::TestCase +class RoccoHeredocTest < Test::Unit::TestCase def test_basics r = Rocco.new( 'test', '', { :language => "rb" } ) { "" } # Generate throwaway instance so I can test `parse` assert_equal(