From 094d58ccea3ba6ee2b47c610bd8f470937b1600f Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 29 Jun 2022 15:35:02 +0100 Subject: [PATCH 1/9] port executablebooks/markdown-it-dollarmath#8 --- mdit_py_plugins/dollarmath/index.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 5fe0381..4b64478 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -302,15 +302,14 @@ def _math_block_dollar( start = state.bMarks[nextLine] + state.tShift[nextLine] end = state.eMarks[nextLine] - if end - start < 2: - continue - lineText = state.src[start:end] if lineText.strip().endswith("$$"): haveEndMarker = True end = end - 2 - (len(lineText) - len(lineText.strip())) break + if lineText.strip() == "": + break # blank lines are not allowed within $$ # reverse the line and match if allow_labels: From 1879f1794c42174ef9128db0a25a28c4286049f2 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Wed, 29 Jun 2022 15:37:26 +0100 Subject: [PATCH 2/9] Copy across the tests from upstream --- tests/fixtures/dollar_math.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/fixtures/dollar_math.md b/tests/fixtures/dollar_math.md index a1c2d43..a11cffb 100644 --- a/tests/fixtures/dollar_math.md +++ b/tests/fixtures/dollar_math.md @@ -226,6 +226,18 @@ b = 2 . +display equation with blank lines. (valid=False) +. +$$ +1+1=2 + +$$ +. +

$$ +1+1=2

+

$$

+. + equation followed by a labelled equation (valid=True) . $$ @@ -540,13 +552,3 @@ i.e., $[\alpha \bar{X}, \infty)$ is a lower 1-sided $1-\alpha$ confidence bound
\mathbb P (\alpha \bar{X} \ge \mu) \le \alpha;
i.e., [\alpha \bar{X}, \infty) is a lower 1-sided 1-\alpha confidence bound for \mu.

. - -display equation with label containing whitespace. (valid=True) -. -$$1+1=2$$ (a b) -. -
- -1+1=2 -
-. From 3318fb18156263edc43636cad075703aa4b13977 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Mon, 4 Jul 2022 11:12:09 +0100 Subject: [PATCH 3/9] Revert removal --- tests/fixtures/dollar_math.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/fixtures/dollar_math.md b/tests/fixtures/dollar_math.md index a11cffb..ffeef83 100644 --- a/tests/fixtures/dollar_math.md +++ b/tests/fixtures/dollar_math.md @@ -552,3 +552,14 @@ i.e., $[\alpha \bar{X}, \infty)$ is a lower 1-sided $1-\alpha$ confidence bound
\mathbb P (\alpha \bar{X} \ge \mu) \le \alpha;
i.e., [\alpha \bar{X}, \infty) is a lower 1-sided 1-\alpha confidence bound for \mu.

. + +display equation with label containing whitespace. (valid=True) +. +$$1+1=2$$ (a b) +. +
+ +1+1=2 +
+. + From f76fe8cde66e719803536c2a3f9b287b2aa0af65 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Mon, 4 Jul 2022 11:12:41 +0100 Subject: [PATCH 4/9] Remove stray extra line --- tests/fixtures/dollar_math.md | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/fixtures/dollar_math.md b/tests/fixtures/dollar_math.md index ffeef83..c6554ec 100644 --- a/tests/fixtures/dollar_math.md +++ b/tests/fixtures/dollar_math.md @@ -562,4 +562,3 @@ $$1+1=2$$ (a b) 1+1=2 . - From e74277d97f3ea0913729ba765db7407a5e95e845 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Fri, 3 Mar 2023 22:32:37 +0000 Subject: [PATCH 5/9] Add a feature flag to restore the old behavior --- mdit_py_plugins/dollarmath/index.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 4b64478..3d21e13 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -13,6 +13,7 @@ def dollarmath_plugin( allow_labels: bool = True, allow_space: bool = True, allow_digits: bool = True, + allow_blank_lines: bool = False, double_inline: bool = False, label_normalizer: Optional[Callable[[str], str]] = None, renderer: Optional[Callable[[str, Dict[str, Any]], str]] = None, @@ -30,6 +31,9 @@ def dollarmath_plugin( :param allow_digits: Parse inline math when there is a digit before/after the opening/closing ``$``, e.g. ``1$`` or ``$2``. This is useful when also using currency. + :param allow_blank_lines: Allow blank lines inside ``$$``. Note that blank lines are + not allowed in LaTeX nor executablebooks/markdown-it-dollarmath, but have special + semantics if used within Sphinx `..math` admonitions. :param double_inline: Search for double-dollar math within inline contexts :param label_normalizer: Function to normalize the label, by default replaces whitespace with `-` @@ -47,7 +51,7 @@ def dollarmath_plugin( math_inline_dollar(allow_space, allow_digits, double_inline), ) md.block.ruler.before( - "fence", "math_block", math_block_dollar(allow_labels, label_normalizer) + "fence", "math_block", math_block_dollar(allow_labels, label_normalizer, allow_blank_lines) ) # TODO the current render rules are really just for testing @@ -247,6 +251,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool: def math_block_dollar( allow_labels: bool = True, label_normalizer: Optional[Callable[[str], str]] = None, + allow_blank_lines: bool = False, ) -> Callable[[StateBlock, int, int, bool], bool]: """Generate block dollar rule.""" @@ -308,7 +313,7 @@ def _math_block_dollar( haveEndMarker = True end = end - 2 - (len(lineText) - len(lineText.strip())) break - if lineText.strip() == "": + if lineText.strip() == "" and not allow_blank_lines: break # blank lines are not allowed within $$ # reverse the line and match From 3d8489aa1faf133b1cb40b00ab2601813b8c36c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 22:33:03 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mdit_py_plugins/dollarmath/index.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 3d21e13..14edb5a 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -51,7 +51,9 @@ def dollarmath_plugin( math_inline_dollar(allow_space, allow_digits, double_inline), ) md.block.ruler.before( - "fence", "math_block", math_block_dollar(allow_labels, label_normalizer, allow_blank_lines) + "fence", + "math_block", + math_block_dollar(allow_labels, label_normalizer, allow_blank_lines), ) # TODO the current render rules are really just for testing From 4312f8172a274b31678005c29321a5ec77396e7d Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Fri, 3 Mar 2023 22:36:27 +0000 Subject: [PATCH 7/9] Update index.py --- mdit_py_plugins/dollarmath/index.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mdit_py_plugins/dollarmath/index.py b/mdit_py_plugins/dollarmath/index.py index 14edb5a..51cd3f4 100644 --- a/mdit_py_plugins/dollarmath/index.py +++ b/mdit_py_plugins/dollarmath/index.py @@ -13,7 +13,7 @@ def dollarmath_plugin( allow_labels: bool = True, allow_space: bool = True, allow_digits: bool = True, - allow_blank_lines: bool = False, + allow_blank_lines: bool = True, double_inline: bool = False, label_normalizer: Optional[Callable[[str], str]] = None, renderer: Optional[Callable[[str, Dict[str, Any]], str]] = None, @@ -32,8 +32,9 @@ def dollarmath_plugin( before/after the opening/closing ``$``, e.g. ``1$`` or ``$2``. This is useful when also using currency. :param allow_blank_lines: Allow blank lines inside ``$$``. Note that blank lines are - not allowed in LaTeX nor executablebooks/markdown-it-dollarmath, but have special - semantics if used within Sphinx `..math` admonitions. + not allowed in LaTeX, executablebooks/markdown-it-dollarmath, or the Github or + StackExchange markdown dialects. Hoever, they have special semantics if used + within Sphinx `..math` admonitions, so are allowed for backwards-compatibility. :param double_inline: Search for double-dollar math within inline contexts :param label_normalizer: Function to normalize the label, by default replaces whitespace with `-` From 6dcc8040ae6f317b992a14c4bd5f3c2d87b5d313 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Fri, 3 Mar 2023 23:28:18 +0000 Subject: [PATCH 8/9] Update tests --- tests/test_dollarmath.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_dollarmath.py b/tests/test_dollarmath.py index 1cb1b12..ef09ae3 100644 --- a/tests/test_dollarmath.py +++ b/tests/test_dollarmath.py @@ -88,7 +88,8 @@ def test_plugin_parse(data_regression): ) def test_dollarmath_fixtures(line, title, input, expected): md = MarkdownIt("commonmark").use( - dollarmath_plugin, allow_space=False, allow_digits=False, double_inline=True + dollarmath_plugin, allow_space=False, allow_digits=False, double_inline=True, + allow_blank_lines=False, ) md.options.xhtmlOut = False text = md.render(input) From 65d763a6d38a047662aa87ab8e965a13f6dd624f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 23:29:30 +0000 Subject: [PATCH 9/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_dollarmath.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_dollarmath.py b/tests/test_dollarmath.py index ef09ae3..6b2c71b 100644 --- a/tests/test_dollarmath.py +++ b/tests/test_dollarmath.py @@ -88,7 +88,10 @@ def test_plugin_parse(data_regression): ) def test_dollarmath_fixtures(line, title, input, expected): md = MarkdownIt("commonmark").use( - dollarmath_plugin, allow_space=False, allow_digits=False, double_inline=True, + dollarmath_plugin, + allow_space=False, + allow_digits=False, + double_inline=True, allow_blank_lines=False, ) md.options.xhtmlOut = False