From b9ac243986c1dcfb8915c0e4c167ddb0df78aa49 Mon Sep 17 00:00:00 2001 From: John MacFarlane <jgm@berkeley.edu> Date: Thu, 3 Feb 2022 13:21:27 -0800 Subject: [PATCH] Trim whitespace from math in --webtex. This fixes problems with --webtex and markdown output, when display math starts or ends with a newline. Closes #7892. --- src/Text/Pandoc/Writers/HTML.hs | 8 +++++--- src/Text/Pandoc/Writers/Markdown/Inline.hs | 14 +++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index b1161fded..79d9b12e7 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -1465,13 +1465,15 @@ inlineToHtml opts inline = do case writerHTMLMathMethod opts of WebTeX url -> do let imtag = if html5 then H5.img else H.img + let str' = T.strip str let s = case t of InlineMath -> "\\textstyle " DisplayMath -> "\\displaystyle " return $ imtag ! A.style "vertical-align:middle" - ! A.src (toValue . (url <>) . urlEncode $ s <> str) - ! A.alt (toValue str) - ! A.title (toValue str) + ! A.src (toValue . (url <>) . + urlEncode $ s <> str') + ! A.alt (toValue str') + ! A.title (toValue str') ! A.class_ mathClass GladTeX -> return $ diff --git a/src/Text/Pandoc/Writers/Markdown/Inline.hs b/src/Text/Pandoc/Writers/Markdown/Inline.hs index 929912f99..1ae7e1847 100644 --- a/src/Text/Pandoc/Writers/Markdown/Inline.hs +++ b/src/Text/Pandoc/Writers/Markdown/Inline.hs @@ -485,8 +485,10 @@ inlineToMarkdown opts (Math InlineMath str) = do case () of _ | variant == Markua -> return $ "`" <> literal str <> "`" <> "$" | otherwise -> case writerHTMLMathMethod opts of - WebTeX url -> inlineToMarkdown opts - (Image nullAttr [Str str] (url <> urlEncode str, str)) + WebTeX url -> + let str' = T.strip str + in inlineToMarkdown opts + (Image nullAttr [Str str'] (url <> urlEncode str', str')) _ | isEnabled Ext_tex_math_dollars opts -> return $ "$" <> literal str <> "$" | isEnabled Ext_tex_math_single_backslash opts -> @@ -507,9 +509,11 @@ inlineToMarkdown opts (Math DisplayMath str) = do return $ blankline <> attributes <> cr <> literal "```" <> cr <> literal str <> cr <> literal "```" <> blankline | otherwise -> case writerHTMLMathMethod opts of - WebTeX url -> (\x -> blankline <> x <> blankline) `fmap` - inlineToMarkdown opts (Image nullAttr [Str str] - (url <> urlEncode str, str)) + WebTeX url -> + let str' = T.strip str + in (\x -> blankline <> x <> blankline) `fmap` + inlineToMarkdown opts (Image nullAttr [Str str'] + (url <> urlEncode str', str')) _ | isEnabled Ext_tex_math_dollars opts -> return $ "$$" <> literal str <> "$$" | isEnabled Ext_tex_math_single_backslash opts ->