    {-# 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
      (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 base of
        [Elt Identifier
    "math.op" Maybe SourcePos
    _ Map Identifier Val
    fs] -> do
          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 limits == VBoolean True
             then pure $ Just Limits
             else pure Nothing
        [Elt Identifier
    "math.limits" Maybe SourcePos
    _ Map Identifier Val
    fs] -> do
          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 inl == VBoolean False
             then pure $ Just LimitsDisplay
             else pure $ Just Limits
        [Elt Identifier
    "math.scripts" Maybe SourcePos
    _ Map Identifier Val
    _] -> Maybe AttachmentStyle
    -> ParsecT [Content] PState m (Maybe AttachmentStyle)
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Maybe AttachmentStyle
     -> ParsecT [Content] PState m (Maybe AttachmentStyle))
    -> Maybe AttachmentStyle
    -> ParsecT [Content] PState 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
    -> ParsecT [Content] PState 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
          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
          (mbweight :: Maybe Text) <- getField "weight" fields
          case 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
          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
          denom <- getField "denom" fields >>= pMathGrouped
          pure $ EFraction NormalFrac num denom
        Elt Identifier
    "math.accent" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          acc <- getField "accent" fields >>= pMathGrouped
          let 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
          pure $ EOver False base acc'
        Elt Identifier
    "math.attach" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          t' <- getField "t" fields
          b' <- getField "b" fields
          tr' <- getField "tr" fields
          tl' <- getField "tl" fields
          br' <- getField "br" fields
          bl' <- getField "bl" fields
          attachmentStyle <- getAttachmentStyle fields
          let limits = case Maybe AttachmentStyle
    attachmentStyle of
                         Just AttachmentStyle
    Limits -> Bool
    True
                         Just AttachmentStyle
    LimitsDisplay -> Bool
    True
                         Maybe AttachmentStyle
    _ -> Bool
    False
          let 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 (mbt, mbtr) =
                case (t', 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 (mbb, mbbr) =
                case (b', 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] -> Exp
    EGrouped []
          let 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
                    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
                    pure $ EGrouped [res, x]
                  (Maybe (Seq Content)
    Nothing, Just Seq Content
    bot) -> do
                    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
                    pure $ EGrouped [res, x]
                  (Just Seq Content
    top, Just Seq Content
    bot) -> do
                    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
                    pure $ EGrouped [res, x]

          base' <- case (mbtr, 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

          suffix <- case (mbt, 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

          addPrefix 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
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EUnder Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TUnder Text
    "\9183")
          case 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
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EOver Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TOver Text
    "\9182")
          case 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
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EUnder Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TUnder Text
    "\9141")
          case 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
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EOver Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TOver Text
    "\9140")
          case 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.underparen" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EUnder Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TUnder Text
    "\9181")
          case 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.overparen" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          body <- getField "body" fields >>= pMathGrouped
          let x = Bool -> Exp -> Exp -> Exp
    EOver Bool
    False Exp
    body (TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    TOver Text
    "\9180")
          case 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
          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
          radicand <- getField "radicand" fields >>= pMathGrouped
          case 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
              index' <- Seq Content -> P m Exp
    forall (m :: * -> *). PandocMonad m => Seq Content -> P m Exp
    pMathGrouped Seq Content
    index
              pure $ ERoot index' 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
          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
          pure $ EDelimited "|" "|" [Right body]
        Elt Identifier
    "math.floor" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          pure $ EDelimited "\8970" "\8971" [Right body]
        Elt Identifier
    "math.ceil" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          pure $ EDelimited "\8968" "\8969" [Right body]
        Elt Identifier
    "math.norm" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          pure $ EDelimited "\8214" "\8214" [Right body]
        Elt Identifier
    "math.round" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          pure $ EDelimited "\8970" "\8969" [Right body]
        Elt Identifier
    "math.lr" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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] -> [[Exp]] -> [Exp]
    forall a. [a] -> [[a]] -> [a]
    intercalate [TeXSymbolType -> Text -> Exp
    ESymbol TeXSymbolType
    Pun Text
    ","] [[Exp]]
    bodyparts
          let (op, rest) =
                case rawbody of
                  (ESymbol TeXSymbolType
    _ Text
    t : [Exp]
    xs) -> (Text
    t, [Exp]
    xs)
                  [Exp]
    _ -> (Text
    "", [Exp]
    rawbody)
          let (body, cl) =
                case reverse 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
    "")
          pure $ EDelimited op cl body
        Elt Identifier
    "math.binom" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          low <- getField "lower" fields >>= pMathGrouped
          pure $ EDelimited "(" ")" [Right (EFraction NoLineFrac up low)]
        Elt Identifier
    "math.cases" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          (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
          (children :: [Seq Content]) <-
            map valToContent . V.toList <$> getField "children" fields
          let isAlignPoint (Elt Identifier
    "math.alignpoint" Maybe SourcePos
    _ Map Identifier Val
    _) = Bool
    True
              isAlignPoint Content
    _ = Bool
    False
          let 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
                      xs' <- Seq Content -> P m [Exp]
    forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
    pMathMany Seq Content
    xs
                      ys' <- pMathMany rest
                      pure [xs', 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
          rows <- mapM formatRow children
          pure $
            EDelimited
              (fromMaybe "{" delim)
              ""
              [Right (EArray [AlignLeft, AlignLeft] rows)]
        Elt Identifier
    "math.vec" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          (op, cl) <- Map Identifier Val -> P m (Text, Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier Val -> P m (Text, Text)
    arrayDelims Map Identifier Val
    fields
          rows <-
            getField "children" fields
              >>= mapM (fmap (: []) . pMathMany) . V.toList
          pure $
            EDelimited
              op
              cl
              [Right (EArray [AlignCenter] rows)]
        Elt Identifier
    "math.mat" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          (op, 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
    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' <- Seq Content -> P m [Exp]
    forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
    pMathMany Seq Content
    content
                pure (align, exp')
          let 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"
          (rawrows :: V.Vector Val) <- getField "rows" fields
          rows <- mapM formatRow (V.toList rawrows)
          let 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
          pure $
            EDelimited
              op
              cl
              [Right (EArray aligns (map (map snd) 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
          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 = 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!
          pure $ ESpace em
        Elt Identifier
    "grid" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          (columns :: Val) <- getField "columns" fields
          numcols <- case 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 = Int -> [[Exp]] -> [[[Exp]]]
    forall a. Int -> [a] -> [[a]]
    chunks Int
    numcols [[Exp]]
    children
          pure $ EArray (replicate numcols AlignLeft) 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
          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
          ignored "hyperlink in math"
          pMathGrouped body
        Elt Identifier
    "math.display" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          ignored "display"
          pMathGrouped content
        Elt Identifier
    "math.inline" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          ignored "inline"
          pMathGrouped content
        Elt Identifier
    "math.script" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          ignored "script"
          pMathGrouped content
        Elt Identifier
    "math.sscript" Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          ignored "sscript"
          pMathGrouped content
        Elt (Identifier Text
    name) Maybe SourcePos
    _ Map Identifier Val
    fields -> do
          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
          ignored name
          pMathGrouped 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
      (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
      pure $ case 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
          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] -> 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 = 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]
          pure [EArray cols 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