From 60cc93321977ed9e89916aca7b6324be958329b2 Mon Sep 17 00:00:00 2001 From: Frank Sauerburger <frank@sauerburger.com> Date: Thu, 3 Aug 2017 23:27:39 +0200 Subject: [PATCH] Make --> optional in markdown command line Don't enforce the end comment characters --> in markdowns command line. This closes #2. The new implementation however, enforce that > is not used in neither the command nor the args. --- doxec/__init__.py | 12 +++++++++--- doxec/tests/markdown.py | 12 +++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/doxec/__init__.py b/doxec/__init__.py index 6c8c677..ea23cfc 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 771f845..6e31b87 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) -- GitLab