added support for impure block comment styles
This commit is contained in:
parent
d495074320
commit
c39dbd7816
31
lib/rocco.rb
31
lib/rocco.rb
@ -289,29 +289,50 @@ class Rocco
|
|||||||
if not @options[:comment_chars][:multi].nil?
|
if not @options[:comment_chars][:multi].nil?
|
||||||
block_comment_start = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:start])}\\s*$")
|
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_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]
|
if @options[:comment_chars][:multi][:middle]
|
||||||
block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?")
|
block_comment_mid = Regexp.new("^\\s*#{Regexp.escape(@options[:comment_chars][:multi][:middle])}\\s?")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
lines.each do |line|
|
lines.each do |line|
|
||||||
# If we're currently in a comment block, check whether the line matches
|
# 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 in_comment_block
|
||||||
if block_comment_end && line.match( block_comment_end )
|
if block_comment_end && line.match( block_comment_end )
|
||||||
in_comment_block = false
|
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
|
else
|
||||||
docs << line.sub( block_comment_mid || '', '' )
|
docs << line.sub( block_comment_mid || '', '' )
|
||||||
end
|
end
|
||||||
# Otherwise, check whether the line matches the beginning of a block, or
|
# Otherwise, check whether the line matches a one-liner block comment,
|
||||||
# a single-line comment all on it's lonesome. In either case, if there's
|
# the beginning of a block, the beginning of a block with a comment, or a
|
||||||
# code, start a new section
|
# single-line comment. In any case, if there's code, start a new section.
|
||||||
else
|
else
|
||||||
if block_comment_start && line.match( block_comment_start )
|
if 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
|
in_comment_block = true
|
||||||
if code.any?
|
if code.any?
|
||||||
sections << [docs, code]
|
sections << [docs, code]
|
||||||
docs, code = [], []
|
docs, code = [], []
|
||||||
end
|
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 )
|
elsif single_line_comment && line.match( single_line_comment )
|
||||||
if code.any?
|
if code.any?
|
||||||
sections << [docs, code]
|
sections << [docs, code]
|
||||||
|
64
test/test_block_comment_styles.rb
Normal file
64
test/test_block_comment_styles.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user