git.iko.soy/typeable/pandoc
on master help sign in
HTML Writer: MathML now outputted with tex annotation.

Closes #1635

commit575c76e36bba6ab6dab5b6e68cffb66a1842d460 authormpickering <matthewtpickering@gmail.com> dateThu 25 Sep 2014, 15:28 (12 years ago) parentc0b9ad4
1 file changed+15−1
src/Text/Pandoc/Writers/HTML.hs+15−1
src/Text/Pandoc/Writers/HTML.hs+15−1
@@ -60,6 +60,8 @@ import qualified Text.Blaze.XHtml1.Transitional.Attributes as A
6060import Text.Blaze.Renderer.String (renderHtml)
6161import Text.TeXMath
6262import Text.XML.Light.Output
63+import Text.XML.Light (unode, elChildren, add_attr, unqual)
64+import qualified Text.XML.Light as XML
6365import System.FilePath (takeExtension)
6466import Data.Monoid
6567import Data.Aeson (Value)
@@ -615,6 +617,18 @@ inlineListToHtml :: WriterOptions -> [Inline] -> State WriterState Html
615617inlineListToHtml opts lst =
616618 mapM (inlineToHtml opts) lst >>= return . mconcat
617619
620+-- | Annotates a MathML expression with the tex source
621+annotateMML :: XML.Element -> String -> XML.Element
622+annotateMML e tex = math (unode "semantics" [cs, unode "annotation" (annotAttrs, tex)])
623+ where
624+ cs = case elChildren e of
625+ [] -> unode "mrow" ()
626+ [x] -> x
627+ xs -> unode "mrow" xs
628+ math = add_attr (XML.Attr (unqual "xmlns") "http://www.w3.org/1998/Math/MathML") . unode "math"
629+ annotAttrs = [XML.Attr (unqual "encoding") "application/x-tex"]
630+
631+
618632-- | Convert Pandoc inline element to HTML.
619633inlineToHtml :: WriterOptions -> Inline -> State WriterState Html
620634inlineToHtml opts inline =
@@ -706,7 +720,7 @@ inlineToHtml opts inline =
706720 defaultConfigPP
707721 case writeMathML dt <$> readTeX str of
708722 Right r -> return $ preEscapedString $
709− ppcElement conf r
723+ ppcElement conf (annotateMML r str)
710724 Left _ -> inlineListToHtml opts
711725 (texMathToInlines t str) >>=
712726 return . (H.span ! A.class_ "math")