Python block comments (no middle character), and CSS syntax

This commit is contained in:
Mike West 2010-11-22 13:38:03 +01:00
parent 77dff765b6
commit d0211ecc99
2 changed files with 34 additions and 6 deletions

View File

@ -211,6 +211,7 @@ class Rocco
"c" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "c" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } },
"coffee-script" => { :single => "#", :multi => { :start => "###", :middle => nil, :end => "###" } }, "coffee-script" => { :single => "#", :multi => { :start => "###", :middle => nil, :end => "###" } },
"cpp" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "cpp" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } },
"css" => { :single => nil, :multi => { :start => "/**", :middle => "*", :end => "*/" } },
"java" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "java" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } },
"js" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } }, "js" => { :single => "//", :multi => { :start => "/**", :middle => "*", :end => "*/" } },
"lua" => { :single => "--", :multi => nil }, "lua" => { :single => "--", :multi => nil },
@ -265,7 +266,7 @@ class Rocco
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
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 the beginning of a block, or
# a single-line comment all on it's lonesome. In either case, if there's # a single-line comment all on it's lonesome. In either case, if there's
@ -282,7 +283,7 @@ class Rocco
sections << [docs, code] sections << [docs, code]
docs, code = [], [] docs, code = [], []
end end
docs << line.sub( single_line_comment, '' ) docs << line.sub( single_line_comment || '', '' )
else else
code << line code << line
end end

View File

@ -7,13 +7,13 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
[ [
[ [ "Comment 1" ], [ "def codeblock", "end" ] ] [ [ "Comment 1" ], [ "def codeblock", "end" ] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n" ) r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n" )
) )
assert_equal( assert_equal(
[ [
[ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ] [ [ "Comment 1a", "Comment 1b" ], [ "def codeblock", "end" ] ]
], ],
r.parse( "/**\n * Comment 1a\n * Comment 1b\n*/\ndef codeblock\nend\n" ) r.parse( "/**\n * Comment 1a\n * Comment 1b\n */\ndef codeblock\nend\n" )
) )
end end
def test_multiple_blocks def test_multiple_blocks
@ -23,14 +23,41 @@ class RoccoBlockCommentTest < Test::Unit::TestCase
[ [ "Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
[ [ "Comment 2" ], [] ] [ [ "Comment 2" ], [] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\n" ) r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\n" )
) )
assert_equal( assert_equal(
[ [
[ [ "Comment 1" ], [ "def codeblock", "end" ] ], [ [ "Comment 1" ], [ "def codeblock", "end" ] ],
[ [ "Comment 2" ], [ "if false", "end" ] ] [ [ "Comment 2" ], [ "if false", "end" ] ]
], ],
r.parse( "/**\n * Comment 1\n*/\ndef codeblock\nend\n/**\n * Comment 2\n*/\nif false\nend" ) r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\nif false\nend" )
)
end
def test_block_without_middle_character
r = Rocco.new( 'test', '', { :language => "python" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal(
[
[ [ " Comment 1" ], [ "def codeblock", "end" ] ],
[ [ " Comment 2" ], [] ]
],
r.parse( "\"\"\"\n Comment 1\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2\n\"\"\"\n" )
)
assert_equal(
[
[ [ " Comment 1" ], [ "def codeblock", "end" ] ],
[ [ " Comment 2" ], [ "if false", "end" ] ]
],
r.parse( "\"\"\"\n Comment 1\n\"\"\"\ndef codeblock\nend\n\"\"\"\n Comment 2\n\"\"\"\nif false\nend" )
)
end
def test_language_without_single_line_comments
r = Rocco.new( 'test', '', { :language => "css" } ) { "" } # Generate throwaway instance so I can test `parse`
assert_equal(
[
[ [ "Comment 1" ], [ "def codeblock", "end" ] ],
[ [ "Comment 2" ], [ "if false", "end" ] ]
],
r.parse( "/**\n * Comment 1\n */\ndef codeblock\nend\n/**\n * Comment 2\n */\nif false\nend" )
) )
end end
end end