    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE UndecidableInstances #-}
    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE OverloadedLists #-}
    {-# LANGUAGE ScopedTypeVariables #-}

    {- |
       Module      : Text.Pandoc.Readers.Typst
       Copyright   : Copyright (C) 2023 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    Reads and evaluates a Typst document as a Pandoc AST.
    -}
    module Text.Pandoc.Readers.Typst
      ( readTypst
      )
    where

    import Text.Pandoc.Class
    import Text.Pandoc.Sources
    import Text.Pandoc.Options
    import Text.Pandoc.Definition
    import Typst ( parseTypst, evaluateTypst )
    import Text.Pandoc.Error (PandocError(..))
    import Text.Pandoc.Shared (tshow, blocksToInlines)
    import Control.Monad.Except (throwError)
    import Control.Monad (MonadPlus (mplus), void, guard, foldM)
    import qualified Data.Foldable as F
    import qualified Data.Map as M
    import Data.Maybe (catMaybes, fromMaybe)
    import Data.Sequence (Seq)
    import qualified Data.Sequence as Seq
    import qualified Data.Set as Set
    import Data.Text (Text)
    import qualified Data.Text as T
    import qualified Text.Pandoc.Builder as B
    import Text.Pandoc.Walk
    import Text.Parsec
    import Text.TeXMath (writeTeX)
    import Text.TeXMath.Shared (getSpaceChars)
    import Text.Pandoc.Readers.Typst.Math (pMathMany)
    import Text.Pandoc.Readers.Typst.Parsing (pTok, ignored, getField, P,
                                              PState(..), defaultPState)
    import Typst.Methods (formatNumber, applyPureFunction)
    import Typst.Types
    import qualified Data.Vector as V

    -- import Debug.Trace

    -- | Read Typst from an input string and return a Pandoc document.
    readTypst :: (PandocMonad m, ToSources a)
               => ReaderOptions -> a -> m Pandoc
    readTypst :: forall (m :: * -> *) a.
    (PandocMonad m, ToSources a) =>
    ReaderOptions -> a -> m Pandoc
    readTypst ReaderOptions
    _opts a
    inp = do
      let sources :: Sources
    sources = a -> Sources
    forall a. ToSources a => a -> Sources
    toSources a
    inp
      let inputName :: SourceName
    inputName = case Sources
    sources of
            Sources ((SourcePos
    pos, Text
    _):[(SourcePos, Text)]
    _) -> SourcePos -> SourceName
    sourceName SourcePos
    pos
            Sources
    _ -> SourceName
    ""
      case SourceName -> Text -> Either ParseError [Markup]
    parseTypst SourceName
    inputName (Sources -> Text
    sourcesToText Sources
    sources) of
        Left ParseError
    e -> PandocError -> m Pandoc
    forall a. PandocError -> m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
    forall a b. (a -> b) -> a -> b
    $ Text -> PandocError
    PandocParseError (Text -> PandocError) -> Text -> PandocError
    forall a b. (a -> b) -> a -> b
    $ SourceName -> Text
    T.pack (SourceName -> Text) -> SourceName -> Text
    forall a b. (a -> b) -> a -> b
    $ ParseError -> SourceName
    forall a. Show a => a -> SourceName
    show ParseError
    e
        Right [Markup]
    parsed -> do
          let ops :: Operations m
    ops = Operations {
                      loadBytes :: SourceName -> m ByteString
    loadBytes = SourceName -> m ByteString
    forall (m :: * -> *). PandocMonad m => SourceName -> m ByteString
    readFileStrict,
                      currentUTCTime :: m UTCTime
    currentUTCTime = m UTCTime
    forall (m :: * -> *). PandocMonad m => m UTCTime
    getCurrentTime,
                      lookupEnvVar :: SourceName -> m (Maybe SourceName)
    lookupEnvVar = (Maybe Text -> Maybe SourceName)
    -> m (Maybe Text) -> m (Maybe SourceName)
    forall a b. (a -> b) -> m a -> m b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ((Text -> SourceName) -> Maybe Text -> Maybe SourceName
    forall a b. (a -> b) -> Maybe a -> Maybe b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Text -> SourceName
    T.unpack) (m (Maybe Text) -> m (Maybe SourceName))
    -> (SourceName -> m (Maybe Text))
    -> SourceName
    -> m (Maybe SourceName)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> m (Maybe Text)
    forall (m :: * -> *). PandocMonad m => Text -> m (Maybe Text)
    lookupEnv (Text -> m (Maybe Text))
    -> (SourceName -> Text) -> SourceName -> m (Maybe Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . SourceName -> Text
    T.pack,
                      checkExistence :: SourceName -> m Bool
    checkExistence = SourceName -> m Bool
    forall (m :: * -> *). PandocMonad m => SourceName -> m Bool
    fileExists }
          res <- Operations m
    -> SourceName -> [Markup] -> m (Either ParseError Content)
    forall (m :: * -> *).
    Monad m =>
    Operations m
    -> SourceName -> [Markup] -> m (Either ParseError Content)
    evaluateTypst Operations m
    ops SourceName
    inputName [Markup]
    parsed
          case res of
            Left ParseError
    e -> PandocError -> m Pandoc
    forall a. PandocError -> m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
    forall a b. (a -> b) -> a -> b
    $ Text -> PandocError
    PandocParseError (Text -> PandocError) -> Text -> PandocError
    forall a b. (a -> b) -> a -> b
    $ ParseError -> Text
    forall a. Show a => a -> Text
    tshow ParseError
    e
            Right Content
    content -> do
              let labs :: [Text]
    labs = Seq Content -> [Text]
    findLabels [Item (Seq Content)
    Content
    content]
              ParsecT [Content] PState m Pandoc
    -> PState
    -> SourceName
    -> [Content]
    -> m (Either ParseError Pandoc)
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
    runParserT ParsecT [Content] PState m Pandoc
    forall (m :: * -> *). PandocMonad m => P m Pandoc
    pPandoc PState
    defaultPState{ sLabels = labs }
                SourceName
    inputName [Item [Content]
    Content
    content] m (Either ParseError Pandoc)
    -> (Either ParseError Pandoc -> m Pandoc) -> m Pandoc
    forall a b. m a -> (a -> m b) -> m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>=
                  (ParseError -> m Pandoc)
    -> (Pandoc -> m Pandoc) -> Either ParseError Pandoc -> m Pandoc
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either (PandocError -> m Pandoc
    forall a. PandocError -> m a
    forall e (m :: * -> *) a. MonadError e m => e -> m a
    throwError (PandocError -> m Pandoc)
    -> (ParseError -> PandocError) -> ParseError -> m Pandoc
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> PandocError
    PandocParseError (Text -> PandocError)
    -> (ParseError -> Text) -> ParseError -> PandocError
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . SourceName -> Text
    T.pack (SourceName -> Text)
    -> (ParseError -> SourceName) -> ParseError -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ParseError -> SourceName
    forall a. Show a => a -> SourceName
    show) Pandoc -> m Pandoc
    forall a. a -> m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure

    pBlockElt :: PandocMonad m => P m B.Blocks
    pBlockElt :: forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlockElt = ParsecT [Content] PState m Blocks
    -> ParsecT [Content] PState m Blocks
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Content] PState m Blocks
     -> ParsecT [Content] PState m Blocks)
    -> ParsecT [Content] PState m Blocks
    -> ParsecT [Content] PState m Blocks
    forall a b. (a -> b) -> a -> b
    $ do
      res <- (Content -> Bool) -> P m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok (\Content
    t -> Content -> Bool
    isBlock Content
    t Bool -> Bool -> Bool
    || Bool -> Bool
    not (Content -> Bool
    isInline Content
    t))
      -- check for following label
      mbident <- option Nothing $ Just <$> pLab
      case res of
        Elt name :: Identifier
    name@(Identifier Text
    tname) Maybe SourcePos
    pos Map Identifier Val
    fields -> do
          case Identifier
    -> Map
         Identifier
         (Maybe Text
          -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
    -> Maybe
         (Maybe Text
          -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    name Map
      Identifier
      (Maybe Text
       -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
    forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
    blockHandlers of
            Maybe
      (Maybe Text
       -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
    Nothing -> do
              Text -> P m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored (Text
    "unknown block element " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    tname Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                       Text
    " at " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Maybe SourcePos -> Text
    forall a. Show a => a -> Text
    tshow Maybe SourcePos
    pos)
              Blocks -> ParsecT [Content] PState m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Blocks
    forall a. Monoid a => a
    mempty
            Just Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Blocks
    handler -> Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Blocks
    handler Maybe Text
    mbident Map Identifier Val
    fields
        Content
    _ -> Blocks -> ParsecT [Content] PState m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Blocks
    forall a. Monoid a => a
    mempty

    pInline :: PandocMonad m => P m B.Inlines
    pInline :: forall (m :: * -> *). PandocMonad m => P m Inlines
    pInline = ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m Inlines
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Content] PState m Inlines
     -> ParsecT [Content] PState m Inlines)
    -> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m Inlines
    forall a b. (a -> b) -> a -> b
    $ do
      res <- (Content -> Bool) -> P m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok (\Content
    t -> Content -> Bool
    isInline Content
    t Bool -> Bool -> Bool
    || Bool -> Bool
    not (Content -> Bool
    isBlock Content
    t))
      case res of
        Txt Text
    t -> Inlines -> ParsecT [Content] PState m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines -> ParsecT [Content] PState m Inlines)
    -> Inlines -> ParsecT [Content] PState m Inlines
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines
    B.text Text
    t
        Lab Text
    name -> Inlines -> ParsecT [Content] PState m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines -> ParsecT [Content] PState m Inlines)
    -> Inlines -> ParsecT [Content] PState m Inlines
    forall a b. (a -> b) -> a -> b
    $ Attr -> Inlines -> Inlines
    B.spanWith (Text
    name, [], []) Inlines
    forall a. Monoid a => a
    mempty
        Elt (Identifier Text
    tname) Maybe SourcePos
    _ Map Identifier Val
    _
          | Text
    "math." Text -> Text -> Bool
    `T.isPrefixOf` Text
    tname
          , Text
    tname Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Text
    "math.equation" ->
              Text -> Inlines
    B.math (Text -> Inlines) -> ([Exp] -> Text) -> [Exp] -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Exp] -> Text
    writeTeX ([Exp] -> Inlines)
    -> ParsecT [Content] PState m [Exp]
    -> ParsecT [Content] PState m Inlines
    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 (Content -> Seq Content
    forall a. a -> Seq a
    Seq.singleton Content
    res)
        Elt name :: Identifier
    name@(Identifier Text
    tname) Maybe SourcePos
    pos Map Identifier Val
    fields -> do
          labs <- PState -> [Text]
    sLabels (PState -> [Text])
    -> ParsecT [Content] PState m PState
    -> ParsecT [Content] PState m [Text]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Content] PState m PState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
          labelTarget <- (do result <- getField "target" fields
                             case result of
                               VLabel Text
    t | Text
    t Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    labs -> Bool -> ParsecT [Content] PState m Bool
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Bool
    True
                               Val
    _ -> Bool -> ParsecT [Content] PState m Bool
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Bool
    False)
                      <|> pure False
          if tname == "ref" && not labelTarget
             then do
               -- @foo is a citation unless it links to a lab in the doc:
               let targetToKey (Identifier Text
    "target") = Text -> Identifier
    Identifier Text
    "key"
                   targetToKey Identifier
    k = Identifier
    k
               case M.lookup "cite" inlineHandlers of
                 Maybe
      (Maybe Text
       -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
    Nothing -> do
                   Text -> P m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored (Text
    "unknown inline element " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    tname Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                            Text
    " at " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Maybe SourcePos -> Text
    forall a. Show a => a -> Text
    tshow Maybe SourcePos
    pos)
                   Inlines -> ParsecT [Content] PState m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Inlines
    forall a. Monoid a => a
    mempty
                 Just Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Inlines
    handler -> Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Inlines
    handler Maybe Text
    forall a. Maybe a
    Nothing ((Identifier -> Identifier)
    -> Map Identifier Val -> Map Identifier Val
    forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
    M.mapKeys Identifier -> Identifier
    targetToKey Map Identifier Val
    fields)
             else do
              case M.lookup name inlineHandlers of
                Maybe
      (Maybe Text
       -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
    Nothing -> do
                  Text -> P m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored (Text
    "unknown inline element " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    tname Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                           Text
    " at " Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Maybe SourcePos -> Text
    forall a. Show a => a -> Text
    tshow Maybe SourcePos
    pos)
                  Inlines -> ParsecT [Content] PState m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Inlines
    forall a. Monoid a => a
    mempty
                Just Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Inlines
    handler -> Maybe Text
    -> Map Identifier Val -> ParsecT [Content] PState m Inlines
    handler Maybe Text
    forall a. Maybe a
    Nothing Map Identifier Val
    fields

    pPandoc :: PandocMonad m => P m B.Pandoc
    pPandoc :: forall (m :: * -> *). PandocMonad m => P m Pandoc
    pPandoc = do
      Elt "document" _ fields <- (Content -> Bool) -> P m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok Content -> Bool
    isDocument
      bs <- getField "body" fields >>= pWithContents pBlocks
      pure $ B.doc bs
      -- The following alternative code would add metadata from the
      -- fields on the document element. It is commented out because
      -- the typst metadata doesn't print anything by default, in contrast
      -- to pandoc with its usual templates.  Hence, with this code,
      -- converting a typst document might yield a double title, author, etc.
      --
      -- title <- (getField "title" fields >>= pWithContents pInlines) <|>
      --             pure mempty
      -- authors <- (getField "author" fields >>=
      --                         mapM (pWithContents pInlines) . V.toList) <|>
      --            ((:[]) <$> (getField "author" fields >>=
      --                          (\x -> guard (not (null x)) *>
      --                            pWithContents pInlines x))) <|>
      --             pure []
      -- date <- (getField "date" fields >>= pWithContents pInlines) <|>
      --             pure mempty
      -- keywords <- (getField "keywords" fields >>=
      --                mapM (pWithContents pInlines) . V.toList)
      --               <|> pure []
      -- pure $
      --   (if title == mempty
      --       then id
      --       else B.setMeta "title" title) .
      --   (if null authors
      --       then id
      --       else B.setMeta "author" authors) .
      --   (if null date
      --       then id
      --       else B.setMeta "date" date) .
      --   (if null keywords
      --       then id
      --       else B.setMeta "keywords" keywords) $ B.doc bs

    pBlocks :: PandocMonad m => P m B.Blocks
    pBlocks :: forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks = [Blocks] -> Blocks
    forall a. Monoid a => [a] -> a
    mconcat ([Blocks] -> Blocks)
    -> ParsecT [Content] PState m [Blocks]
    -> ParsecT [Content] PState m Blocks
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Content] PState m Blocks
    -> ParsecT [Content] PState m [Blocks]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many ParsecT [Content] PState m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlock

    pBlock :: PandocMonad m => P m B.Blocks
    pBlock :: forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlock = P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pPara P m Blocks -> P m Blocks -> P m Blocks
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlockElt

    pSpace :: PandocMonad m => P m Content
    pSpace :: forall (m :: * -> *). PandocMonad m => P m Content
    pSpace = (Content -> Bool) -> P m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok
          ( \case
              Txt Text
    t | (Char -> Bool) -> Text -> Bool
    T.all (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    ' ') Text
    t -> Bool
    True
              Content
    _ -> Bool
    False )

    pLab :: PandocMonad m => P m Text
    pLab :: forall (m :: * -> *). PandocMonad m => P m Text
    pLab = ParsecT [Content] PState m Text -> ParsecT [Content] PState m Text
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT [Content] PState m Text
     -> ParsecT [Content] PState m Text)
    -> ParsecT [Content] PState m Text
    -> ParsecT [Content] PState m Text
    forall a b. (a -> b) -> a -> b
    $ do
      ParsecT [Content] PState m Content -> ParsecT [Content] PState m ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m ()
    optional ParsecT [Content] PState m Content
    forall (m :: * -> *). PandocMonad m => P m Content
    pSpace
      Lab t <- (Content -> Bool) -> ParsecT [Content] PState m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok
           ( \case
               Lab Text
    _ -> Bool
    True
               Content
    _ -> Bool
    False
           )
      pure t

    isDocument :: Content -> Bool
    isDocument :: Content -> Bool
    isDocument (Elt Identifier
    "document" Maybe SourcePos
    _ Map Identifier Val
    _) = Bool
    True
    isDocument Content
    _ = Bool
    False

    isBlock :: Content -> Bool
    isBlock :: Content -> Bool
    isBlock (Elt Identifier
    "raw" Maybe SourcePos
    _ Map Identifier Val
    fields) = Identifier -> Map Identifier Val -> Maybe Val
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    "block" Map Identifier Val
    fields Maybe Val -> Maybe Val -> Bool
    forall a. Eq a => a -> a -> Bool
    == Val -> Maybe Val
    forall a. a -> Maybe a
    Just (Bool -> Val
    VBoolean Bool
    True)
    isBlock (Elt Identifier
    name Maybe SourcePos
    _ Map Identifier Val
    _) = Identifier
    name Identifier -> Set Identifier -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Identifier
    blockKeys
    isBlock Lab{} = Bool
    True
    isBlock Content
    _ = Bool
    False

    isInline :: Content -> Bool
    isInline :: Content -> Bool
    isInline (Elt Identifier
    "raw" Maybe SourcePos
    _ Map Identifier Val
    fields) = Identifier -> Map Identifier Val -> Maybe Val
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    "block" Map Identifier Val
    fields Maybe Val -> Maybe Val -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Val -> Maybe Val
    forall a. a -> Maybe a
    Just (Bool -> Val
    VBoolean Bool
    True)
    isInline (Elt Identifier
    name Maybe SourcePos
    _ Map Identifier Val
    _) = Identifier
    name Identifier -> Set Identifier -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Identifier
    inlineKeys
    isInline Lab{} = Bool
    True
    isInline Txt{} = Bool
    True

    blockKeys :: Set.Set Identifier
    blockKeys :: Set Identifier
    blockKeys = [Identifier] -> Set Identifier
    forall a. Ord a => [a] -> Set a
    Set.fromList ([Identifier] -> Set Identifier) -> [Identifier] -> Set Identifier
    forall a b. (a -> b) -> a -> b
    $ Map
      Identifier
      (Maybe Text -> Map Identifier Val -> P PandocPure Blocks)
    -> [Identifier]
    forall k a. Map k a -> [k]
    M.keys
      (Map
      Identifier
      (Maybe Text -> Map Identifier Val -> P PandocPure Blocks)
    forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
    blockHandlers :: M.Map Identifier
         (Maybe Text -> M.Map Identifier Val -> P PandocPure B.Blocks))

    inlineKeys :: Set.Set Identifier
    inlineKeys :: Set Identifier
    inlineKeys = [Identifier] -> Set Identifier
    forall a. Ord a => [a] -> Set a
    Set.fromList ([Identifier] -> Set Identifier) -> [Identifier] -> Set Identifier
    forall a b. (a -> b) -> a -> b
    $ Map
      Identifier
      (Maybe Text -> Map Identifier Val -> P PandocPure Inlines)
    -> [Identifier]
    forall k a. Map k a -> [k]
    M.keys
      (Map
      Identifier
      (Maybe Text -> Map Identifier Val -> P PandocPure Inlines)
    forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
    inlineHandlers :: M.Map Identifier
         (Maybe Text -> M.Map Identifier Val -> P PandocPure B.Inlines))

    blockHandlers :: PandocMonad m =>
                       M.Map Identifier
                       (Maybe Text -> M.Map Identifier Val -> P m B.Blocks)
    blockHandlers :: forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
    blockHandlers = [(Identifier, Maybe Text -> Map Identifier Val -> P m Blocks)]
    -> Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [(Identifier
    "text", \Maybe Text
    _ 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
          -- sometimes text elements include para breaks
          notFollowedBy $ void $ pWithContents pInlines body
          pWithContents pBlocks body)
      ,(Identifier
    "box", \Maybe Text
    _ 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
          B.divWith ("", ["box"], []) <$> pWithContents pBlocks body)
      ,(Identifier
    "heading", \Maybe Text
    mbident 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
          lev <- getField "level" fields <|> pure 1
          B.headerWith (fromMaybe "" mbident,[],[]) lev
             <$> pWithContents pInlines body)
      ,(Identifier
    "quote", \Maybe Text
    _ Map Identifier Val
    fields -> do
          Identifier -> Map Identifier Val -> ParsecT [Content] PState m Bool
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "block" Map Identifier Val
    fields ParsecT [Content] PState m Bool
    -> (Bool -> ParsecT [Content] PState m ())
    -> ParsecT [Content] PState m ()
    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
    >>= Bool -> ParsecT [Content] PState m ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard
          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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks
          attribution' <- getField "attribution" fields
          attribution <- if attribution' == mempty
                            then pure mempty
                            else (\Inlines
    x -> Inlines -> Blocks
    B.para (Inlines
    "\x2014\xa0" Inlines -> Inlines -> Inlines
    forall a. Semigroup a => a -> a -> a
    <> Inlines
    x)) <$>
                                  (pWithContents pInlines attribution')
          pure $ B.blockQuote $ body <> attribution)
      ,(Identifier
    "list", \Maybe Text
    _ Map Identifier Val
    fields -> do
          children <- Vector (Seq Content) -> [Seq Content]
    forall a. Vector a -> [a]
    V.toList (Vector (Seq Content) -> [Seq Content])
    -> ParsecT [Content] PState m (Vector (Seq Content))
    -> 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 (Seq Content))
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "children" Map Identifier Val
    fields
          B.bulletList <$> mapM (pWithContents pBlocks) children)
      ,(Identifier
    "list.item", \Maybe Text
    _ 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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ,(Identifier
    "enum", \Maybe Text
    _ Map Identifier Val
    fields -> do
          children <- Vector (Seq Content) -> [Seq Content]
    forall a. Vector a -> [a]
    V.toList (Vector (Seq Content) -> [Seq Content])
    -> ParsecT [Content] PState m (Vector (Seq Content))
    -> 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 (Seq Content))
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "children" Map Identifier Val
    fields
          mbstart <- getField "start" fields
          start <- case mbstart of
            Maybe Int
    Nothing -> 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
            Just Int
    x
              | Int
    x Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    0 -> Int -> ParsecT [Content] PState m Int
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Int
    x
              | Bool
    otherwise -> SourceName -> ParsecT [Content] PState m Int
    forall a. SourceName -> ParsecT [Content] PState m a
    forall (m :: * -> *) a. MonadFail m => SourceName -> m a
    fail SourceName
    "number must be positive"
          (numbering :: Text) <- getField "numbering" fields `mplus` pure ""
          let (sty, delim) =
                case numbering of
                  Text
    "1." -> (ListNumberStyle
    B.Decimal, ListNumberDelim
    B.Period)
                  Text
    "1)" -> (ListNumberStyle
    B.Decimal, ListNumberDelim
    B.OneParen)
                  Text
    "(1)" -> (ListNumberStyle
    B.Decimal, ListNumberDelim
    B.TwoParens)
                  Text
    "a." -> (ListNumberStyle
    B.LowerAlpha, ListNumberDelim
    B.Period)
                  Text
    "a)" -> (ListNumberStyle
    B.LowerAlpha, ListNumberDelim
    B.OneParen)
                  Text
    "(a)" -> (ListNumberStyle
    B.LowerAlpha, ListNumberDelim
    B.TwoParens)
                  Text
    "A." -> (ListNumberStyle
    B.UpperAlpha, ListNumberDelim
    B.Period)
                  Text
    "A)" -> (ListNumberStyle
    B.UpperAlpha, ListNumberDelim
    B.OneParen)
                  Text
    "(A)" -> (ListNumberStyle
    B.UpperAlpha, ListNumberDelim
    B.TwoParens)
                  Text
    "i." -> (ListNumberStyle
    B.LowerRoman, ListNumberDelim
    B.Period)
                  Text
    "i)" -> (ListNumberStyle
    B.LowerRoman, ListNumberDelim
    B.OneParen)
                  Text
    "(i)" -> (ListNumberStyle
    B.LowerRoman, ListNumberDelim
    B.TwoParens)
                  Text
    "I." -> (ListNumberStyle
    B.UpperRoman, ListNumberDelim
    B.Period)
                  Text
    "I)" -> (ListNumberStyle
    B.UpperRoman, ListNumberDelim
    B.OneParen)
                  Text
    "(I)" -> (ListNumberStyle
    B.UpperRoman, ListNumberDelim
    B.TwoParens)
                  Text
    _ -> (ListNumberStyle
    B.DefaultStyle, ListNumberDelim
    B.DefaultDelim)
          let listAttr = (Int
    start, ListNumberStyle
    sty, ListNumberDelim
    delim)
          B.orderedListWith listAttr <$> mapM (pWithContents pBlocks) children)
      ,(Identifier
    "enum.item", \Maybe Text
    _ 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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ,(Identifier
    "terms", \Maybe Text
    _ Map Identifier Val
    fields -> do
          children <- Vector Val -> [Val]
    forall a. Vector a -> [a]
    V.toList (Vector Val -> [Val])
    -> ParsecT [Content] PState m (Vector Val)
    -> ParsecT [Content] PState m [Val]
    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
          B.definitionList
            <$> mapM
              ( \case
                  VTermItem Seq Content
    t Seq Content
    d -> do
                    t' <- ParsecT [Content] PState m Inlines
    -> Seq Content -> ParsecT [Content] PState m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents ParsecT [Content] PState m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines Seq Content
    t
                    d' <- pWithContents pBlocks d
                    pure (t', [d'])
                  Val
    _ -> (Inlines, [Blocks])
    -> ParsecT [Content] PState m (Inlines, [Blocks])
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines
    forall a. Monoid a => a
    mempty, [])
              )
              children)
      ,(Identifier
    "terms.item", \Maybe Text
    _ 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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ,(Identifier
    "raw", \Maybe Text
    mbident Map Identifier Val
    fields -> do
          txt <- (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '\r') (Text -> Text)
    -> ParsecT [Content] PState m Text
    -> ParsecT [Content] PState m Text
    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
          mblang <- getField "lang" fields
          let attr = (Text -> Maybe Text -> Text
    forall a. a -> Maybe a -> a
    fromMaybe Text
    "" Maybe Text
    mbident, [Text] -> (Item [Text] -> [Text]) -> Maybe (Item [Text]) -> [Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Item [Text]
    l -> [Item [Text]
    l]) Maybe Text
    Maybe (Item [Text])
    mblang, [])
          pure $ B.codeBlockWith attr txt)
      ,(Identifier
    "parbreak", \Maybe Text
    _ Map Identifier Val
    _ -> Blocks -> P m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Blocks
    forall a. Monoid a => a
    mempty)
      ,(Identifier
    "block", \Maybe Text
    mbident Map Identifier Val
    fields ->
          (Blocks -> Blocks)
    -> (Text -> Blocks -> Blocks) -> Maybe Text -> Blocks -> Blocks
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Blocks -> Blocks
    forall a. a -> a
    id (\Text
    ident -> Attr -> Blocks -> Blocks
    B.divWith (Text
    ident, [], [])) Maybe Text
    mbident
            (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "place", \Maybe Text
    _ Map Identifier Val
    fields -> do
          Text -> ParsecT [Content] PState m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored Text
    "parameters of place"
          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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ,(Identifier
    "columns", \Maybe Text
    _ Map Identifier Val
    fields -> do
          (cnt :: Integer) <- Identifier
    -> Map Identifier Val -> ParsecT [Content] PState m Integer
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "count" Map Identifier Val
    fields
          B.divWith ("", ["columns-flow"], [("count", T.pack (show cnt))])
            <$> (getField "body" fields >>= pWithContents pBlocks))
      ,(Identifier
    "rect", \Maybe Text
    _ Map Identifier Val
    fields ->
          Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [Text
    Item [Text]
    "rect"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "circle", \Maybe Text
    _ Map Identifier Val
    fields ->
          Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [Text
    Item [Text]
    "circle"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "ellipse", \Maybe Text
    _ Map Identifier Val
    fields ->
          Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [Text
    Item [Text]
    "ellipse"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "polygon", \Maybe Text
    _ Map Identifier Val
    fields ->
          Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [Text
    Item [Text]
    "polygon"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "square", \Maybe Text
    _ Map Identifier Val
    fields ->
          Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [Text
    Item [Text]
    "square"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
    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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "align", \Maybe Text
    _ Map Identifier Val
    fields -> do
          alignment <- 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
    "alignment" Map Identifier Val
    fields
          B.divWith ("", [], [("align", repr alignment)])
            <$> (getField "body" fields >>= pWithContents pBlocks))
      ,(Identifier
    "stack", \Maybe Text
    _ Map Identifier Val
    fields -> do
          (dir :: Direction) <- Identifier
    -> Map Identifier Val -> ParsecT [Content] PState m Direction
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "dir" Map Identifier Val
    fields ParsecT [Content] PState m Direction
    -> ParsecT [Content] PState m Direction
    -> ParsecT [Content] PState m Direction
    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` Direction -> ParsecT [Content] PState m Direction
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Direction
    Ltr
          rawchildren <- getField "children" fields
          children <-
            mapM
              ( \case
                  val :: Val
    val@(VFraction {}) ->
                    Blocks -> P m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
    forall a b. (a -> b) -> a -> b
    $ Attr -> Blocks -> Blocks
    B.divWith (Text
    "", [], [(Text
    "space", Val -> Text
    repr Val
    val)]) Blocks
    forall a. Monoid a => a
    mempty
                  Val
    val -> Val -> ParsecT [Content] PState m (Seq Content)
    forall a (m :: * -> *).
    (FromVal a, MonadPlus m, MonadFail m) =>
    Val -> m a
    forall (m :: * -> *).
    (MonadPlus m, MonadFail m) =>
    Val -> m (Seq Content)
    fromVal Val
    val ParsecT [Content] PState m (Seq Content)
    -> (Seq Content -> P m Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks
              )
              (V.toList rawchildren)
          pure $
            B.divWith ("", [], [("stack", repr (VDirection dir))]) $
              mconcat $
                map (B.divWith ("", [], [])) children)
      ,(Identifier
    "grid", \Maybe Text
    mbident Map Identifier Val
    fields -> Maybe Text -> Map Identifier Val -> P m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    Maybe Text -> Map Identifier Val -> P m Blocks
    parseTable Maybe Text
    mbident Map Identifier Val
    fields)
      ,(Identifier
    "table", \Maybe Text
    mbident Map Identifier Val
    fields -> Maybe Text -> Map Identifier Val -> P m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    Maybe Text -> Map Identifier Val -> P m Blocks
    parseTable Maybe Text
    mbident Map Identifier Val
    fields)
      ,(Identifier
    "figure", \Maybe Text
    mbident 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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks
          (mbCaption :: Maybe (Seq Content)) <- getField "caption" fields
          (caption :: B.Blocks) <- maybe mempty (pWithContents pBlocks) mbCaption
          pure $ case B.toList body of
            [B.Table Attr
    attr Caption
    _ [ColSpec]
    colspecs TableHead
    thead [TableBody]
    tbodies TableFoot
    tfoot] ->
              Block -> Blocks
    forall a. a -> Many a
    B.singleton
                (Attr
    -> Caption
    -> [ColSpec]
    -> TableHead
    -> [TableBody]
    -> TableFoot
    -> Block
    B.Table Attr
    attr (Maybe [Inline] -> [Block] -> Caption
    B.Caption Maybe [Inline]
    forall a. Maybe a
    Nothing (Blocks -> [Block]
    forall a. Many a -> [a]
    B.toList Blocks
    caption)) [ColSpec]
    colspecs TableHead
    thead [TableBody]
    tbodies TableFoot
    tfoot)
            [Block]
    _ -> Attr -> Caption -> Blocks -> Blocks
    B.figureWith (Text -> Maybe Text -> Text
    forall a. a -> Maybe a -> a
    fromMaybe Text
    "" Maybe Text
    mbident, [], [])
                              (Maybe [Inline] -> [Block] -> Caption
    B.Caption Maybe [Inline]
    forall a. Maybe a
    Nothing (Blocks -> [Block]
    forall a. Many a -> [a]
    B.toList Blocks
    caption)) Blocks
    body)
      ,(Identifier
    "line", \Maybe Text
    _ Map Identifier Val
    fields ->
          case ( Identifier -> Map Identifier Val -> Maybe Val
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    "start" Map Identifier Val
    fields
                  Maybe Val -> Maybe Val -> Maybe Val
    forall a b. Maybe a -> Maybe b -> Maybe b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Identifier -> Map Identifier Val -> Maybe Val
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    "end" Map Identifier Val
    fields
                  Maybe Val -> Maybe Val -> Maybe Val
    forall a b. Maybe a -> Maybe b -> Maybe b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Identifier -> Map Identifier Val -> Maybe Val
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Identifier
    "angle" Map Identifier Val
    fields ) of
            Maybe Val
    Nothing -> Blocks -> P m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Blocks
    B.horizontalRule
            Maybe Val
    _ -> Blocks -> P m Blocks
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Blocks
    forall a. Monoid a => a
    mempty)
      ,(Identifier
    "numbering", \Maybe Text
    _ Map Identifier Val
    fields -> do
          numStyle <- 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
    "numbering" Map Identifier Val
    fields
          (nums :: V.Vector Integer) <- getField "numbers" fields
          let toText Val
    v = a -> Maybe a -> a
    forall a. a -> Maybe a -> a
    fromMaybe a
    "" (Maybe a -> a) -> Maybe a -> a
    forall a b. (a -> b) -> a -> b
    $ Val -> Maybe a
    forall a (m :: * -> *).
    (FromVal a, MonadPlus m, MonadFail m) =>
    Val -> m a
    forall (m :: * -> *). (MonadPlus m, MonadFail m) => Val -> m a
    fromVal Val
    v
          let toNum Integer
    n =
                case Val
    numStyle of
                  VString Text
    t -> Text -> Int -> Text
    formatNumber Text
    t (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)
                  VFunction Maybe Identifier
    _ Map Identifier Val
    _ Function
    f ->
                    case Function -> [Val] -> Attempt Val
    applyPureFunction Function
    f [Integer -> Val
    VInteger Integer
    n] of
                      Success Val
    x -> Val -> Text
    forall {a}. (IsString a, FromVal a) => Val -> a
    toText Val
    x
                      Failure SourceName
    _ -> Text
    "?"
                  Val
    _ -> Text
    "?"
          pure $ B.plain . B.text . mconcat . map toNum $ V.toList nums)
      ,(Identifier
    "footnote.entry", \Maybe Text
    _ 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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ,(Identifier
    "pad", \Maybe Text
    _ Map Identifier Val
    fields ->  -- ignore paddingy
          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 Blocks) -> P m Blocks
    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
    >>= P m Blocks -> Seq Content -> P m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
      ]

    inlineHandlers :: PandocMonad m =>
        M.Map Identifier (Maybe Text -> M.Map Identifier Val -> P m B.Inlines)
    inlineHandlers :: forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
    inlineHandlers = [(Identifier, Maybe Text -> Map Identifier Val -> P m Inlines)]
    -> Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [(Identifier
    "ref", \Maybe Text
    _ Map Identifier Val
    fields -> do
          VLabel target <- 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
    "target" Map Identifier Val
    fields
          supplement' <- getField "supplement" fields
          supplement <- case supplement' of
                          Val
    VAuto -> -- TODO for now, until we can locate the element
                            Inlines -> P m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines
    B.text (Text
    "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    target Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "]")
                          VContent Seq Content
    cs -> P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines Seq Content
    cs
                          VFunction Maybe Identifier
    _ Map Identifier Val
    _ Function
    _f -> -- TODO for now, until we can locate the element
                               Inlines -> P m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines
    B.text (Text
    "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    target Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "]")
                          Val
    _ -> Inlines -> P m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Inlines
    forall a. Monoid a => a
    mempty
          pure $ B.linkWith ("", ["ref"], []) ("#" <> target) "" supplement)
      ,(Identifier
    "linebreak", \Maybe Text
    _ Map Identifier Val
    _ -> Inlines -> P m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Inlines
    B.linebreak)
      ,(Identifier
    "text", \Maybe Text
    _ 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" -> Inlines -> Inlines
    B.strong (Inlines -> Inlines) -> P m Inlines -> P m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines Seq Content
    body
            Maybe Text
    _ -> P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines Seq Content
    body)
      ,(Identifier
    "raw", \Maybe Text
    _ Map Identifier Val
    fields -> Text -> Inlines
    B.code (Text -> Inlines) -> (Text -> Text) -> Text -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '\r') (Text -> Inlines) -> ParsecT [Content] PState m Text -> P m Inlines
    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)
      ,(Identifier
    "footnote", \Maybe Text
    _ Map Identifier Val
    fields ->
          Blocks -> Inlines
    B.note (Blocks -> Inlines)
    -> ParsecT [Content] PState m Blocks -> P m Inlines
    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 Blocks)
    -> ParsecT [Content] PState m Blocks
    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
    >>= ParsecT [Content] PState m Blocks
    -> Seq Content -> ParsecT [Content] PState m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents ParsecT [Content] PState m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks))
      ,(Identifier
    "cite", \Maybe Text
    _ Map Identifier Val
    fields -> do
          VLabel key <- 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
    "key" Map Identifier Val
    fields
          (form :: Text) <- getField "form" fields <|> pure "normal"
          let citation =
                B.Citation
                  { citationId :: Text
    B.citationId = Text
    key,
                    citationPrefix :: [Inline]
    B.citationPrefix = [Inline]
    forall a. Monoid a => a
    mempty,
                    citationSuffix :: [Inline]
    B.citationSuffix = [Inline]
    forall a. Monoid a => a
    mempty,
                    citationMode :: CitationMode
    B.citationMode = case Text
    form of
                                        Text
    "year" -> CitationMode
    B.SuppressAuthor
                                        Text
    _ -> CitationMode
    B.NormalCitation,
                    citationNoteNum :: Int
    B.citationNoteNum = Int
    0,
                    citationHash :: Int
    B.citationHash = Int
    0
                  }
          pure $ B.cite [citation] (B.text $ "[" <> key <> "]"))
      ,(Identifier
    "lower", \Maybe Text
    _ 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
    "text" Map Identifier Val
    fields
          walk (modString T.toLower) <$> pWithContents pInlines body)
      ,(Identifier
    "upper", \Maybe Text
    _ 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
    "text" Map Identifier Val
    fields
          walk (modString T.toUpper) <$> pWithContents pInlines body)
      ,(Identifier
    "emph", \Maybe Text
    _ 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
          B.emph <$> pWithContents pInlines body)
      ,(Identifier
    "strong", \Maybe Text
    _ 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
          B.strong <$> pWithContents pInlines body)
      ,(Identifier
    "sub", \Maybe Text
    _ 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
          B.subscript <$> pWithContents pInlines body)
      ,(Identifier
    "super", \Maybe Text
    _ 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
          B.superscript <$> pWithContents pInlines body)
      ,(Identifier
    "strike", \Maybe Text
    _ 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
          B.strikeout <$> pWithContents pInlines body)
      ,(Identifier
    "smallcaps", \Maybe Text
    _ 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
          B.smallcaps <$> pWithContents pInlines body)
      ,(Identifier
    "underline", \Maybe Text
    _ 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
          B.underline <$> pWithContents pInlines body)
      ,(Identifier
    "quote", \Maybe Text
    _ Map Identifier Val
    fields -> do
          (Identifier -> Map Identifier Val -> ParsecT [Content] PState m Bool
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "block" Map Identifier Val
    fields ParsecT [Content] PState m Bool
    -> ParsecT [Content] PState m Bool
    -> ParsecT [Content] PState m Bool
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> Bool -> ParsecT [Content] PState m Bool
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Bool
    False) ParsecT [Content] PState m Bool
    -> (Bool -> ParsecT [Content] PState m ())
    -> ParsecT [Content] PState m ()
    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
    >>= Bool -> ParsecT [Content] PState m ()
    forall (f :: * -> *). Alternative f => Bool -> f ()
    guard (Bool -> ParsecT [Content] PState m ())
    -> (Bool -> Bool) -> Bool -> ParsecT [Content] PState m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Bool -> Bool
    not
          body <- Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
    forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier Val -> P m (Seq Content)
    getInlineBody Map Identifier Val
    fields ParsecT [Content] PState m (Seq Content)
    -> (Seq Content -> P m Inlines) -> P m Inlines
    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
    >>= P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines
          pure $ B.doubleQuoted $ B.trimInlines body)
      ,(Identifier
    "link", \Maybe Text
    _ Map Identifier Val
    fields -> do
          dest <- 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
    "dest" Map Identifier Val
    fields
          src <- case dest of
            VString Text
    t -> Text -> ParsecT [Content] PState m Text
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Text
    t
            VLabel Text
    t -> Text -> ParsecT [Content] PState m Text
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Text -> ParsecT [Content] PState m Text)
    -> Text -> ParsecT [Content] PState m Text
    forall a b. (a -> b) -> a -> b
    $ Text
    "#" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    t
            VDict OMap Identifier Val
    _ -> do
              Text -> ParsecT [Content] PState m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored Text
    "link to location, linking to #"
              Text -> ParsecT [Content] PState m Text
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Text
    "#"
            Val
    _ -> SourceName -> ParsecT [Content] PState m Text
    forall a. SourceName -> ParsecT [Content] PState m a
    forall (m :: * -> *) a. MonadFail m => SourceName -> m a
    fail (SourceName -> ParsecT [Content] PState m Text)
    -> SourceName -> ParsecT [Content] PState m Text
    forall a b. (a -> b) -> a -> b
    $ SourceName
    "Expected string or label for dest"
          body <- getField "body" fields
          description <-
            if null body
              then
                pure $
                  B.text $
                    if "mailto:" `T.isPrefixOf` src
                      then T.drop 7 src
                      else
                        if "tel:" `T.isPrefixOf` src
                          then T.drop 4 src
                          else src
              else pWithContents pInlines body <|>
                   pWithContents
                    (B.fromList . blocksToInlines . B.toList <$> pBlocks) body
          pure $ B.link src "" description)
      ,(Identifier
    "image", \Maybe Text
    _ Map Identifier Val
    fields -> do
          path <- 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
    "source" Map Identifier Val
    fields ParsecT [Content] PState m Text
    -> ParsecT [Content] PState m Text
    -> ParsecT [Content] PState m Text
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> 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
    "path" Map Identifier Val
    fields
          alt <- (B.text <$> getField "alt" fields) `mplus` pure mempty
          (mbwidth :: Maybe Text) <-
            fmap (renderLength False) <$> getField "width" fields
          (mbheight :: Maybe Text) <-
            fmap (renderLength False) <$> getField "height" fields
          let attr =
                ( Text
    "",
                  [],
                  [(Text, Text)]
    -> (Text -> [(Text, Text)]) -> Maybe Text -> [(Text, Text)]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [(Text
    "width", Text
    x)]) Maybe Text
    mbwidth
                    [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
    forall a. [a] -> [a] -> [a]
    ++ [(Text, Text)]
    -> (Text -> [(Text, Text)]) -> Maybe Text -> [(Text, Text)]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [(Text
    "height", Text
    x)]) Maybe Text
    mbheight
                )
          pure $ B.imageWith attr path "" alt)
      ,(Identifier
    "box", \Maybe Text
    _ 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
          B.spanWith ("", ["box"], []) <$> pWithContents pInlines body)
      ,(Identifier
    "h", \Maybe Text
    _ 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 ParsecT [Content] PState m Length
    -> ParsecT [Content] PState m Length
    -> ParsecT [Content] PState m Length
    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` Length -> ParsecT [Content] PState m Length
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Double -> LUnit -> Length
    LExact Double
    1 LUnit
    LEm)
          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 $ B.text $ getSpaceChars em)
      ,(Identifier
    "place", \Maybe Text
    _ Map Identifier Val
    fields -> do
          Text -> ParsecT [Content] PState m ()
    forall (m :: * -> *). PandocMonad m => Text -> P m ()
    ignored Text
    "parameters of place"
          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 Inlines) -> P m Inlines
    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
    >>= P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines)
      ,(Identifier
    "align", \Maybe Text
    _ Map Identifier Val
    fields -> do
          alignment <- 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
    "alignment" Map Identifier Val
    fields
          B.spanWith ("", [], [("align", repr alignment)])
            <$> (getField "body" fields >>= pWithContents pInlines))
      ,(Identifier
    "sys.version", \Maybe Text
    _ Map Identifier Val
    _ -> Inlines -> P m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines
    B.text Text
    "typst-hs")
      ,(Identifier
    "math.equation", \Maybe Text
    _ 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
          display <- getField "block" fields
          (if display then B.displayMath else B.math) . writeTeX <$> pMathMany body)
      ,(Identifier
    "pad", \Maybe Text
    _ Map Identifier Val
    fields ->  -- ignore paddingy
          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 Inlines) -> P m Inlines
    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
    >>= P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines)
      ,(Identifier
    "block", \Maybe Text
    mbident Map Identifier Val
    fields ->
          (Inlines -> Inlines)
    -> (Text -> Inlines -> Inlines) -> Maybe Text -> Inlines -> Inlines
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Inlines -> Inlines
    forall a. a -> a
    id (\Text
    ident -> Attr -> Inlines -> Inlines
    B.spanWith (Text
    ident, [], [])) Maybe Text
    mbident
            (Inlines -> Inlines) -> P m Inlines -> P m Inlines
    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 Inlines) -> P m Inlines
    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
    >>= P m Inlines -> Seq Content -> P m Inlines
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines))
      ]

    getInlineBody :: PandocMonad m => M.Map Identifier Val -> P m (Seq Content)
    getInlineBody :: forall (m :: * -> *).
    PandocMonad m =>
    Map Identifier Val -> P m (Seq Content)
    getInlineBody Map Identifier Val
    fields =
      Seq Content -> Seq Content
    parbreaksToLinebreaks (Seq Content -> Seq Content)
    -> ParsecT [Content] PState m (Seq Content)
    -> 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 (Seq Content)
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "body" Map Identifier Val
    fields

    parbreaksToLinebreaks :: Seq Content -> Seq Content
    parbreaksToLinebreaks :: Seq Content -> Seq Content
    parbreaksToLinebreaks =
      (Content -> Content) -> Seq Content -> Seq Content
    forall a b. (a -> b) -> Seq a -> Seq b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Content -> Content
    go (Seq Content -> Seq Content)
    -> (Seq Content -> Seq Content) -> Seq Content -> Seq Content
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Content -> Bool) -> Seq Content -> Seq Content
    forall a. (a -> Bool) -> Seq a -> Seq a
    Seq.dropWhileL Content -> Bool
    isParbreak (Seq Content -> Seq Content)
    -> (Seq Content -> Seq Content) -> Seq Content -> Seq Content
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Content -> Bool) -> Seq Content -> Seq Content
    forall a. (a -> Bool) -> Seq a -> Seq a
    Seq.dropWhileR Content -> Bool
    isParbreak
     where
       go :: Content -> Content
    go (Elt Identifier
    "parbreak" Maybe SourcePos
    pos Map Identifier Val
    _) = Identifier -> Maybe SourcePos -> Map Identifier Val -> Content
    Elt Identifier
    "linebreak" Maybe SourcePos
    pos Map Identifier Val
    forall a. Monoid a => a
    mempty
       go Content
    x = Content
    x
       isParbreak :: Content -> Bool
    isParbreak (Elt Identifier
    "parbreak" Maybe SourcePos
    _ Map Identifier Val
    _) = Bool
    True
       isParbreak Content
    _ = Bool
    False

    pPara :: PandocMonad m => P m B.Blocks
    pPara :: forall (m :: * -> *). PandocMonad m => P m Blocks
    pPara = do
      ils <- Inlines -> Inlines
    B.trimInlines (Inlines -> Inlines)
    -> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Inlines -> Inlines
    collapseAdjacentCites (Inlines -> Inlines)
    -> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inlines] -> Inlines
    forall a. Monoid a => [a] -> a
    mconcat ([Inlines] -> Inlines)
    -> ParsecT [Content] PState m [Inlines]
    -> ParsecT [Content] PState m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m [Inlines]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many1 ParsecT [Content] PState m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInline
      optional pParBreak
      pure $ if ils == mempty
             then mempty
             else B.para ils

    pParBreak :: PandocMonad m => P m ()
    pParBreak :: forall (m :: * -> *). PandocMonad m => P m ()
    pParBreak =
      ParsecT [Content] PState m Content -> ParsecT [Content] PState m ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void (ParsecT [Content] PState m Content
     -> ParsecT [Content] PState m ())
    -> ParsecT [Content] PState m Content
    -> ParsecT [Content] PState m ()
    forall a b. (a -> b) -> a -> b
    $
        (Content -> Bool) -> ParsecT [Content] PState m Content
    forall (m :: * -> *).
    PandocMonad m =>
    (Content -> Bool) -> P m Content
    pTok
          ( \case
              Elt Identifier
    "parbreak" Maybe SourcePos
    _ Map Identifier Val
    _ -> Bool
    True
              Content
    _ -> Bool
    False
          )

    pWithContents :: PandocMonad m => P m a -> Seq Content -> P m a
    pWithContents :: forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents P m a
    pa Seq Content
    cs = P m a -> P m a
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (P m a -> P m a) -> P m a -> P m a
    forall a b. (a -> b) -> a -> b
    $ do
      inp <- ParsecT [Content] PState m [Content]
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
      setInput $ F.toList cs
      res <- pa
      eof
      setInput inp
      pure res

    pInlines :: PandocMonad m => P m B.Inlines
    pInlines :: forall (m :: * -> *). PandocMonad m => P m Inlines
    pInlines =
      Inlines -> Inlines -> Inlines
    forall a. Monoid a => a -> a -> a
    mappend (Inlines -> Inlines -> Inlines)
    -> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m (Inlines -> Inlines)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Inlines -> Inlines
    collapseAdjacentCites (Inlines -> Inlines)
    -> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inlines] -> Inlines
    forall a. Monoid a => [a] -> a
    mconcat ([Inlines] -> Inlines)
    -> ParsecT [Content] PState m [Inlines]
    -> ParsecT [Content] PState m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m [Inlines]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many ParsecT [Content] PState m Inlines
    forall (m :: * -> *). PandocMonad m => P m Inlines
    pInline)
              ParsecT [Content] PState m (Inlines -> Inlines)
    -> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m Inlines
    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
    <*> ((Inlines
    B.softbreak Inlines
    -> ParsecT [Content] PState m ()
    -> ParsecT [Content] PState m Inlines
    forall a b.
    a -> ParsecT [Content] PState m b -> ParsecT [Content] PState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ ParsecT [Content] PState m ()
    forall (m :: * -> *). PandocMonad m => P m ()
    pParBreak) ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m Inlines
    -> ParsecT [Content] PState m Inlines
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> Inlines -> ParsecT [Content] PState m Inlines
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Inlines
    forall a. Monoid a => a
    mempty)

    collapseAdjacentCites :: B.Inlines -> B.Inlines
    collapseAdjacentCites :: Inlines -> Inlines
    collapseAdjacentCites = [Inline] -> Inlines
    forall a. [a] -> Many a
    B.fromList ([Inline] -> Inlines)
    -> (Inlines -> [Inline]) -> Inlines -> Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Inline -> [Inline] -> [Inline])
    -> [Inline] -> [Inline] -> [Inline]
    forall a b. (a -> b -> b) -> b -> [a] -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Inline -> [Inline] -> [Inline]
    go [] ([Inline] -> [Inline])
    -> (Inlines -> [Inline]) -> Inlines -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Inlines -> [Inline]
    forall a. Many a -> [a]
    B.toList
     where
       go :: Inline -> [Inline] -> [Inline]
    go (Cite [Citation]
    cs1 [Inline]
    ils1) (Cite [Citation]
    cs2 [Inline]
    ils2 : [Inline]
    xs) =
         [Citation] -> [Inline] -> Inline
    Cite ([Citation]
    cs1 [Citation] -> [Citation] -> [Citation]
    forall a. [a] -> [a] -> [a]
    ++ [Citation]
    cs2) ([Inline]
    ils1 [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <> [Inline]
    ils2) Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline]
    xs
       go (Cite [Citation]
    cs1 [Inline]
    ils1) (Inline
    Space : Cite [Citation]
    cs2 [Inline]
    ils2 : [Inline]
    xs) =
         [Citation] -> [Inline] -> Inline
    Cite ([Citation]
    cs1 [Citation] -> [Citation] -> [Citation]
    forall a. [a] -> [a] -> [a]
    ++ [Citation]
    cs2) ([Inline]
    ils1 [Inline] -> [Inline] -> [Inline]
    forall a. Semigroup a => a -> a -> a
    <> [Inline]
    ils2) Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline]
    xs
       go Inline
    x [Inline]
    xs = Inline
    xInline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :[Inline]
    xs

    modString :: (Text -> Text) -> B.Inline -> B.Inline
    modString :: (Text -> Text) -> Inline -> Inline
    modString Text -> Text
    f (B.Str Text
    t) = Text -> Inline
    B.Str (Text -> Text
    f Text
    t)
    modString Text -> Text
    _ Inline
    x = Inline
    x

    findLabels :: Seq.Seq Content -> [Text]
    findLabels :: Seq Content -> [Text]
    findLabels = (Content -> [Text] -> [Text]) -> [Text] -> Seq Content -> [Text]
    forall a b. (a -> b -> b) -> b -> Seq a -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Content -> [Text] -> [Text]
    go []
     where
       go :: Content -> [Text] -> [Text]
    go (Txt{}) = [Text] -> [Text]
    forall a. a -> a
    id
       go (Lab Text
    t) = (Text
    t Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :)
       go (Elt{ eltFields :: Content -> Map Identifier Val
    eltFields = Map Identifier Val
    fs }) = \[Text]
    ts -> (Val -> [Text] -> [Text]) -> [Text] -> Map Identifier Val -> [Text]
    forall a b. (a -> b -> b) -> b -> Map Identifier a -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Val -> [Text] -> [Text]
    go' [Text]
    ts Map Identifier Val
    fs
       go' :: Val -> [Text] -> [Text]
    go' (VContent Seq Content
    cs) = (Seq Content -> [Text]
    findLabels Seq Content
    cs [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++)
       go' Val
    _ = [Text] -> [Text]
    forall a. a -> a
    id

    parseTable :: PandocMonad m
               => Maybe Text -> M.Map Identifier Val -> P m B.Blocks
    parseTable :: forall (m :: * -> *).
    PandocMonad m =>
    Maybe Text -> Map Identifier Val -> P m Blocks
    parseTable Maybe Text
    mbident Map Identifier Val
    fields = do
      children <- Vector (Seq Content) -> [Seq Content]
    forall a. Vector a -> [a]
    V.toList (Vector (Seq Content) -> [Seq Content])
    -> ParsecT [Content] PState m (Vector (Seq Content))
    -> 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 (Seq Content))
    forall (m :: * -> *) a.
    (MonadFail m, MonadPlus m, FromVal a) =>
    Identifier -> Map Identifier Val -> m a
    getField Identifier
    "children" Map Identifier Val
    fields
      (columns :: Val) <- getField "columns" fields
      let toWidth (VFraction Double
    f) = a -> Maybe a
    forall a. a -> Maybe a
    Just (Double -> a
    forall b. Integral b => Double -> b
    forall a b. (RealFrac a, Integral b) => a -> b
    floor (Double -> a) -> Double -> a
    forall a b. (a -> b) -> a -> b
    $ Double
    1000 Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    f)
          toWidth Val
    _ = Maybe a
    forall a. Maybe a
    Nothing
      let normalizeWidths [Maybe Int]
    xs =
            let givenwidths :: [Int]
    givenwidths = [Maybe Int] -> [Int]
    forall a. [Maybe a] -> [a]
    catMaybes [Maybe Int]
    xs
                (Int
    totgivenwidth :: Int) = [Int] -> Int
    forall a. Num a => [a] -> a
    forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
    sum [Int]
    givenwidths
                avgwidth :: Int
    avgwidth = Int
    totgivenwidth Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `div` [Int] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Int]
    givenwidths
                totwidth :: Int
    totwidth = Int
    avgwidth Int -> Int -> Int
    forall a. Num a => a -> a -> a
    * [Maybe Int] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Maybe Int]
    xs
             in if [Int] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Int]
    givenwidths
                  then Int -> ColWidth -> [ColWidth]
    forall a. Int -> a -> [a]
    replicate ([Maybe Int] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Maybe Int]
    xs) ColWidth
    B.ColWidthDefault
                  else
                    (Maybe Int -> ColWidth) -> [Maybe Int] -> [ColWidth]
    forall a b. (a -> b) -> [a] -> [b]
    map
                      ( \case
                          Just Int
    x ->
                            Double -> ColWidth
    B.ColWidth (Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    totwidth)
                          Maybe Int
    Nothing ->
                            Double -> ColWidth
    B.ColWidth
                            (Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    avgwidth Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    totwidth)
                      )
                      [Maybe Int]
    xs
      widths <- case columns of
        VInteger Integer
    x -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ([ColWidth] -> ParsecT [Content] PState m [ColWidth])
    -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
    forall a b. (a -> b) -> a -> b
    $ Int -> ColWidth -> [ColWidth]
    forall a. Int -> a -> [a]
    replicate (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    x) ColWidth
    B.ColWidthDefault
        VArray Vector Val
    x -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ([ColWidth] -> ParsecT [Content] PState m [ColWidth])
    -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
    forall a b. (a -> b) -> a -> b
    $ [Maybe Int] -> [ColWidth]
    normalizeWidths ([Maybe Int] -> [ColWidth]) -> [Maybe Int] -> [ColWidth]
    forall a b. (a -> b) -> a -> b
    $ (Val -> Maybe Int) -> [Val] -> [Maybe Int]
    forall a b. (a -> b) -> [a] -> [b]
    map Val -> Maybe Int
    forall {a}. Integral a => Val -> Maybe a
    toWidth (Vector Val -> [Val]
    forall a. Vector a -> [a]
    V.toList Vector Val
    x)
        Val
    VNone -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure [Item [ColWidth]
    ColWidth
    B.ColWidthDefault]
        Val
    _ -> SourceName -> ParsecT [Content] PState m [ColWidth]
    forall a. SourceName -> ParsecT [Content] PState m a
    forall (m :: * -> *) a. MonadFail m => SourceName -> m a
    fail (SourceName -> ParsecT [Content] PState m [ColWidth])
    -> SourceName -> ParsecT [Content] PState m [ColWidth]
    forall a b. (a -> b) -> a -> b
    $ SourceName
    "Could not determine number of columns: " SourceName -> SourceName -> SourceName
    forall a. Semigroup a => a -> a -> a
    <> Val -> SourceName
    forall a. Show a => a -> SourceName
    show Val
    columns
      let numcols = [ColWidth] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [ColWidth]
    widths
      align <- getField "align" fields
      let toAlign (VAlignment (Just Horiz
    horiz) Maybe Vert
    _) =
            case Horiz
    horiz of
              Horiz
    HorizStart -> Alignment
    B.AlignLeft
              Horiz
    HorizLeft -> Alignment
    B.AlignLeft
              Horiz
    HorizEnd -> Alignment
    B.AlignRight
              Horiz
    HorizRight -> Alignment
    B.AlignRight
              Horiz
    HorizCenter -> Alignment
    B.AlignCenter
          toAlign Val
    _ = Alignment
    B.AlignDefault
      aligns <-
        case align of
          VAlignment {} -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
    -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a b. (a -> b) -> a -> b
    $ Int -> Alignment -> [Alignment]
    forall a. Int -> a -> [a]
    replicate Int
    numcols (Val -> Alignment
    toAlign Val
    align)
          VArray Vector Val
    v -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
    -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a b. (a -> b) -> a -> b
    $ (Val -> Alignment) -> [Val] -> [Alignment]
    forall a b. (a -> b) -> [a] -> [b]
    map Val -> Alignment
    toAlign (Vector Val -> [Val]
    forall a. Vector a -> [a]
    V.toList Vector Val
    v)
          VFunction Maybe Identifier
    _ Map Identifier Val
    _ Function
    f -> do
            (Integer -> ParsecT [Content] PState m Alignment)
    -> [Integer] -> ParsecT [Content] PState m [Alignment]
    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
              ( \Integer
    colnum -> case Function -> [Val] -> Attempt Val
    applyPureFunction
                  Function
    f
                  [Integer -> Val
    VInteger Integer
    colnum, Integer -> Val
    VInteger Integer
    0] of
                  Success Val
    x -> Alignment -> ParsecT [Content] PState m Alignment
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Alignment -> ParsecT [Content] PState m Alignment)
    -> Alignment -> ParsecT [Content] PState m Alignment
    forall a b. (a -> b) -> a -> b
    $ Val -> Alignment
    toAlign Val
    x
                  Failure SourceName
    e -> SourceName -> ParsecT [Content] PState m Alignment
    forall a. SourceName -> ParsecT [Content] PState m a
    forall (m :: * -> *) a. MonadFail m => SourceName -> m a
    fail SourceName
    e
              )
              [Integer
    Item [Integer]
    0 .. (Int -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    numcols Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1)]
          Val
    _ -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
    -> [Alignment] -> ParsecT [Content] PState m [Alignment]
    forall a b. (a -> b) -> a -> b
    $ Int -> Alignment -> [Alignment]
    forall a. Int -> a -> [a]
    replicate Int
    numcols Alignment
    B.AlignDefault
      let colspecs = [Alignment] -> [ColWidth] -> [ColSpec]
    forall a b. [a] -> [b] -> [(a, b)]
    zip ([Alignment]
    aligns [Alignment] -> [Alignment] -> [Alignment]
    forall a. [a] -> [a] -> [a]
    ++ Alignment -> [Alignment]
    forall a. a -> [a]
    repeat Alignment
    B.AlignDefault) [ColWidth]
    widths
      let addCell' Cell
    cell Maybe ([Int], [[Cell]])
    Nothing = Cell -> Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    addCell' Cell
    cell (([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    forall a. a -> Maybe a
    Just ([], []))
          addCell' cell :: Cell
    cell@(B.Cell Attr
    _ Alignment
    _ (B.RowSpan Int
    rowspan) (B.ColSpan Int
    colspan) [Block]
    _)
           (Just ([Int]
    freecols, [[Cell]]
    revrows))  =
            let freecols' :: [Int]
    freecols' =
                  case (Int
    rowspan Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - [Int] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Int]
    freecols of
                    Int
    n | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0 -> [Int]
    freecols
                      | Bool
    otherwise -> [Int]
    freecols [Int] -> [Int] -> [Int]
    forall a. [a] -> [a] -> [a]
    ++ Int -> Int -> [Int]
    forall a. Int -> a -> [a]
    replicate Int
    n Int
    numcols
            in case [Int]
    freecols' of
                 [] -> -- should not happen
                       SourceName -> Maybe ([Int], [[Cell]])
    forall a. HasCallStack => SourceName -> a
    error SourceName
    "empty freecols'"
                 Int
    x:[Int]
    xs
                   | Int
    colspan Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    x -- there is room on current row
                     -> let ([Int]
    as, [Int]
    bs) = Int -> [Int] -> ([Int], [Int])
    forall a. Int -> [a] -> ([a], [a])
    splitAt Int
    rowspan (Int
    xInt -> [Int] -> [Int]
    forall a. a -> [a] -> [a]
    :[Int]
    xs)
                        in  ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    forall a. a -> Maybe a
    Just
                            ((Int -> Int) -> [Int] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map (\Int
    z -> Int
    z Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    colspan) [Int]
    as [Int] -> [Int] -> [Int]
    forall a. [a] -> [a] -> [a]
    ++ [Int]
    bs,
                               case [[Cell]]
    revrows of
                                 [] -> [[Item [Cell]
    Cell
    cell]]
                                 [Cell]
    r:[[Cell]]
    rs -> (Cell
    cellCell -> [Cell] -> [Cell]
    forall a. a -> [a] -> [a]
    :[Cell]
    r)[Cell] -> [[Cell]] -> [[Cell]]
    forall a. a -> [a] -> [a]
    :[[Cell]]
    rs)
                   | Bool
    otherwise ->
                        let ([Int]
    as, [Int]
    bs) = Int -> [Int] -> ([Int], [Int])
    forall a. Int -> [a] -> ([a], [a])
    splitAt Int
    rowspan [Int]
    xs
                        in  ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    forall a. a -> Maybe a
    Just ((Int -> Int) -> [Int] -> [Int]
    forall a b. (a -> b) -> [a] -> [b]
    map (\Int
    z -> Int
    z Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    colspan) [Int]
    as [Int] -> [Int] -> [Int]
    forall a. [a] -> [a] -> [a]
    ++ [Int]
    bs, [Item [Cell]
    Cell
    cell][Cell] -> [[Cell]] -> [[Cell]]
    forall a. a -> [a] -> [a]
    :[[Cell]]
    revrows)
      let addCell TableSection
    tableSection Cell
    cell (TableData Map TableSection ([Int], [[Cell]])
    tdata) =
            Map TableSection ([Int], [[Cell]]) -> TableData
    TableData ((Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]]))
    -> TableSection
    -> Map TableSection ([Int], [[Cell]])
    -> Map TableSection ([Int], [[Cell]])
    forall k a.
    Ord k =>
    (Maybe a -> Maybe a) -> k -> Map k a -> Map k a
    M.alter (Cell -> Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    addCell' Cell
    cell) TableSection
    tableSection Map TableSection ([Int], [[Cell]])
    tdata)
      let toCell TableSection
    tableSection TableData
    tableData Seq Content
    contents = do
            case Seq Content
    contents of
              [Elt (Identifier Text
    "grid.cell") Maybe SourcePos
    _pos Map Identifier Val
    fs] -> do
                bs <- Blocks -> [Block]
    forall a. Many a -> [a]
    B.toList (Blocks -> [Block])
    -> ParsecT [Content] PState m Blocks
    -> ParsecT [Content] PState m [Block]
    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
    fs ParsecT [Content] PState m (Seq Content)
    -> (Seq Content -> ParsecT [Content] PState m Blocks)
    -> ParsecT [Content] PState m Blocks
    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
    >>= ParsecT [Content] PState m Blocks
    -> Seq Content -> ParsecT [Content] PState m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents ParsecT [Content] PState m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks)
                rowspan <- getField "rowspan" fs <|> pure 1
                colspan <- getField "colspan" fs <|> pure 1
                align' <- (toAlign <$> getField "align" fs) <|> pure B.AlignDefault
                pure $ addCell tableSection
                  (B.Cell B.nullAttr align' (B.RowSpan rowspan)
                    (B.ColSpan colspan) bs) tableData
              [Elt (Identifier Text
    "table.cell") Maybe SourcePos
    pos Map Identifier Val
    fs] ->
                TableSection
    -> TableData -> Seq Content -> ParsecT [Content] PState m TableData
    toCell TableSection
    tableSection TableData
    tableData [Identifier -> Maybe SourcePos -> Map Identifier Val -> Content
    Elt (Text -> Identifier
    Identifier Text
    "grid.cell") Maybe SourcePos
    pos Map Identifier Val
    fs]
              [Elt (Identifier Text
    "table.vline") Maybe SourcePos
    _pos Map Identifier Val
    _fs] -> TableData -> ParsecT [Content] PState m TableData
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure TableData
    tableData
              [Elt (Identifier Text
    "table.hline") Maybe SourcePos
    _pos Map Identifier Val
    _fs] -> TableData -> ParsecT [Content] PState m TableData
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure TableData
    tableData
              [Elt (Identifier Text
    "grid.vline") Maybe SourcePos
    _pos Map Identifier Val
    _fs] -> TableData -> ParsecT [Content] PState m TableData
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure TableData
    tableData
              [Elt (Identifier Text
    "grid.hline") Maybe SourcePos
    _pos Map Identifier Val
    _fs] -> TableData -> ParsecT [Content] PState m TableData
    forall a. a -> ParsecT [Content] PState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure TableData
    tableData
              [Elt (Identifier Text
    "table.header") Maybe SourcePos
    _pos Map Identifier Val
    fs] ->
                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
    fs ParsecT [Content] PState m (Vector (Seq Content))
    -> (Vector (Seq Content) -> ParsecT [Content] PState m TableData)
    -> ParsecT [Content] PState m TableData
    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
    >>=
                  (TableData -> Seq Content -> ParsecT [Content] PState m TableData)
    -> TableData
    -> [Seq Content]
    -> ParsecT [Content] PState m TableData
    forall (t :: * -> *) (m :: * -> *) b a.
    (Foldable t, Monad m) =>
    (b -> a -> m b) -> b -> t a -> m b
    foldM (TableSection
    -> TableData -> Seq Content -> ParsecT [Content] PState m TableData
    toCell TableSection
    THeader) TableData
    tableData ([Seq Content] -> ParsecT [Content] PState m TableData)
    -> (Vector (Seq Content) -> [Seq Content])
    -> Vector (Seq Content)
    -> ParsecT [Content] PState m TableData
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Vector (Seq Content) -> [Seq Content]
    forall a. Vector a -> [a]
    V.toList
              [Elt (Identifier Text
    "table.footer") Maybe SourcePos
    _pos Map Identifier Val
    fs] ->
                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
    fs ParsecT [Content] PState m (Vector (Seq Content))
    -> (Vector (Seq Content) -> ParsecT [Content] PState m TableData)
    -> ParsecT [Content] PState m TableData
    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
    >>=
                  (TableData -> Seq Content -> ParsecT [Content] PState m TableData)
    -> TableData
    -> [Seq Content]
    -> ParsecT [Content] PState m TableData
    forall (t :: * -> *) (m :: * -> *) b a.
    (Foldable t, Monad m) =>
    (b -> a -> m b) -> b -> t a -> m b
    foldM (TableSection
    -> TableData -> Seq Content -> ParsecT [Content] PState m TableData
    toCell TableSection
    TFooter) TableData
    tableData ([Seq Content] -> ParsecT [Content] PState m TableData)
    -> (Vector (Seq Content) -> [Seq Content])
    -> Vector (Seq Content)
    -> ParsecT [Content] PState m TableData
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Vector (Seq Content) -> [Seq Content]
    forall a. Vector a -> [a]
    V.toList
              Seq Content
    _ -> do
                bs <- Blocks -> [Block]
    forall a. Many a -> [a]
    B.toList (Blocks -> [Block])
    -> ParsecT [Content] PState m Blocks
    -> ParsecT [Content] PState m [Block]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT [Content] PState m Blocks
    -> Seq Content -> ParsecT [Content] PState m Blocks
    forall (m :: * -> *) a.
    PandocMonad m =>
    P m a -> Seq Content -> P m a
    pWithContents ParsecT [Content] PState m Blocks
    forall (m :: * -> *). PandocMonad m => P m Blocks
    pBlocks Seq Content
    contents
                pure $ addCell tableSection
                  (B.Cell B.nullAttr B.AlignDefault (B.RowSpan 1) (B.ColSpan 1) bs)
                  tableData
      tableData <- foldM (toCell TBody) (TableData mempty) children
      let getRows TableSection
    tablePart = ([Cell] -> Row) -> [[Cell]] -> [Row]
    forall a b. (a -> b) -> [a] -> [b]
    map (Attr -> [Cell] -> Row
    B.Row Attr
    B.nullAttr ([Cell] -> Row) -> ([Cell] -> [Cell]) -> [Cell] -> Row
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Cell] -> [Cell]
    forall a. [a] -> [a]
    reverse)
                              ([[Cell]] -> [Row])
    -> (TableData -> [[Cell]]) -> TableData -> [Row]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [[Cell]]
    -> (([Int], [[Cell]]) -> [[Cell]])
    -> Maybe ([Int], [[Cell]])
    -> [[Cell]]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] ([[Cell]] -> [[Cell]]
    forall a. [a] -> [a]
    reverse ([[Cell]] -> [[Cell]])
    -> (([Int], [[Cell]]) -> [[Cell]]) -> ([Int], [[Cell]]) -> [[Cell]]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([Int], [[Cell]]) -> [[Cell]]
    forall a b. (a, b) -> b
    snd)
                              (Maybe ([Int], [[Cell]]) -> [[Cell]])
    -> (TableData -> Maybe ([Int], [[Cell]])) -> TableData -> [[Cell]]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TableSection
    -> Map TableSection ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup TableSection
    tablePart (Map TableSection ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]]))
    -> (TableData -> Map TableSection ([Int], [[Cell]]))
    -> TableData
    -> Maybe ([Int], [[Cell]])
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TableData -> Map TableSection ([Int], [[Cell]])
    unTableData
      let headRows = TableSection -> TableData -> [Row]
    getRows TableSection
    THeader TableData
    tableData
      let bodyRows = TableSection -> TableData -> [Row]
    getRows TableSection
    TBody TableData
    tableData
      let footRows = TableSection -> TableData -> [Row]
    getRows TableSection
    TFooter TableData
    tableData
      pure $
        B.tableWith
          (fromMaybe "" mbident, [], [])
          (B.Caption mempty mempty)
          colspecs
          (B.TableHead B.nullAttr headRows)
          [B.TableBody B.nullAttr 0 [] bodyRows]
          (B.TableFoot B.nullAttr footRows)

    data TableSection = THeader | TBody | TFooter
      deriving (Int -> TableSection -> SourceName -> SourceName
    [TableSection] -> SourceName -> SourceName
    TableSection -> SourceName
    (Int -> TableSection -> SourceName -> SourceName)
    -> (TableSection -> SourceName)
    -> ([TableSection] -> SourceName -> SourceName)
    -> Show TableSection
    forall a.
    (Int -> a -> SourceName -> SourceName)
    -> (a -> SourceName) -> ([a] -> SourceName -> SourceName) -> Show a
    $cshowsPrec :: Int -> TableSection -> SourceName -> SourceName
    showsPrec :: Int -> TableSection -> SourceName -> SourceName
    $cshow :: TableSection -> SourceName
    show :: TableSection -> SourceName
    $cshowList :: [TableSection] -> SourceName -> SourceName
    showList :: [TableSection] -> SourceName -> SourceName
    Show, Eq TableSection
    Eq TableSection =>
    (TableSection -> TableSection -> Ordering)
    -> (TableSection -> TableSection -> Bool)
    -> (TableSection -> TableSection -> Bool)
    -> (TableSection -> TableSection -> Bool)
    -> (TableSection -> TableSection -> Bool)
    -> (TableSection -> TableSection -> TableSection)
    -> (TableSection -> TableSection -> TableSection)
    -> Ord TableSection
    TableSection -> TableSection -> Bool
    TableSection -> TableSection -> Ordering
    TableSection -> TableSection -> TableSection
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: TableSection -> TableSection -> Ordering
    compare :: TableSection -> TableSection -> Ordering
    $c< :: TableSection -> TableSection -> Bool
    < :: TableSection -> TableSection -> Bool
    $c<= :: TableSection -> TableSection -> Bool
    <= :: TableSection -> TableSection -> Bool
    $c> :: TableSection -> TableSection -> Bool
    > :: TableSection -> TableSection -> Bool
    $c>= :: TableSection -> TableSection -> Bool
    >= :: TableSection -> TableSection -> Bool
    $cmax :: TableSection -> TableSection -> TableSection
    max :: TableSection -> TableSection -> TableSection
    $cmin :: TableSection -> TableSection -> TableSection
    min :: TableSection -> TableSection -> TableSection
    Ord, TableSection -> TableSection -> Bool
    (TableSection -> TableSection -> Bool)
    -> (TableSection -> TableSection -> Bool) -> Eq TableSection
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: TableSection -> TableSection -> Bool
    == :: TableSection -> TableSection -> Bool
    $c/= :: TableSection -> TableSection -> Bool
    /= :: TableSection -> TableSection -> Bool
    Eq)

    newtype TableData =
      TableData { TableData -> Map TableSection ([Int], [[Cell]])
    unTableData :: M.Map TableSection ([Int], [[Cell]]) }
      deriving (Int -> TableData -> SourceName -> SourceName
    [TableData] -> SourceName -> SourceName
    TableData -> SourceName
    (Int -> TableData -> SourceName -> SourceName)
    -> (TableData -> SourceName)
    -> ([TableData] -> SourceName -> SourceName)
    -> Show TableData
    forall a.
    (Int -> a -> SourceName -> SourceName)
    -> (a -> SourceName) -> ([a] -> SourceName -> SourceName) -> Show a
    $cshowsPrec :: Int -> TableData -> SourceName -> SourceName
    showsPrec :: Int -> TableData -> SourceName -> SourceName
    $cshow :: TableData -> SourceName
    show :: TableData -> SourceName
    $cshowList :: [TableData] -> SourceName -> SourceName
    showList :: [TableData] -> SourceName -> SourceName
    Show)
      -- for each table section, we have a pair
      -- the first element indicates the number of column spaces left
      -- in [currentLine, nextLine, lineAfter, etc.]
      -- the second element is a list of rows, in reverse order,
      -- each of which is a list of cells, in reverse order