From ba7868765acae0071f684797771d83f9cc31b402 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 22 Jun 2016 11:45:45 -0700 Subject: HTML writer: Better support for raw LaTeX environments. Previously we just passed all raw TeX through when MathJax was used for HTML math. This passed through too much. With this patch, only raw LaTeX environments that MathJax can handle get passed through. This patch also causes raw LaTeX environments to be treated as math, when possible, with MathML and WebTeX output. Closes #2758. --- src/Text/Pandoc/Writers/HTML.hs | 53 +++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-) (limited to 'src/Text/Pandoc/Writers/HTML.hs') diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index d8b8384e7..433e28bf2 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -483,11 +483,9 @@ blockToHtml opts (Div attr@(ident, classes, kvs) bs) = do else addAttrs opts (ident, classes', kvs) $ divtag $ contents' blockToHtml opts (RawBlock f str) | f == Format "html" = return $ preEscapedString str - | f == Format "latex" = - case writerHTMLMathMethod opts of - MathJax _ -> do modify (\st -> st{ stMath = True }) - return $ toHtml str - _ -> return mempty + | (f == Format "latex" || f == Format "tex") && + allowsMathEnvironments (writerHTMLMathMethod opts) && + isMathEnvironment str = blockToHtml opts $ Plain [Math DisplayMath str] | otherwise = return mempty blockToHtml opts (HorizontalRule) = return $ if writerHtml5 opts then H5.hr else H.hr blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do @@ -811,13 +809,6 @@ inlineToHtml opts inline = InlineMath -> m DisplayMath -> brtag >> m >> brtag (RawInline f str) - | f == Format "latex" -> - case writerHTMLMathMethod opts of - LaTeXMathML _ -> do modify (\st -> st {stMath = True}) - return $ toHtml str - MathJax _ -> do modify (\st -> st {stMath = True}) - return $ toHtml str - _ -> return mempty | f == Format "html" -> return $ preEscapedString str | otherwise -> return mempty (Link attr txt (s,_)) | "mailto:" `isPrefixOf` s -> do @@ -915,3 +906,41 @@ renderKaTeX = unlines [ , " katex.render(texText.data, mathElements[i])" , "}}" ] + +isMathEnvironment :: String -> Bool +isMathEnvironment s = "\\begin{" `isPrefixOf` s && + envName `elem` mathmlenvs + where envName = takeWhile (/= '}') (drop 7 s) + mathmlenvs = [ "align" + , "align*" + , "alignat" + , "alignat*" + , "aligned" + , "alignedat" + , "array" + , "Bmatrix" + , "bmatrix" + , "cases" + , "CD" + , "eqnarray" + , "eqnarray*" + , "equation" + , "equation*" + , "gather" + , "gather*" + , "gathered" + , "matrix" + , "multline" + , "multline*" + , "pmatrix" + , "smallmatrix" + , "split" + , "subarray" + , "Vmatrix" + , "vmatrix" ] + +allowsMathEnvironments :: HTMLMathMethod -> Bool +allowsMathEnvironments (MathJax _) = True +allowsMathEnvironments (MathML _) = True +allowsMathEnvironments (WebTeX _) = True +allowsMathEnvironments _ = False -- cgit v1.2.3