Permalink
Browse files

Pulling in support for "impure" block comments

Closes issue #37
  • Loading branch information...
2 parents 6736281 + e727446 commit 61d84c85c22a5c84cef3ceec43cad91b34da6c6c @mikewest mikewest committed Apr 20, 2011
Showing with 87 additions and 2 deletions.
  1. +22 −1 lib/rocco.rb
  2. +64 −0 test/test_block_comment_styles.rb
  3. +1 −1 test/test_heredoc.rb
View
@@ -300,6 +300,9 @@ def parse(data)
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 @@ def parse(data)
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 @@ def parse(data)
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]
@@ -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
View
@@ -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(

0 comments on commit 61d84c8

Please sign in to comment.