Skip to content

Commit

Permalink
Merge pull request #399 from lostenderman/fix-tex-backslash-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jan 31, 2024
2 parents b113333 + 0ac80c5 commit fb24af4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Docker:

- Uninstall the distribution Markdown package. (258a73d4)

Fixes:

- Make Pandoc syntax extensions `tex_single_backslash_math` and
`tex_double_backslash_math` robust against extra spaces at the beginning /
end of inline / display math. (#386, #399, contributed by @lostenderman)

## 3.3.0 (2023-12-30)

Development:
Expand Down
22 changes: 18 additions & 4 deletions markdown.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -31603,8 +31603,16 @@ M.extensions.tex_math = function(tex_math_dollars,
return (starter * Cs(p * (p - ender)^0) * ender)
end

local function strip_preceding_whitespaces(str)
return str:gsub("^%s*(.-)$", "%1")
end

local allowed_before_closing = B( parsers.backslash * parsers.any
+ parsers.any * (parsers.nonspacechar - parsers.backslash))
+ parsers.any * (parsers.any - parsers.backslash))

local allowed_before_closing_no_space = B( parsers.backslash * parsers.any
+ parsers.any * (parsers.nonspacechar - parsers.backslash))

% \end{macrocode}
% \begin{markdown}
%
Expand All @@ -31621,7 +31629,7 @@ M.extensions.tex_math = function(tex_math_dollars,
local inline_math_opening_dollars = parsers.dollar
* #(parsers.nonspacechar)

local inline_math_closing_dollars = allowed_before_closing
local inline_math_closing_dollars = allowed_before_closing_no_space
* parsers.dollar
* -#(parsers.digit)

Expand Down Expand Up @@ -31660,29 +31668,32 @@ M.extensions.tex_math = function(tex_math_dollars,
local inline_math_opening_double = parsers.backslash
* parsers.backslash
* parsers.lparent
* #(parsers.nonspacechar)

local inline_math_closing_double = allowed_before_closing
* parsers.spacechar^0
* parsers.backslash
* parsers.backslash
* parsers.rparent

local inline_math_double = between(Cs( backslash_math_content),
inline_math_opening_double,
inline_math_closing_double)
/ strip_preceding_whitespaces

local display_math_opening_double = parsers.backslash
* parsers.backslash
* parsers.lbracket

local display_math_closing_double = allowed_before_closing
* parsers.spacechar^0
* parsers.backslash
* parsers.backslash
* parsers.rbracket

local display_math_double = between(Cs( backslash_math_content),
display_math_opening_double,
display_math_closing_double)
/ strip_preceding_whitespaces
% \end{macrocode}
% \begin{markdown}
%
Expand All @@ -31693,26 +31704,29 @@ M.extensions.tex_math = function(tex_math_dollars,
% \begin{macrocode}
local inline_math_opening_single = parsers.backslash
* parsers.lparent
* #(parsers.nonspacechar)

local inline_math_closing_single = allowed_before_closing
* parsers.spacechar^0
* parsers.backslash
* parsers.rparent

local inline_math_single = between(Cs( backslash_math_content),
inline_math_opening_single,
inline_math_closing_single)
/ strip_preceding_whitespaces

local display_math_opening_single = parsers.backslash
* parsers.lbracket

local display_math_closing_single = allowed_before_closing
* parsers.spacechar^0
* parsers.backslash
* parsers.rbracket

local display_math_single = between(Cs( backslash_math_content),
display_math_opening_single,
display_math_closing_single)
/ strip_preceding_whitespaces

local display_math = parsers.fail

Expand Down
37 changes: 29 additions & 8 deletions tests/testfiles/lunamark-markdown/tex-math-double-backslash.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,29 @@ c\\)

c\\]

No spaces are allowed at the beginning or the end of inline math:

\\( \infty \\)

Escaped spaces are allowed at the beginning or the end of inline math:

\\(\ \\)

The following is not inline math:

\\\(a\\\)

Whitespace characters are allowed at the beginning or the end of inline math or display math:

\\( 1+2=3 \\)

\\( 1+2=3 \\)

\\[ 1+2=3 \\]

\\[ 1+2=3 \\]

Whitespace-only content is allowed:

\\( \\)

\\[ \\]
>>>
BEGIN document
codeSpan: texMathDoubleBackslash
Expand All @@ -68,14 +80,23 @@ paragraphSeparator
backslash
paragraphSeparator
paragraphSeparator
backslash
inlineMath: \
paragraphSeparator
paragraphSeparator
backslash
backslash
paragraphSeparator
paragraphSeparator
inlineMath: \
inlineMath: 1+2=3
paragraphSeparator
inlineMath: 1+2=3
paragraphSeparator
backslash
backslash
displayMath: 1+2=3
paragraphSeparator
displayMath: 1+2=3
paragraphSeparator
paragraphSeparator
inlineMath:
paragraphSeparator
displayMath:
END document
37 changes: 30 additions & 7 deletions tests/testfiles/lunamark-markdown/tex-math-single-backslash.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,29 @@ c\)

c\]

No spaces are allowed at the beginning or the end of inline math:

\( \infty \)

Escaped spaces are allowed at the beginning or the end of inline math:

\(\ \)

The following is inline math with two backslashes:

\\\(a\\\)

Whitespace characters are allowed at the beginning or the end of inline math or display math:

\( 1+2=3 \)

\( 1+2=3 \)

\[ 1+2=3 \]

\[ 1+2=3 \]

Whitespace-only content is allowed:

\( \)

\[ \]
>>>
BEGIN document
codeSpan: texMathSingleBackslash
Expand All @@ -60,12 +72,23 @@ paragraphSeparator
paragraphSeparator
paragraphSeparator
paragraphSeparator
backslash
paragraphSeparator
paragraphSeparator
inlineMath: \
paragraphSeparator
paragraphSeparator
backslash
inlineMath: a\\
paragraphSeparator
paragraphSeparator
inlineMath: 1+2=3
paragraphSeparator
inlineMath: 1+2=3
paragraphSeparator
displayMath: 1+2=3
paragraphSeparator
displayMath: 1+2=3
paragraphSeparator
paragraphSeparator
inlineMath:
paragraphSeparator
displayMath:
END document

0 comments on commit fb24af4

Please sign in to comment.