aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers/Math.hs
blob: b959ce972ee856634c044d9798a467d62f1e64ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Text.Pandoc.Writers.Math
  ( texMathToInlines
  , convertMath
  )
where

import Text.Pandoc.Class
import Text.Pandoc.Definition
import Text.TeXMath (Exp, writePandoc, DisplayType(..), readTeX)

-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
-- Defaults to raw formula between @$@ or @$$@ characters if entire formula
-- can't be converted.
texMathToInlines :: PandocMonad m
                 => MathType
                 -> String    -- ^ String to parse (assumes @'\n'@ line endings)
                 -> m [Inline]
texMathToInlines mt inp = do
  res <- convertMath writePandoc mt inp
  case res of
       Right (Just ils)  -> return ils
       Right (Nothing)   -> do
         warning $ "Could not render TeX math as unicode, rendering as raw TeX:\n" ++ inp
         return [mkFallback mt inp]
       Left il           -> return [il]

mkFallback :: MathType -> String -> Inline
mkFallback mt str = Str (delim ++ str ++ delim)
   where delim = case mt of
                      DisplayMath -> "$$"
                      InlineMath  -> "$"

-- | Converts a raw TeX math formula using a writer function,
-- issuing a warning and producing a fallback (a raw string)
-- on failure.
convertMath :: PandocMonad m
            => (DisplayType -> [Exp] -> a) -> MathType -> String
            -> m (Either Inline a)
convertMath writer mt str = do
  case writer dt <$> readTeX str of
       Right r  -> return (Right r)
       Left e   -> do
         warning $ "Could not convert TeX math, rendering as raw TeX:\n" ++
                 str ++ "\n" ++ e
         return (Left $ mkFallback mt str)
   where dt = case mt of
                   DisplayMath -> DisplayBlock
                   InlineMath  -> DisplayInline