Skip to content

Commit

Permalink
dollarmath: Add allow_blank_lines option (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-wieser authored Mar 6, 2023
1 parent be14587 commit 061a544
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
15 changes: 11 additions & 4 deletions mdit_py_plugins/dollarmath/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def dollarmath_plugin(
allow_labels: bool = True,
allow_space: bool = True,
allow_digits: bool = True,
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,
Expand All @@ -30,6 +31,10 @@ 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, 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 `-`
Expand All @@ -47,7 +52,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)
"fence",
"math_block",
math_block_dollar(allow_labels, label_normalizer, allow_blank_lines),
)

# TODO the current render rules are really just for testing
Expand Down Expand Up @@ -246,6 +253,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."""

Expand Down Expand Up @@ -299,15 +307,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() == "" and not allow_blank_lines:
break # blank lines are not allowed within $$

# reverse the line and match
if allow_labels:
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/dollar_math.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ b = 2
</div>
.

display equation with blank lines. (valid=False)
.
$$
1+1=2
$$
.
<p>$$
1+1=2</p>
<p>$$</p>
.

equation followed by a labelled equation (valid=True)
.
$$
Expand Down
6 changes: 5 additions & 1 deletion tests/test_dollarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ def test_custom_renderer(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)
Expand Down

0 comments on commit 061a544

Please sign in to comment.