Skip to content

Commit

Permalink
HTML writer: Better support for raw LaTeX environments.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jgm committed Jun 22, 2016
1 parent b06d6c1 commit ba78687
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit ba78687

Please sign in to comment.