git.iko.soy/typeable/pandoc
on master help sign in
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.

commitba7868765acae0071f684797771d83f9cc31b402 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 22 Jun 2016, 11:45 (10 years ago) parentb06d6c1
1 file changed+41−12
src/Text/Pandoc/Writers/HTML.hs+41−12
src/Text/Pandoc/Writers/HTML.hs+41−12
@@ -483,11 +483,9 @@ blockToHtml opts (Div attr@(ident, classes, kvs) bs) = do
483483 else addAttrs opts (ident, classes', kvs) $ divtag $ contents'
484484blockToHtml opts (RawBlock f str)
485485 | f == Format "html" = return $ preEscapedString str
486− | f == Format "latex" =
487− case writerHTMLMathMethod opts of
488− MathJax _ -> do modify (\st -> st{ stMath = True })
489− return $ toHtml str
490− _ -> return mempty
486+ | (f == Format "latex" || f == Format "tex") &&
487+ allowsMathEnvironments (writerHTMLMathMethod opts) &&
488+ isMathEnvironment str = blockToHtml opts $ Plain [Math DisplayMath str]
491489 | otherwise = return mempty
492490blockToHtml opts (HorizontalRule) = return $ if writerHtml5 opts then H5.hr else H.hr
493491blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
@@ -811,13 +809,6 @@ inlineToHtml opts inline =
811809 InlineMath -> m
812810 DisplayMath -> brtag >> m >> brtag
813811 (RawInline f str)
814− | f == Format "latex" ->
815− case writerHTMLMathMethod opts of
816− LaTeXMathML _ -> do modify (\st -> st {stMath = True})
817− return $ toHtml str
818− MathJax _ -> do modify (\st -> st {stMath = True})
819− return $ toHtml str
820− _ -> return mempty
821812 | f == Format "html" -> return $ preEscapedString str
822813 | otherwise -> return mempty
823814 (Link attr txt (s,_)) | "mailto:" `isPrefixOf` s -> do
@@ -915,3 +906,41 @@ renderKaTeX = unlines [
915906 , " katex.render(texText.data, mathElements[i])"
916907 , "}}"
917908 ]
909+
910+isMathEnvironment :: String -> Bool
911+isMathEnvironment s = "\\begin{" `isPrefixOf` s &&
912+ envName `elem` mathmlenvs
913+ where envName = takeWhile (/= '}') (drop 7 s)
914+ mathmlenvs = [ "align"
915+ , "align*"
916+ , "alignat"
917+ , "alignat*"
918+ , "aligned"
919+ , "alignedat"
920+ , "array"
921+ , "Bmatrix"
922+ , "bmatrix"
923+ , "cases"
924+ , "CD"
925+ , "eqnarray"
926+ , "eqnarray*"
927+ , "equation"
928+ , "equation*"
929+ , "gather"
930+ , "gather*"
931+ , "gathered"
932+ , "matrix"
933+ , "multline"
934+ , "multline*"
935+ , "pmatrix"
936+ , "smallmatrix"
937+ , "split"
938+ , "subarray"
939+ , "Vmatrix"
940+ , "vmatrix" ]
941+
942+allowsMathEnvironments :: HTMLMathMethod -> Bool
943+allowsMathEnvironments (MathJax _) = True
944+allowsMathEnvironments (MathML _) = True
945+allowsMathEnvironments (WebTeX _) = True
946+allowsMathEnvironments _ = False