diff --git a/doxec/__init__.py b/doxec/__init__.py index 6c8c6772cdb5d960119ff99553a8ea91f31c8b5a..ea23cfc640997ee8e015e16f0b4f7ca7e796ba07 100644 --- a/doxec/__init__.py +++ b/doxec/__init__.py @@ -199,19 +199,25 @@ class Markdown(DoxecSyntax): <!-- COMMAND ARGS --> + The args part must not contain the greater than '>" character. The + trailing '-->' is optional. None is returned, if the format does not match. If a valid line is found, the tuple (command, args) is returned. >>> Markdown.parse_command("<!-- write file.txt -->") ('write', 'file.txt') - >>> Markdown.parse_command("<!-- invalid line ") is None + >>> Markdown.parse_command("<!-- write file.txt") + ('write', 'file.txt') + + >>> Markdown.parse_command("-- invalid line -->") is None True """ - match = re.match(r"<!--\s+(\S.*\S)\s+-->\s*$", line) + match = re.match(r"<!--\s+([^> \t\n\r\f\v][^>]*)(\s+-->)?\s*$", line) if match is None: return None - token = re.split("\s+", match.group(1), maxsplit=1) + + token = re.split("\s+", match.group(1).strip(), maxsplit=1) if len(token) == 2: return tuple(token) else: diff --git a/doxec/tests/markdown.py b/doxec/tests/markdown.py index 771f84558832971695eaadc9705ab9e0a56b9c9f..6e31b87bebbd4b3d3ff31cc6b250488e8909d5e4 100644 --- a/doxec/tests/markdown.py +++ b/doxec/tests/markdown.py @@ -14,6 +14,14 @@ class MarkdownSyntaxTestCase(unittest.TestCase): retval = Markdown.parse_command("<!-- WRITE hello_world.c -->") self.assertEqual(retval, ("WRITE", "hello_world.c")) + def test_parse_command_valid_no_end(self): + """ + Run parse_command on an input without the comment end `-->` and check + the return value. + """ + retval = Markdown.parse_command("<!-- WRITE hello_world.c") + self.assertEqual(retval, ("WRITE", "hello_world.c")) + def test_parse_command_whitespace(self): """ Run parse_command on a valid input with whitespace added/removed in various @@ -69,6 +77,7 @@ class MarkdownSyntaxTestCase(unittest.TestCase): retval = Markdown.parse_command("<! -- WRITE hello_world.c -->") self.assertIsNone(retval) + # this is still invalid, since the args part must not contain '>' retval = Markdown.parse_command("<!-- WRITE hello_world.c - ->") self.assertIsNone(retval) @@ -83,9 +92,6 @@ class MarkdownSyntaxTestCase(unittest.TestCase): retval = Markdown.parse_command("!-- WRITE hello_world.c -->") self.assertIsNone(retval) - retval = Markdown.parse_command("<!-- WRITE hello_world.c ") - self.assertIsNone(retval) - retval = Markdown.parse_command("WRITE hello_world.c") self.assertIsNone(retval)