{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Writers.Math
( texMathToInlines
, convertMath
, defaultMathJaxURL
, defaultKaTeXURL
)
where
import qualified Data.Text as T
import Text.Pandoc.Class.PandocMonad
import Text.Pandoc.Definition
import Text.Pandoc.Logging
import Text.TeXMath (DisplayType (..), Exp, readTeX, writePandoc)
import Text.Pandoc.Options (defaultMathJaxURL, defaultKaTeXURL)
texMathToInlines :: PandocMonad m
=> MathType
-> T.Text
-> m [Inline]
texMathToInlines :: forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> m [Inline]
texMathToInlines MathType
mt Text
inp = do
Either Inline (Maybe [Inline])
res <- forall (m :: * -> *) a.
PandocMonad m =>
(DisplayType -> [Exp] -> a)
-> MathType -> Text -> m (Either Inline a)
convertMath DisplayType -> [Exp] -> Maybe [Inline]
writePandoc MathType
mt Text
inp
case Either Inline (Maybe [Inline])
res of
Right (Just [Inline]
ils) -> forall (m :: * -> *) a. Monad m => a -> m a
return [Inline]
ils
Right Maybe [Inline]
Nothing -> do
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report forall a b. (a -> b) -> a -> b
$ Text -> Text -> LogMessage
CouldNotConvertTeXMath Text
inp Text
""
forall (m :: * -> *) a. Monad m => a -> m a
return [MathType -> Text -> Inline
mkFallback MathType
mt Text
inp]
Left Inline
il -> forall (m :: * -> *) a. Monad m => a -> m a
return [Inline
il]
mkFallback :: MathType -> T.Text -> Inline
mkFallback :: MathType -> Text -> Inline
mkFallback MathType
mt Text
str = Text -> Inline
Str (Text
delim forall a. Semigroup a => a -> a -> a
<> Text
str forall a. Semigroup a => a -> a -> a
<> Text
delim)
where delim :: Text
delim = case MathType
mt of
MathType
DisplayMath -> Text
"$$"
MathType
InlineMath -> Text
"$"
convertMath :: PandocMonad m
=> (DisplayType -> [Exp] -> a) -> MathType -> T.Text
-> m (Either Inline a)
convertMath :: forall (m :: * -> *) a.
PandocMonad m =>
(DisplayType -> [Exp] -> a)
-> MathType -> Text -> m (Either Inline a)
convertMath DisplayType -> [Exp] -> a
writer MathType
mt Text
str =
case DisplayType -> [Exp] -> a
writer DisplayType
dt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Either Text [Exp]
readTeX Text
str of
Right a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right a
r)
Left Text
e -> do
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report forall a b. (a -> b) -> a -> b
$ Text -> Text -> LogMessage
CouldNotConvertTeXMath Text
str Text
e
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ MathType -> Text -> Inline
mkFallback MathType
mt Text
str)
where dt :: DisplayType
dt = case MathType
mt of
MathType
DisplayMath -> DisplayType
DisplayBlock
MathType
InlineMath -> DisplayType
DisplayInline