{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Text.Pandoc.Readers.Typst.Math
  ( pMathMany
  )
where

import Control.Monad (MonadPlus (mplus))
import Data.Char (isAlphaNum, isDigit)
import Data.List (intercalate)
import qualified Data.Map as M
import Data.Maybe (fromMaybe)
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Vector as V
import Text.Pandoc.Parsing ( many )
import Text.Pandoc.Class ( PandocMonad )
import Text.TeXMath.Types
  ( Alignment (..),
    Exp (..),
    FractionType (..),
    TeXSymbolType (..),
    TextType (..),
  )
import Text.TeXMath.Unicode.ToTeX (getSymbolType)
import Text.Pandoc.Readers.Typst.Parsing
    ( P, pTok, ignored, pWithContents, getField, chunks )
import Typst.Types

withGroup :: [Exp] -> Exp
withGroup :: [Exp] -> Exp
withGroup [Item [Exp]
x] = Item [Exp]
Exp
x
withGroup [Exp]
xs = [Exp] -> Exp
EGrouped [Exp]
xs

data AttachmentStyle = Limits | LimitsDisplay | Scripts
  deriving (AttachmentStyle -> AttachmentStyle -> Bool
(AttachmentStyle -> AttachmentStyle -> Bool)
-> (AttachmentStyle -> AttachmentStyle -> Bool)
-> Eq AttachmentStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AttachmentStyle -> AttachmentStyle -> Bool
== :: AttachmentStyle -> AttachmentStyle -> Bool
$c/= :: AttachmentStyle -> AttachmentStyle -> Bool
/= :: AttachmentStyle -> AttachmentStyle -> Bool
Eq, Int -> AttachmentStyle -> ShowS
[AttachmentStyle] -> ShowS
AttachmentStyle -> String
(Int -> AttachmentStyle -> ShowS)
-> (AttachmentStyle -> String)
-> ([AttachmentStyle] -> ShowS)
-> Show AttachmentStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AttachmentStyle -> ShowS
showsPrec :: Int -> AttachmentStyle -> ShowS
$cshow :: AttachmentStyle -> String
show :: AttachmentStyle -> String
$cshowList :: [AttachmentStyle] -> ShowS
showList :: [AttachmentStyle] -> ShowS
Show)

getAttachmentStyle :: PandocMonad m => M.Map Identifier Val -> P m (Maybe AttachmentStyle)
getAttachmentStyle :: forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Maybe AttachmentStyle)
getAttachmentStyle Map Identifier Val
fields = do
  (Seq Content
base :: Seq Content) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"base" Map Identifier Val
fields
  case Seq Content
base of
    [Elt Identifier
"math.op" Maybe SourcePos
_ Map Identifier Val
fs] -> do
      Val
limits <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"limits" Map Identifier Val
fs
      if Val
limits Val -> Val -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Val
VBoolean Bool
True
         then Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe AttachmentStyle -> P m (Maybe AttachmentStyle))
-> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a b. (a -> b) -> a -> b
$ AttachmentStyle -> Maybe AttachmentStyle
forall a. a -> Maybe a
Just AttachmentStyle
Limits
         else Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe AttachmentStyle
forall a. Maybe a
Nothing
    [Elt Identifier
"math.limits" Maybe SourcePos
_ Map Identifier Val
fs] -> do
      Val
inl <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"inline" Map Identifier Val
fs
      if Val
inl Val -> Val -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Val
VBoolean Bool
False
         then Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe AttachmentStyle -> P m (Maybe AttachmentStyle))
-> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a b. (a -> b) -> a -> b
$ AttachmentStyle -> Maybe AttachmentStyle
forall a. a -> Maybe a
Just AttachmentStyle
LimitsDisplay
         else Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe AttachmentStyle -> P m (Maybe AttachmentStyle))
-> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a b. (a -> b) -> a -> b
$ AttachmentStyle -> Maybe AttachmentStyle
forall a. a -> Maybe a
Just AttachmentStyle
Limits
    [Elt Identifier
"math.scripts" Maybe SourcePos
_ Map Identifier Val
_] -> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe AttachmentStyle -> P m (Maybe AttachmentStyle))
-> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a b. (a -> b) -> a -> b
$ AttachmentStyle -> Maybe AttachmentStyle
forall a. a -> Maybe a
Just AttachmentStyle
Scripts
    Seq Content
_ -> Maybe AttachmentStyle -> P m (Maybe AttachmentStyle)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe AttachmentStyle
forall a. Maybe a
Nothing

pMath :: PandocMonad m => P m Exp
pMath :: forall (m :: * -> *). PandocMonad m => P m Exp
pMath = (Content -> Bool) -> P m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok (Bool -> Content -> Bool
forall a b. a -> b -> a
const Bool
True) P m Content
-> (Content -> ParsecT [Content] PState m Exp)
-> ParsecT [Content] PState m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Content -> ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => Content -> P m Exp
handleMath

handleMath :: PandocMonad m => Content -> P m Exp
handleMath :: forall (m :: * -> *). PandocMonad m => Content -> P m Exp
handleMath Content
tok =
  case Content
tok of
    Lab Text
t -> do
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored (Text
"label " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t)
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Exp] -> Exp
EGrouped [])
    Txt Text
t
      | (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
isDigit Text
t -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Exp
ENumber Text
t
      | Text -> Int
T.length Text
t Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 ->
          case Text -> String
T.unpack Text
t of
            [Item String
c] | Bool -> Bool
not (Char -> Bool
isAlphaNum Char
Item String
c) -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ TeXSymbolType -> Text -> Exp
ESymbol (Char -> TeXSymbolType
getSymbolType Char
Item String
c) Text
t
            String
_ -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Exp
EIdentifier Text
t
      | Bool
otherwise -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ TextType -> Text -> Exp
EText TextType
TextNormal Text
t
    Elt Identifier
"math.dif" Maybe SourcePos
_ Map Identifier Val
_ -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Exp
EIdentifier Text
"d"
    Elt Identifier
"math.Dif" Maybe SourcePos
_ Map Identifier Val
_ -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Exp
EIdentifier Text
"D"
    Elt Identifier
"math.equation" Maybe SourcePos
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
    Elt Identifier
"text" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      (Maybe Text
mbweight :: Maybe Text) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Text)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"weight" Map Identifier Val
fields
      case Maybe Text
mbweight of
        Just Text
"bold" -> TextType -> [Exp] -> Exp
EStyled TextType
TextBold ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
body
        Maybe Text
_ -> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
body
    Elt Identifier
"math.op" Maybe SourcePos
_ Map Identifier Val
fields -> Text -> Exp
EMathOperator (Text -> Exp) -> ParsecT [Content] PState m Text -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"text" Map Identifier Val
fields
    Elt Identifier
"math.frac" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
num <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"num" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp
denom <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"denom" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ FractionType -> Exp -> Exp -> Exp
EFraction FractionType
NormalFrac Exp
num Exp
denom
    Elt Identifier
"math.accent" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
base <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"base" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp
acc <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"accent" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      let acc' :: Exp
acc' = case Exp
acc of
            ESymbol TeXSymbolType
_ Text
"\8901" -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
"\775" -- \dot
            ESymbol TeXSymbolType
_ Text
"\168" -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
"\776" -- \ddot
            ESymbol TeXSymbolType
_ Text
"\8764" -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
"\771" -- \tilde
            ESymbol TeXSymbolType
_ Text
t -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
t
            Exp
_ -> Exp
acc
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
base Exp
acc'
    Elt Identifier
"math.attach" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
base <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"base" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Maybe (Seq Content)
t' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"t" Map Identifier Val
fields
      Maybe (Seq Content)
b' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"b" Map Identifier Val
fields
      Maybe (Seq Content)
tr' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"tr" Map Identifier Val
fields
      Maybe (Seq Content)
tl' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"tl" Map Identifier Val
fields
      Maybe (Seq Content)
br' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"br" Map Identifier Val
fields
      Maybe (Seq Content)
bl' <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"bl" Map Identifier Val
fields
      Maybe AttachmentStyle
attachmentStyle <- Map Identifier Val -> P m (Maybe AttachmentStyle)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Maybe AttachmentStyle)
getAttachmentStyle Map Identifier Val
fields
      let limits :: Bool
limits = case Maybe AttachmentStyle
attachmentStyle of
                     Just AttachmentStyle
Limits -> Bool
True
                     Just AttachmentStyle
LimitsDisplay -> Bool
True
                     Maybe AttachmentStyle
_ -> Bool
False
      let convertible :: Bool
convertible = Maybe AttachmentStyle
attachmentStyle Maybe AttachmentStyle -> Maybe AttachmentStyle -> Bool
forall a. Eq a => a -> a -> Bool
== AttachmentStyle -> Maybe AttachmentStyle
forall a. a -> Maybe a
Just AttachmentStyle
LimitsDisplay
      let (Maybe (Seq Content)
mbt, Maybe (Seq Content)
mbtr) =
            case (Maybe (Seq Content)
t', Maybe (Seq Content)
tr') of
              (Just Seq Content
top, Just Seq Content
topright) -> (Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
top, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
topright)
              (Just Seq Content
top, Maybe (Seq Content)
Nothing)
                | Bool
limits -> (Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
top, Maybe (Seq Content)
forall a. Maybe a
Nothing)
                | Bool
otherwise -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
top)
              (Maybe (Seq Content)
Nothing, Just Seq Content
topright) -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
topright)
              (Maybe (Seq Content)
Nothing, Maybe (Seq Content)
Nothing) -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Maybe (Seq Content)
forall a. Maybe a
Nothing)
      let (Maybe (Seq Content)
mbb, Maybe (Seq Content)
mbbr) =
            case (Maybe (Seq Content)
b', Maybe (Seq Content)
br') of
              (Just Seq Content
bot, Just Seq Content
botright) -> (Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
bot, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
botright)
              (Just Seq Content
bot, Maybe (Seq Content)
Nothing)
                | Bool
limits -> (Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
bot, Maybe (Seq Content)
forall a. Maybe a
Nothing)
                | Bool
otherwise -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
bot)
              (Maybe (Seq Content)
Nothing, Just Seq Content
topright) -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Seq Content -> Maybe (Seq Content)
forall a. a -> Maybe a
Just Seq Content
topright)
              (Maybe (Seq Content)
Nothing, Maybe (Seq Content)
Nothing) -> (Maybe (Seq Content)
forall a. Maybe a
Nothing, Maybe (Seq Content)
forall a. Maybe a
Nothing)
      let dummy :: Exp
dummy = [Exp] -> Exp
EGrouped []
      let addPrefix :: Exp -> ParsecT [Content] PState m Exp
addPrefix Exp
x =
            case (Maybe (Seq Content)
tl', Maybe (Seq Content)
bl') of
              (Maybe (Seq Content)
Nothing, Maybe (Seq Content)
Nothing) -> Exp -> ParsecT [Content] PState m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
x
              (Just Seq Content
top, Maybe (Seq Content)
Nothing) -> do
                Exp
res <- Exp -> Exp -> Exp
ESuper Exp
dummy (Exp -> Exp)
-> ParsecT [Content] PState m Exp -> ParsecT [Content] PState m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
top
                Exp -> ParsecT [Content] PState m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> ParsecT [Content] PState m Exp)
-> Exp -> ParsecT [Content] PState m Exp
forall a b. (a -> b) -> a -> b
$ [Exp] -> Exp
EGrouped [Item [Exp]
Exp
res, Item [Exp]
Exp
x]
              (Maybe (Seq Content)
Nothing, Just Seq Content
bot) -> do
                Exp
res <- Exp -> Exp -> Exp
ESub Exp
dummy (Exp -> Exp)
-> ParsecT [Content] PState m Exp -> ParsecT [Content] PState m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
bot
                Exp -> ParsecT [Content] PState m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> ParsecT [Content] PState m Exp)
-> Exp -> ParsecT [Content] PState m Exp
forall a b. (a -> b) -> a -> b
$ [Exp] -> Exp
EGrouped [Item [Exp]
Exp
res, Item [Exp]
Exp
x]
              (Just Seq Content
top, Just Seq Content
bot) -> do
                Exp
res <- Exp -> Exp -> Exp -> Exp
ESubsup Exp
dummy (Exp -> Exp -> Exp)
-> ParsecT [Content] PState m Exp
-> ParsecT [Content] PState m (Exp -> Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
bot ParsecT [Content] PState m (Exp -> Exp)
-> ParsecT [Content] PState m Exp -> ParsecT [Content] PState m Exp
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Seq Content -> ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
top
                Exp -> ParsecT [Content] PState m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> ParsecT [Content] PState m Exp)
-> Exp -> ParsecT [Content] PState m Exp
forall a b. (a -> b) -> a -> b
$ [Exp] -> Exp
EGrouped [Item [Exp]
Exp
res, Item [Exp]
Exp
x]

      Exp
base' <- case (Maybe (Seq Content)
mbtr, Maybe (Seq Content)
mbbr) of
        (Maybe (Seq Content)
Nothing, Maybe (Seq Content)
Nothing) -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
base
        (Maybe (Seq Content)
Nothing, Just Seq Content
br) -> Exp -> Exp -> Exp
ESub Exp
base (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
br
        (Just Seq Content
tr, Maybe (Seq Content)
Nothing) -> Exp -> Exp -> Exp
ESuper Exp
base (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
tr
        (Just Seq Content
tr, Just Seq Content
br) -> Exp -> Exp -> Exp -> Exp
ESubsup Exp
base (Exp -> Exp -> Exp)
-> P m Exp -> ParsecT [Content] PState m (Exp -> Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
br ParsecT [Content] PState m (Exp -> Exp) -> P m Exp -> P m Exp
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
tr

      Exp
suffix <- case (Maybe (Seq Content)
mbt, Maybe (Seq Content)
mbb) of
        (Maybe (Seq Content)
Nothing, Maybe (Seq Content)
Nothing) -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
base'
        (Maybe (Seq Content)
Nothing, Just Seq Content
bot) -> Bool -> Exp -> Exp -> Exp
EUnder Bool
convertible Exp
base' (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
bot
        (Just Seq Content
top, Maybe (Seq Content)
Nothing) -> Bool -> Exp -> Exp -> Exp
EOver Bool
convertible Exp
base' (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
top
        (Just Seq Content
top, Just Seq Content
bot) -> Bool -> Exp -> Exp -> Exp -> Exp
EUnderover Bool
convertible Exp
base'
                                  (Exp -> Exp -> Exp)
-> P m Exp -> ParsecT [Content] PState m (Exp -> Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
bot ParsecT [Content] PState m (Exp -> Exp) -> P m Exp -> P m Exp
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
top

      Exp -> P m Exp
forall {m :: * -> *}.
PandocMonad m =>
Exp -> ParsecT [Content] PState m Exp
addPrefix Exp
suffix
    Elt Identifier
"math.serif" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextNormal ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.sans" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextSansSerif ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.frak" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextFraktur ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.mono" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextMonospace ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.cal" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextScript ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.bb" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextDoubleStruck ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.upright" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextNormal ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.bold" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextBold ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.italic" Maybe SourcePos
_ Map Identifier Val
fields ->
      TextType -> [Exp] -> Exp
EStyled TextType
TextItalic ([Exp] -> Exp) -> ParsecT [Content] PState m [Exp] -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> ParsecT [Content] PState m [Exp]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany)
    Elt Identifier
"math.underline" Maybe SourcePos
_ Map Identifier Val
fields ->
      Bool -> Exp -> Exp -> Exp
EUnder Bool
False
        (Exp -> Exp -> Exp)
-> P m Exp -> ParsecT [Content] PState m (Exp -> Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped)
        ParsecT [Content] PState m (Exp -> Exp) -> P m Exp -> P m Exp
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
"_")
    Elt Identifier
"math.overline" Maybe SourcePos
_ Map Identifier Val
fields ->
      Bool -> Exp -> Exp -> Exp
EOver Bool
False
        (Exp -> Exp -> Exp)
-> P m Exp -> ParsecT [Content] PState m (Exp -> Exp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped)
        ParsecT [Content] PState m (Exp -> Exp) -> P m Exp -> P m Exp
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
"\175")
    Elt Identifier
"math.underbrace" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Maybe (Seq Content)
mbAnn <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"annotation" Map Identifier Val
fields
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      let x :: Exp
x = Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
body (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
"\9183")
      case Maybe (Seq Content)
mbAnn of
        Maybe (Seq Content)
Nothing -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
x
        Just Seq Content
ann -> Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
x (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
ann
    Elt Identifier
"math.overbrace" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Maybe (Seq Content)
mbAnn <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"annotation" Map Identifier Val
fields
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      let x :: Exp
x = Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
body (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
"\9182")
      case Maybe (Seq Content)
mbAnn of
        Maybe (Seq Content)
Nothing -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
x
        Just Seq Content
ann -> Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
x (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
ann
    Elt Identifier
"math.underbracket" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Maybe (Seq Content)
mbAnn <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"annotation" Map Identifier Val
fields
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      let x :: Exp
x = Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
body (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
"\9141")
      case Maybe (Seq Content)
mbAnn of
        Maybe (Seq Content)
Nothing -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
x
        Just Seq Content
ann -> Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
x (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
ann
    Elt Identifier
"math.overbracket" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Maybe (Seq Content)
mbAnn <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"annotation" Map Identifier Val
fields
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      let x :: Exp
x = Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
body (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
"\9140")
      case Maybe (Seq Content)
mbAnn of
        Maybe (Seq Content)
Nothing -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Exp
x
        Just Seq Content
ann -> Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
x (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
ann
    Elt Identifier
"math.scripts" Maybe SourcePos
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
    Elt Identifier
"math.limits" Maybe SourcePos
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
    Elt Identifier
"math.root" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Maybe (Seq Content)
mbindex <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"index" Map Identifier Val
fields
      Exp
radicand <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"radicand" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      case Maybe (Seq Content)
mbindex of
        Maybe (Seq Content)
Nothing -> Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Exp -> Exp
ESqrt Exp
radicand
        Just Seq Content
index -> do
          Exp
index' <- Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
index
          Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Exp -> Exp -> Exp
ERoot Exp
index' Exp
radicand
    Elt Identifier
"math.sqrt" Maybe SourcePos
_ Map Identifier Val
fields ->
      Exp -> Exp
ESqrt (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"radicand" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped)
    Elt Identifier
"math.abs" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"|" Text
"|" [Exp -> InEDelimited
forall a b. b -> Either a b
Right Exp
body]
    Elt Identifier
"math.floor" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"\8970" Text
"\8971" [Exp -> InEDelimited
forall a b. b -> Either a b
Right Exp
body]
    Elt Identifier
"math.ceil" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"\8968" Text
"\8969" [Exp -> InEDelimited
forall a b. b -> Either a b
Right Exp
body]
    Elt Identifier
"math.norm" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"\8214" Text
"\8214" [Exp -> InEDelimited
forall a b. b -> Either a b
Right Exp
body]
    Elt Identifier
"math.round" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"\8970" Text
"\8969" [Exp -> InEDelimited
forall a b. b -> Either a b
Right Exp
body]
    Elt Identifier
"math.lr" Maybe SourcePos
_ Map Identifier Val
fields -> do
      [[Exp]]
bodyparts <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Vector (Seq Content))
-> (Vector (Seq Content) -> ParsecT [Content] PState m [[Exp]])
-> ParsecT [Content] PState m [[Exp]]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Seq Content -> ParsecT [Content] PState m [Exp])
-> [Seq Content] -> ParsecT [Content] PState m [[Exp]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany ([Seq Content] -> ParsecT [Content] PState m [[Exp]])
-> (Vector (Seq Content) -> [Seq Content])
-> Vector (Seq Content)
-> ParsecT [Content] PState m [[Exp]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList
      let rawbody :: [Exp]
rawbody = [Exp] -> [[Exp]] -> [Exp]
forall a. [a] -> [[a]] -> [a]
intercalate [TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Pun Text
","] [[Exp]]
bodyparts
      let (Text
op, [Exp]
rest) =
            case [Exp]
rawbody of
              (ESymbol TeXSymbolType
_ Text
t : [Exp]
xs) -> (Text
t, [Exp]
xs)
              [Exp]
_ -> (Text
"", [Exp]
rawbody)
      let ([Either a Exp]
body, Text
cl) =
            case [Exp] -> [Exp]
forall a. [a] -> [a]
reverse [Exp]
rest of
              (ESymbol TeXSymbolType
_ Text
t : [Exp]
_) -> ((Exp -> Either a Exp) -> [Exp] -> [Either a Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Either a Exp
forall a b. b -> Either a b
Right ([Exp] -> [Exp]
forall a. HasCallStack => [a] -> [a]
init [Exp]
rest), Text
t)
              [Exp]
_ -> ((Exp -> Either a Exp) -> [Exp] -> [Either a Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Either a Exp
forall a b. b -> Either a b
Right [Exp]
rest, Text
"")
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
op Text
cl [InEDelimited]
forall {a}. [Either a Exp]
body
    Elt Identifier
"math.binom" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp
up <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"upper" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp
low <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"lower" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [InEDelimited] -> Exp
EDelimited Text
"(" Text
")" [Exp -> InEDelimited
forall a b. b -> Either a b
Right (FractionType -> Exp -> Exp -> Exp
EFraction FractionType
NoLineFrac Exp
up Exp
low)]
    Elt Identifier
"math.cases" Maybe SourcePos
_ Map Identifier Val
fields -> do
      (Maybe Text
delim :: Maybe Text) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Text)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"delim" Map Identifier Val
fields
      ([Seq Content]
children :: [Seq Content]) <-
        (Val -> Seq Content) -> [Val] -> [Seq Content]
forall a b. (a -> b) -> [a] -> [b]
map Val -> Seq Content
valToContent ([Val] -> [Seq Content])
-> (Vector Val -> [Val]) -> Vector Val -> [Seq Content]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList (Vector Val -> [Seq Content])
-> ParsecT [Content] PState m (Vector Val)
-> ParsecT [Content] PState m [Seq Content]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Vector Val)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
      let isAlignPoint :: Content -> Bool
isAlignPoint (Elt Identifier
"math.alignpoint" Maybe SourcePos
_ Map Identifier Val
_) = Bool
True
          isAlignPoint Content
_ = Bool
False
      let formatRow :: Seq Content -> ParsecT [Content] PState m [[Exp]]
formatRow Seq Content
vs = case (Content -> Bool) -> Seq Content -> (Seq Content, Seq Content)
forall a. (a -> Bool) -> Seq a -> (Seq a, Seq a)
Seq.breakl Content -> Bool
isAlignPoint Seq Content
vs of
            (Seq Content
xs, Seq Content
ys) -> do
              case Seq Content -> ViewL Content
forall a. Seq a -> ViewL a
Seq.viewl Seq Content
ys of
                Content
_ Seq.:< Seq Content
rest -> do
                  [Exp]
xs' <- Seq Content -> P m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
xs
                  [Exp]
ys' <- Seq Content -> P m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
rest
                  [[Exp]] -> ParsecT [Content] PState m [[Exp]]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Exp]
Item [[Exp]]
xs', [Exp]
Item [[Exp]]
ys']
                ViewL Content
_ -> ([Exp] -> [[Exp]] -> [[Exp]]
forall a. a -> [a] -> [a]
: []) ([Exp] -> [[Exp]])
-> P m [Exp] -> ParsecT [Content] PState m [[Exp]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> P m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
vs
      [[[Exp]]]
rows <- (Seq Content -> ParsecT [Content] PState m [[Exp]])
-> [Seq Content] -> ParsecT [Content] PState m [[[Exp]]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Seq Content -> ParsecT [Content] PState m [[Exp]]
forall {m :: * -> *}.
PandocMonad m =>
Seq Content -> ParsecT [Content] PState m [[Exp]]
formatRow [Seq Content]
children
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$
        Text -> Text -> [InEDelimited] -> Exp
EDelimited
          (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"{" Maybe Text
delim)
          Text
""
          [Exp -> InEDelimited
forall a b. b -> Either a b
Right ([Alignment] -> [[[Exp]]] -> Exp
EArray [Item [Alignment]
Alignment
AlignLeft, Item [Alignment]
Alignment
AlignLeft] [[[Exp]]]
rows)]
    Elt Identifier
"math.vec" Maybe SourcePos
_ Map Identifier Val
fields -> do
      (Text
op, Text
cl) <- Map Identifier Val -> P m (Text, Text)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Text, Text)
arrayDelims Map Identifier Val
fields
      [[[Exp]]]
rows <-
        Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
          ParsecT [Content] PState m (Vector (Seq Content))
-> (Vector (Seq Content) -> ParsecT [Content] PState m [[[Exp]]])
-> ParsecT [Content] PState m [[[Exp]]]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Seq Content -> ParsecT [Content] PState m [[Exp]])
-> [Seq Content] -> ParsecT [Content] PState m [[[Exp]]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (([Exp] -> [[Exp]])
-> ParsecT [Content] PState m [Exp]
-> ParsecT [Content] PState m [[Exp]]
forall a b.
(a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Exp] -> [[Exp]] -> [[Exp]]
forall a. a -> [a] -> [a]
: []) (ParsecT [Content] PState m [Exp]
 -> ParsecT [Content] PState m [[Exp]])
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> Seq Content
-> ParsecT [Content] PState m [[Exp]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany) ([Seq Content] -> ParsecT [Content] PState m [[[Exp]]])
-> (Vector (Seq Content) -> [Seq Content])
-> Vector (Seq Content)
-> ParsecT [Content] PState m [[[Exp]]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$
        Text -> Text -> [InEDelimited] -> Exp
EDelimited
          Text
op
          Text
cl
          [Exp -> InEDelimited
forall a b. b -> Either a b
Right ([Alignment] -> [[[Exp]]] -> Exp
EArray [Item [Alignment]
Alignment
AlignCenter] [[[Exp]]]
rows)]
    Elt Identifier
"math.mat" Maybe SourcePos
_ Map Identifier Val
fields -> do
      (Text
op, Text
cl) <- Map Identifier Val -> P m (Text, Text)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Text, Text)
arrayDelims Map Identifier Val
fields
      let formatCell :: Val -> ParsecT [Content] PState m (Alignment, [Exp])
formatCell Val
x = do
            let content :: Seq Content
content = Val -> Seq Content
valToContent Val
x
            let align :: Alignment
align = case Seq Content -> ViewL Content
forall a. Seq a -> ViewL a
Seq.viewl Seq Content
content of
                  Elt Identifier
"math.alignpoint" Maybe SourcePos
_ Map Identifier Val
_ Seq.:< Seq Content
_ -> Alignment
AlignLeft
                  ViewL Content
_ -> case Seq Content -> ViewR Content
forall a. Seq a -> ViewR a
Seq.viewr Seq Content
content of
                    Seq Content
_ Seq.:> Elt Identifier
"math.alignpoint" Maybe SourcePos
_ Map Identifier Val
_ -> Alignment
AlignRight
                    ViewR Content
_ -> Alignment
AlignCenter
            [Exp]
exp' <- Seq Content -> P m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
content
            (Alignment, [Exp]) -> ParsecT [Content] PState m (Alignment, [Exp])
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Alignment
align, [Exp]
exp')
      let formatRow :: Val -> ParsecT [Content] PState m [(Alignment, [Exp])]
formatRow (VArray Vector Val
vs) = (Val -> ParsecT [Content] PState m (Alignment, [Exp]))
-> [Val] -> ParsecT [Content] PState m [(Alignment, [Exp])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Val -> ParsecT [Content] PState m (Alignment, [Exp])
forall {m :: * -> *}.
PandocMonad m =>
Val -> ParsecT [Content] PState m (Alignment, [Exp])
formatCell (Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList Vector Val
vs)
          formatRow Val
_ = String -> ParsecT [Content] PState m [(Alignment, [Exp])]
forall a. String -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"mat expected array"
      (Vector Val
rawrows :: V.Vector Val) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Vector Val)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"rows" Map Identifier Val
fields
      [[(Alignment, [Exp])]]
rows <- (Val -> ParsecT [Content] PState m [(Alignment, [Exp])])
-> [Val] -> ParsecT [Content] PState m [[(Alignment, [Exp])]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Val -> ParsecT [Content] PState m [(Alignment, [Exp])]
forall {m :: * -> *}.
PandocMonad m =>
Val -> ParsecT [Content] PState m [(Alignment, [Exp])]
formatRow (Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList Vector Val
rawrows)
      let aligns :: [Alignment]
aligns =
            case [[(Alignment, [Exp])]]
rows of
              [] -> []
              ([(Alignment, [Exp])]
r : [[(Alignment, [Exp])]]
_) -> ((Alignment, [Exp]) -> Alignment)
-> [(Alignment, [Exp])] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment, [Exp]) -> Alignment
forall a b. (a, b) -> a
fst [(Alignment, [Exp])]
r
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$
        Text -> Text -> [InEDelimited] -> Exp
EDelimited
          Text
op
          Text
cl
          [Exp -> InEDelimited
forall a b. b -> Either a b
Right ([Alignment] -> [[[Exp]]] -> Exp
EArray [Alignment]
aligns (([(Alignment, [Exp])] -> [[Exp]])
-> [[(Alignment, [Exp])]] -> [[[Exp]]]
forall a b. (a -> b) -> [a] -> [b]
map (((Alignment, [Exp]) -> [Exp]) -> [(Alignment, [Exp])] -> [[Exp]]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment, [Exp]) -> [Exp]
forall a b. (a, b) -> b
snd) [[(Alignment, [Exp])]]
rows))]
    Elt Identifier
"hide" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Exp -> Exp
EPhantom (Exp -> Exp) -> P m Exp -> P m Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Exp) -> P m Exp
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped)
    Elt Identifier
"h" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Length
amount <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m Length
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"amount" Map Identifier Val
fields
      let em :: Rational
em = case Length
amount of
            LExact Double
x LUnit
LEm -> Double -> Rational
forall a. Real a => a -> Rational
toRational Double
x
            Length
_ -> case Length
amount Length -> Length -> Length
forall a. Semigroup a => a -> a -> a
<> Double -> LUnit -> Length
LExact Double
0 LUnit
LPt of -- force to Pt
              LExact Double
x LUnit
LPt -> Double -> Rational
forall a. Real a => a -> Rational
toRational Double
x Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
12
              Length
_ -> Rational
1 Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
3 -- guess!
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ Rational -> Exp
ESpace Rational
em
    Elt Identifier
"grid" Maybe SourcePos
_ Map Identifier Val
fields -> do
      [[Exp]]
children <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields ParsecT [Content] PState m (Vector (Seq Content))
-> (Vector (Seq Content) -> ParsecT [Content] PState m [[Exp]])
-> ParsecT [Content] PState m [[Exp]]
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Seq Content -> ParsecT [Content] PState m [Exp])
-> [Seq Content] -> ParsecT [Content] PState m [[Exp]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany ([Seq Content] -> ParsecT [Content] PState m [[Exp]])
-> (Vector (Seq Content) -> [Seq Content])
-> Vector (Seq Content)
-> ParsecT [Content] PState m [[Exp]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList
      (Val
columns :: Val) <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"columns" Map Identifier Val
fields
      Int
numcols <- case Val
columns of
        VInteger Integer
x -> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ParsecT [Content] PState m Int)
-> Int -> ParsecT [Content] PState m Int
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x
        VArray Vector Val
x -> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ParsecT [Content] PState m Int)
-> Int -> ParsecT [Content] PState m Int
forall a b. (a -> b) -> a -> b
$ Vector Val -> Int
forall a. Vector a -> Int
V.length Vector Val
x
        Val
VNone -> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
        Val
_ -> String -> ParsecT [Content] PState m Int
forall a. String -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> ParsecT [Content] PState m Int)
-> String -> ParsecT [Content] PState m Int
forall a b. (a -> b) -> a -> b
$ String
"Could not determine number of columns: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Val -> String
forall a. Show a => a -> String
show Val
columns
      let rows :: [[[Exp]]]
rows = Int -> [[Exp]] -> [[[Exp]]]
forall a. Int -> [a] -> [[a]]
chunks Int
numcols [[Exp]]
children
      Exp -> P m Exp
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Exp -> P m Exp) -> Exp -> P m Exp
forall a b. (a -> b) -> a -> b
$ [Alignment] -> [[[Exp]]] -> Exp
EArray (Int -> Alignment -> [Alignment]
forall a. Int -> a -> [a]
replicate Int
numcols Alignment
AlignLeft) [[[Exp]]]
rows
    Elt Identifier
"table" Maybe SourcePos
pos Map Identifier Val
fields -> Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Content -> P m Exp
handleMath (Identifier -> Maybe SourcePos -> Map Identifier Val -> Content
Elt Identifier
"grid" Maybe SourcePos
pos Map Identifier Val
fields)
    Elt Identifier
"link" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"hyperlink in math"
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
body
    Elt Identifier
"math.display" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
content <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"content" Map Identifier Val
fields
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"display"
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
content
    Elt Identifier
"math.inline" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
content <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"content" Map Identifier Val
fields
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"inline"
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
content
    Elt Identifier
"math.script" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
content <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"content" Map Identifier Val
fields
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"script"
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
content
    Elt Identifier
"math.sscript" Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
content <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"content" Map Identifier Val
fields
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"sscript"
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
content
    Elt (Identifier Text
name) Maybe SourcePos
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> ParsecT [Content] PState m (Seq Content)
-> ParsecT [Content] PState m (Seq Content)
forall a.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Seq Content -> ParsecT [Content] PState m (Seq Content)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Seq Content
forall a. Monoid a => a
mempty
      Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
name
      Seq Content -> P m Exp
forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped Seq Content
body

arrayDelims :: PandocMonad m => M.Map Identifier Val -> P m (Text, Text)
arrayDelims :: forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Text, Text)
arrayDelims Map Identifier Val
fields = do
  (Maybe Text
mbdelim :: Maybe Text) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Text)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"delim" Map Identifier Val
fields
  (Text, Text) -> P m (Text, Text)
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((Text, Text) -> P m (Text, Text))
-> (Text, Text) -> P m (Text, Text)
forall a b. (a -> b) -> a -> b
$ case Maybe Text
mbdelim of
    Just Text
"(" -> (Text
"(", Text
")")
    Just Text
"[" -> (Text
"[", Text
"]")
    Just Text
"{" -> (Text
"{", Text
"}")
    Just Text
"|" -> (Text
"|", Text
"|")
    Just Text
"||" -> (Text
"\8741", Text
"\8741")
    Maybe Text
_ -> (Text
"(", Text
")")

pMathMany :: PandocMonad m => Seq Content -> P m [Exp]
pMathMany :: forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
cs = do
  -- check for "alignpoint" and "linebreak" elements
  -- and use an array structure for alignment
  let lns :: [Seq Content]
lns = Seq Content -> [Seq Content]
splitOnLinebreaks Seq Content
cs
  case [Seq Content]
lns of
    [] -> [Exp] -> P m [Exp]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
    [Item [Seq Content]
ln] | Bool -> Bool
not ((Content -> Bool) -> Seq Content -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Content -> Bool
isAlignpoint Item [Seq Content]
Seq Content
ln) -> P m [Exp] -> Seq Content -> P m [Exp]
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents (ParsecT [Content] PState m Exp -> P m [Exp]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => P m Exp
pMath) Item [Seq Content]
Seq Content
ln
    [Seq Content]
_ -> do
      [[[Exp]]]
rows <- (Seq Content -> ParsecT [Content] PState m [[Exp]])
-> [Seq Content] -> ParsecT [Content] PState m [[[Exp]]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((Seq Content -> P m [Exp])
-> [Seq Content] -> ParsecT [Content] PState m [[Exp]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (P m [Exp] -> Seq Content -> P m [Exp]
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents (ParsecT [Content] PState m Exp -> P m [Exp]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Content] PState m Exp
forall (m :: * -> *). PandocMonad m => P m Exp
pMath)) ([Seq Content] -> ParsecT [Content] PState m [[Exp]])
-> (Seq Content -> [Seq Content])
-> Seq Content
-> ParsecT [Content] PState m [[Exp]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq Content -> [Seq Content]
splitOnAlignpoints) [Seq Content]
lns
      let numcols :: Int
numcols = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([Int] -> Int) -> [Int] -> Int
forall a b. (a -> b) -> a -> b
$ ([[Exp]] -> Int) -> [[[Exp]]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [[Exp]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[[Exp]]]
rows
      let cols :: [Alignment]
cols = Int -> [Alignment] -> [Alignment]
forall a. Int -> [a] -> [a]
take Int
numcols ([Alignment] -> [Alignment]) -> [Alignment] -> [Alignment]
forall a b. (a -> b) -> a -> b
$ Alignment
AlignRight Alignment -> [Alignment] -> [Alignment]
forall a. a -> [a] -> [a]
: [Alignment] -> [Alignment]
forall a. HasCallStack => [a] -> [a]
cycle [Item [Alignment]
Alignment
AlignLeft, Item [Alignment]
Alignment
AlignRight]
      [Exp] -> P m [Exp]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [[Alignment] -> [[[Exp]]] -> Exp
EArray [Alignment]
cols [[[Exp]]]
rows]

pMathGrouped :: PandocMonad m => Seq Content -> P m Exp
pMathGrouped :: forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
pMathGrouped = ([Exp] -> Exp)
-> ParsecT [Content] PState m [Exp]
-> ParsecT [Content] PState m Exp
forall a b.
(a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Exp] -> Exp
withGroup (ParsecT [Content] PState m [Exp]
 -> ParsecT [Content] PState m Exp)
-> (Seq Content -> ParsecT [Content] PState m [Exp])
-> Seq Content
-> ParsecT [Content] PState m Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany

splitOnLinebreaks :: Seq Content -> [Seq Content]
splitOnLinebreaks :: Seq Content -> [Seq Content]
splitOnLinebreaks Seq Content
xs =
  if Seq Content -> Bool
forall a. Seq a -> Bool
Seq.null Seq Content
bs
    then
      if Seq Content -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq Content
as
        then []
        else [Item [Seq Content]
Seq Content
as]
    else Seq Content
as Seq Content -> [Seq Content] -> [Seq Content]
forall a. a -> [a] -> [a]
: Seq Content -> [Seq Content]
splitOnLinebreaks (Int -> Seq Content -> Seq Content
forall a. Int -> Seq a -> Seq a
Seq.drop Int
1 Seq Content
bs)
  where
    (Seq Content
as, Seq Content
bs) = (Content -> Bool) -> Seq Content -> (Seq Content, Seq Content)
forall a. (a -> Bool) -> Seq a -> (Seq a, Seq a)
Seq.breakl Content -> Bool
isLinebreak Seq Content
xs
    isLinebreak :: Content -> Bool
isLinebreak (Elt Identifier
"linebreak" Maybe SourcePos
_ Map Identifier Val
_) = Bool
True
    isLinebreak Content
_ = Bool
False

splitOnAlignpoints :: Seq Content -> [Seq Content]
splitOnAlignpoints :: Seq Content -> [Seq Content]
splitOnAlignpoints Seq Content
xs =
  if Seq Content -> Bool
forall a. Seq a -> Bool
Seq.null Seq Content
bs
    then
      if Seq Content -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq Content
as
        then []
        else [Item [Seq Content]
Seq Content
as]
    else Seq Content
as Seq Content -> [Seq Content] -> [Seq Content]
forall a. a -> [a] -> [a]
: Seq Content -> [Seq Content]
splitOnAlignpoints (Int -> Seq Content -> Seq Content
forall a. Int -> Seq a -> Seq a
Seq.drop Int
1 Seq Content
bs)
  where
    (Seq Content
as, Seq Content
bs) = (Content -> Bool) -> Seq Content -> (Seq Content, Seq Content)
forall a. (a -> Bool) -> Seq a -> (Seq a, Seq a)
Seq.breakl Content -> Bool
isAlignpoint Seq Content
xs

isAlignpoint :: Content -> Bool
isAlignpoint :: Content -> Bool
isAlignpoint (Elt Identifier
"math.alignpoint" Maybe SourcePos
_ Map Identifier Val
_) = Bool
True
isAlignpoint Content
_ = Bool
False