diff --git a/comment_parser/parsers/ruby_parser.py b/comment_parser/parsers/ruby_parser.py index 5e042e9..3f25075 100644 --- a/comment_parser/parsers/ruby_parser.py +++ b/comment_parser/parsers/ruby_parser.py @@ -21,7 +21,9 @@ def extract_comments(code): """ pattern = r""" (?P ([\"'])((?:\\\2|(?:(?!\2)).)*)(\2)) | - (?P \#(?P.*?)$) + (?P \#(?P.*?)$) | + (?P ^=begin\n(?P(.|\n)*?)?\n=end$) | + (?P ^=begin$\*(.*)?) """ compiled = re.compile(pattern, re.VERBOSE | re.MULTILINE) @@ -40,5 +42,10 @@ def extract_comments(code): comment_content = match.group("single_content") comment = common.Comment(comment_content, line_no + 1) comments.append(comment) - + elif kind == "multi": + comment_content = match.group("multi_content") + comment = common.Comment(comment_content, line_no + 1, multiline=True) + comments.append(comment) + elif kind == "error": + raise common.UnterminatedCommentError() return comments diff --git a/comment_parser/parsers/tests/ruby_parser_test.py b/comment_parser/parsers/tests/ruby_parser_test.py index 44babf2..c9e0eb3 100644 --- a/comment_parser/parsers/tests/ruby_parser_test.py +++ b/comment_parser/parsers/tests/ruby_parser_test.py @@ -6,7 +6,7 @@ from comment_parser.parsers import ruby_parser -class ShellParserTest(unittest.TestCase): +class RubyParserTest(unittest.TestCase): def testComment(self): code = '# comment' @@ -63,3 +63,15 @@ def testDifferentLiteralsSeparatedByComment(self): comments = ruby_parser.extract_comments(code) expected = [common.Comment(code[11:], 1, multiline=False)] self.assertEqual(comments, expected) + + def testMultilineComment(self): + code = '=begin\nThis is a multiline comment.' \ + 'It is not terminated by =end \n=end' + comments = ruby_parser.extract_comments(code) + expected = [ + common.Comment( + "This is a multiline comment.It is not terminated by =end ", + 1, + multiline=True) + ] + self.assertEqual(comments, expected)