    {-# LANGUAGE BangPatterns #-}
    {-# LANGUAGE OverloadedStrings #-}
    module Text.Pandoc.Readers.LaTeX.Table
      ( tableEnvironments )
    where

    import Data.Functor (($>))
    import Text.Pandoc.Class
    import Text.Pandoc.Readers.LaTeX.Parsing
    import Text.Pandoc.TeX
    import Text.Pandoc.Builder as B
    import qualified Data.Map as M
    import Data.Text (Text)
    import Data.Maybe (fromMaybe)
    import qualified Data.Text as T
    import Control.Applicative ((<|>), optional, many)
    import Control.Monad (when, void)
    import Text.Pandoc.Shared (safeRead, trim)
    import Text.Pandoc.Logging (LogMessage(SkippedContent))
    import Text.Pandoc.Walk (walkM)
    import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
                                optional, space, spaces, withRaw, (<|>))

    tableEnvironments :: PandocMonad m
                      => LP m Blocks
                      -> LP m Inlines
                      -> M.Map Text (LP m Blocks)
    tableEnvironments :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Map Text (LP m Blocks)
    tableEnvironments LP m Blocks
    block LP m Inlines
    inline =
      [(Text, LP m Blocks)] -> Map Text (LP m Blocks)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ (Text
    "longtable",  Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "longtable" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $
              LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    resetCaption LP m () -> LP m Blocks -> LP m Blocks
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *>
                LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "longtable" Bool
    False LP m Blocks -> (Blocks -> LP m Blocks) -> LP m Blocks
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Blocks -> LP m Blocks
    forall (m :: * -> *). PandocMonad m => Blocks -> LP m Blocks
    addTableCaption)
      , (Text
    "table",  Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "table" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $
              LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    skipopts LP m () -> LP m () -> LP m ()
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    resetCaption LP m () -> LP m Blocks -> LP m Blocks
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> LP m Blocks
    blocks LP m Blocks -> (Blocks -> LP m Blocks) -> LP m Blocks
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Blocks -> LP m Blocks
    forall (m :: * -> *). PandocMonad m => Blocks -> LP m Blocks
    addTableCaption)
      , (Text
    "tabular*", Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "tabular*" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "tabular*" Bool
    True)
      , (Text
    "tabularx", Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "tabularx" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "tabularx" Bool
    True)
      , (Text
    "tabular", Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "tabular"  (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "tabular" Bool
    False)
      , (Text
    "supertabular", Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "supertabular" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "supertabular" Bool
    False)
      , (Text
    "supertabular*", Text -> LP m Blocks -> LP m Blocks
    forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
    env Text
    "supertabular*" (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    "supertabular*" Bool
    False)
      ]
     where
       blocks :: LP m Blocks
    blocks = [Blocks] -> Blocks
    forall a. Monoid a => [a] -> a
    mconcat ([Blocks] -> Blocks)
    -> ParsecT TokStream LaTeXState m [Blocks] -> LP m Blocks
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Blocks -> ParsecT TokStream LaTeXState m [Blocks]
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many LP m Blocks
    block

    hline :: PandocMonad m => LP m ()
    hline :: forall (m :: * -> *). PandocMonad m => LP m ()
    hline = ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT TokStream LaTeXState m ()
     -> ParsecT TokStream LaTeXState m ())
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ do
      ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces
      hasParenArg <-
          (Bool
    False Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "hline") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    False Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "cline") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "cmidrule") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          -- booktabs rules:
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "toprule") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "bottomrule") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "midrule") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "endhead") ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    -> ParsecT TokStream LaTeXState m Bool
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|>
          (Bool
    True Bool
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Bool
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Text -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "endfirsthead")
      optional rawopt
      when hasParenArg $ void $ optional (parenWrapped (() <$ singleChar))

    lbreak :: PandocMonad m => LP m Tok
    lbreak :: forall (m :: * -> *). PandocMonad m => LP m Tok
    lbreak = (Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "\\" LP m Tok -> LP m Tok -> LP m Tok
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "tabularnewline")
             LP m Tok -> ParsecT TokStream LaTeXState m (Maybe ()) -> LP m Tok
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m (Maybe ())
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void ParsecT TokStream LaTeXState m Text
    forall (m :: * -> *). PandocMonad m => LP m Text
    rawopt) LP m Tok -> ParsecT TokStream LaTeXState m () -> LP m Tok
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces

    amp :: PandocMonad m => LP m Tok
    amp :: forall (m :: * -> *). PandocMonad m => LP m Tok
    amp = Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '&'

    -- Split a Word into individual Symbols (for parseAligns)
    splitWordTok :: PandocMonad m => LP m ()
    splitWordTok :: forall (m :: * -> *). PandocMonad m => LP m ()
    splitWordTok = do
      TokStream macrosExpanded inp <- ParsecT TokStream LaTeXState m TokStream
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
      case inp of
           (Tok SourcePos
    spos TokType
    Word Text
    t : [Tok]
    rest) ->
             TokStream -> ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
    setInput (TokStream -> ParsecT TokStream LaTeXState m ())
    -> TokStream -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Bool -> [Tok] -> TokStream
    TokStream Bool
    macrosExpanded
                      ([Tok] -> TokStream) -> [Tok] -> TokStream
    forall a b. (a -> b) -> a -> b
    $ (Char -> Tok) -> String -> [Tok]
    forall a b. (a -> b) -> [a] -> [b]
    map (SourcePos -> TokType -> Text -> Tok
    Tok SourcePos
    spos TokType
    Symbol (Text -> Tok) -> (Char -> Text) -> Char -> Tok
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Char -> Text
    T.singleton) (Text -> String
    T.unpack Text
    t) [Tok] -> [Tok] -> [Tok]
    forall a. Semigroup a => a -> a -> a
    <> [Tok]
    rest
           [Tok]
    _ -> () -> ParsecT TokStream LaTeXState m ()
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()

    parseAligns :: PandocMonad m => LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
    parseAligns :: forall (m :: * -> *).
    PandocMonad m =>
    LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
    parseAligns = ParsecT
      TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
    -> ParsecT
         TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT
       TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
     -> ParsecT
          TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))])
    -> ParsecT
         TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
    -> ParsecT
         TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
    forall a b. (a -> b) -> a -> b
    $ do
      let maybeBar :: ParsecT TokStream LaTeXState m ()
    maybeBar = ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany
            (ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT TokStream LaTeXState m ()
     -> ParsecT TokStream LaTeXState m ())
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    sp ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> (() ()
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m ()
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '|' ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> () ()
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m ()
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ (Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '@' ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced)))
      let cAlign :: ParsecT TokStream LaTeXState m Alignment
    cAlign = Alignment
    AlignCenter Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'c'
      let lAlign :: ParsecT TokStream LaTeXState m Alignment
    lAlign = Alignment
    AlignLeft Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'l'
      let rAlign :: ParsecT TokStream LaTeXState m Alignment
    rAlign = Alignment
    AlignRight Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'r'
      let parAlign :: ParsecT TokStream LaTeXState m Alignment
    parAlign = Alignment
    AlignLeft Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'p'
      -- aligns from tabularx
      let xAlign :: ParsecT TokStream LaTeXState m Alignment
    xAlign = Alignment
    AlignLeft Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'X'
      let mAlign :: ParsecT TokStream LaTeXState m Alignment
    mAlign = Alignment
    AlignLeft Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'm'
      let bAlign :: ParsecT TokStream LaTeXState m Alignment
    bAlign = Alignment
    AlignLeft Alignment
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    'b'
      let alignChar :: ParsecT TokStream LaTeXState m Alignment
    alignChar = ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    splitWordTok ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> (  ParsecT TokStream LaTeXState m Alignment
    cAlign ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    lAlign ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    rAlign ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    parAlign
                                     ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    xAlign ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    mAlign ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ParsecT TokStream LaTeXState m Alignment
    bAlign )
      let alignPrefix :: ParsecT TokStream LaTeXState m [Tok]
    alignPrefix = Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '>' ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced
      let alignSuffix :: ParsecT TokStream LaTeXState m [Tok]
    alignSuffix = Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '<' ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced
      let colWidth :: ParsecT TokStream LaTeXState m (Maybe Double)
    colWidth = ParsecT TokStream LaTeXState m (Maybe Double)
    -> ParsecT TokStream LaTeXState m (Maybe Double)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT TokStream LaTeXState m (Maybe Double)
     -> ParsecT TokStream LaTeXState m (Maybe Double))
    -> ParsecT TokStream LaTeXState m (Maybe Double)
    -> ParsecT TokStream LaTeXState m (Maybe Double)
    forall a b. (a -> b) -> a -> b
    $ do
            ts <- ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced
            let isLinewidth (Tok SourcePos
    _ (CtrlSeq Text
    "linewidth") Text
    _) = Bool
    True
                isLinewidth Tok
    _ = Bool
    False
            case break isLinewidth ts of
              ([Tok]
    ds, Tok
    _:[Tok]
    _) -> Maybe Double -> ParsecT TokStream LaTeXState m (Maybe Double)
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Maybe Double -> ParsecT TokStream LaTeXState m (Maybe Double))
    -> Maybe Double -> ParsecT TokStream LaTeXState m (Maybe Double)
    forall a b. (a -> b) -> a -> b
    $ Text -> Maybe Double
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead (Text -> Maybe Double) -> Text -> Maybe Double
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    trim (Text -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ [Tok] -> Text
    untokenize [Tok]
    ds
              ([Tok], [Tok])
    _ -> Maybe Double -> ParsecT TokStream LaTeXState m (Maybe Double)
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe Double
    forall a. Maybe a
    Nothing
      let alignSpec :: ParsecT
      TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    alignSpec = do
            pref <- [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m [Tok]
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option [] ParsecT TokStream LaTeXState m [Tok]
    alignPrefix
            spaces
            al <- alignChar
            width <- colWidth <|> option Nothing (do s <- untokenize <$> braced
                                                     pos <- getPosition
                                                     report $ SkippedContent s pos
                                                     return Nothing)
            spaces
            suff <- option [] alignSuffix
            return (al, width, (pref, suff))
      let starAlign :: ParsecT TokStream LaTeXState m ()
    starAlign = do -- '*{2}{r}' == 'rr', we just expand like a macro
            Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '*'
            ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces
            ds <- Text -> Text
    trim (Text -> Text) -> ([Tok] -> Text) -> [Tok] -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize ([Tok] -> Text)
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    bracedOrToken
            spaces
            spec <- bracedOrToken
            case safeRead ds of
                 Just Int
    n  -> do
                   TokStream _ ts <- ParsecT TokStream LaTeXState m TokStream
    forall (m :: * -> *) s u. Monad m => ParsecT s u m s
    getInput
                   setInput $ TokStream False (mconcat (replicate n spec) ++ ts)
                 Maybe Int
    Nothing -> String -> ParsecT TokStream LaTeXState m ()
    forall a. String -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    Prelude.fail (String -> ParsecT TokStream LaTeXState m ())
    -> String -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ String
    "Could not parse " String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> Text -> String
    T.unpack Text
    ds String -> String -> String
    forall a. Semigroup a => a -> a -> a
    <> String
    " as number"
      ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    bgroup
      ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces
      ParsecT TokStream LaTeXState m ()
    maybeBar
      aligns' <- ParsecT
      TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT
         TokStream LaTeXState m [(Alignment, Maybe Double, ([Tok], [Tok]))]
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many (ParsecT
       TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
     -> ParsecT
          TokStream LaTeXState m [(Alignment, Maybe Double, ([Tok], [Tok]))])
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT
         TokStream LaTeXState m [(Alignment, Maybe Double, ([Tok], [Tok]))]
    forall a b. (a -> b) -> a -> b
    $ ParsecT
      TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT
       TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
     -> ParsecT
          TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok])))
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    forall a b. (a -> b) -> a -> b
    $ ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m (Maybe ())
    -> ParsecT TokStream LaTeXState m (Maybe ())
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m (Maybe ())
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional ParsecT TokStream LaTeXState m ()
    starAlign ParsecT TokStream LaTeXState m (Maybe ())
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >>
                                (ParsecT
      TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    alignSpec ParsecT
      TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT
         TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m ()
    maybeBar)
      spaces
      egroup
      spaces
      return $ map toSpec aligns'
      where
        toColWidth :: Maybe Double -> ColWidth
    toColWidth (Just Double
    w) | Double
    w Double -> Double -> Bool
    forall a. Ord a => a -> a -> Bool
    > Double
    0 = Double -> ColWidth
    ColWidth Double
    w
        toColWidth Maybe Double
    _                = ColWidth
    ColWidthDefault
        toSpec :: (a, Maybe Double, c) -> (a, ColWidth, c)
    toSpec (a
    x, Maybe Double
    y, c
    z) = (a
    x, Maybe Double -> ColWidth
    toColWidth Maybe Double
    y, c
    z)

    -- N.B. this parser returns a Row that may have erroneous empty cells
    -- in it. See the note above fixTableHead for details.
    parseTableRow :: PandocMonad m
                  => LP m Blocks -- ^ block parser
                  -> LP m Inlines -- ^ inline parser
                  -> Text   -- ^ table environment name
                  -> [([Tok], [Tok])] -- ^ pref/suffixes
                  -> LP m Row
    parseTableRow :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> [([Tok], [Tok])] -> LP m Row
    parseTableRow LP m Blocks
    block LP m Inlines
    inline Text
    envname [([Tok], [Tok])]
    prefsufs = do
      ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall s (m :: * -> *) t a u.
    (Stream s m t, Show a) =>
    ParsecT s u m a -> ParsecT s u m ()
    notFollowedBy (ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> Text -> ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => Text -> LP m ()
    end_ Text
    envname)
      -- contexts that can contain & that is not colsep:
      let canContainAmp :: Tok -> Bool
    canContainAmp (Tok SourcePos
    _ (CtrlSeq Text
    "begin") Text
    _) = Bool
    True
          canContainAmp (Tok SourcePos
    _ (CtrlSeq Text
    "verb") Text
    _)  = Bool
    True
          canContainAmp (Tok SourcePos
    _ (CtrlSeq Text
    "Verb") Text
    _)  = Bool
    True
          canContainAmp Tok
    _       = Bool
    False
      -- add prefixes and suffixes in token stream:
      let celltoks :: ([Tok], [Tok]) -> ParsecT TokStream LaTeXState m [Tok]
    celltoks ([Tok]
    pref, [Tok]
    suff) = do
            prefpos <- ParsecT TokStream LaTeXState m SourcePos
    forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
    getPosition
            contents <- mconcat <$>
                many ( snd <$> withRaw
                         ((lookAhead (controlSeq "parbox") >>
                           void block) -- #5711
                          <|>
                          (lookAhead (satisfyTok canContainAmp) >> void inline)
                          <|>
                          (lookAhead (controlSeq "begin") >>
                           void block) -- #4746
                          <|>
                          (lookAhead (symbol '$') >> void inline))
                      <|>
                       (do notFollowedBy
                             (() <$ amp <|> () <$ lbreak <|> end_ envname)
                           count 1 anyTok) )

            suffpos <- getPosition
            option [] (count 1 amp)
            return $ map (setpos prefpos) pref ++ contents ++ map (setpos suffpos) suff
      rawcells <- (([Tok], [Tok]) -> ParsecT TokStream LaTeXState m [Tok])
    -> [([Tok], [Tok])] -> ParsecT TokStream LaTeXState m [[Tok]]
    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 ([Tok], [Tok]) -> ParsecT TokStream LaTeXState m [Tok]
    celltoks [([Tok], [Tok])]
    prefsufs
      cells <- mapM (parseFromToks (parseTableCell block)) rawcells
      spaces
      return $ Row nullAttr cells

    parseTableCell :: PandocMonad m => LP m Blocks -> LP m Cell
    parseTableCell :: forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    parseTableCell LP m Blocks
    block = do
      LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces
      (LaTeXState -> LaTeXState) -> LP m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> LP m ())
    -> (LaTeXState -> LaTeXState) -> LP m ()
    forall a b. (a -> b) -> a -> b
    $ \LaTeXState
    st -> LaTeXState
    st{ sInTableCell = True }
      cell' <-   LP m Blocks -> LP m Cell
    forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    multicolumnCell LP m Blocks
    block
             LP m Cell -> LP m Cell -> LP m Cell
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m Blocks -> LP m Cell
    forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    multirowCell LP m Blocks
    block
             LP m Cell -> LP m Cell -> LP m Cell
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m Cell
    parseSimpleCell
             LP m Cell -> LP m Cell -> LP m Cell
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> LP m Cell
    parseEmptyCell
      updateState $ \LaTeXState
    st -> LaTeXState
    st{ sInTableCell = False }
      spaces
      return cell'
      where
        -- The parsing of empty cells is important in LaTeX, especially when dealing
        -- with multirow/multicolumn. See #6603.
        parseEmptyCell :: LP m Cell
    parseEmptyCell = LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    spaces LP m () -> Cell -> LP m Cell
    forall (f :: * -> *) a b. Functor f => f a -> b -> f b
    $> Cell
    emptyCell
        parseSimpleCell :: LP m Cell
    parseSimpleCell = Blocks -> Cell
    simpleCell (Blocks -> Cell) -> LP m Blocks -> LP m Cell
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> (Blocks -> Blocks
    plainify (Blocks -> Blocks) -> ([Blocks] -> Blocks) -> [Blocks] -> Blocks
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Blocks] -> Blocks
    forall a. Monoid a => [a] -> a
    mconcat ([Blocks] -> Blocks)
    -> ParsecT TokStream LaTeXState m [Blocks] -> LP m Blocks
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Blocks -> ParsecT TokStream LaTeXState m [Blocks]
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many LP m Blocks
    block)

    cellAlignment :: PandocMonad m => LP m Alignment
    cellAlignment :: forall (m :: * -> *). PandocMonad m => LP m Alignment
    cellAlignment = ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany (Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '|') ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> ParsecT TokStream LaTeXState m Alignment
    alignment ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
    skipMany (Char -> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '|')
      where
        alignment :: ParsecT TokStream LaTeXState m Alignment
    alignment = do
          c <- Tok -> Text
    untoken (Tok -> Text)
    -> ParsecT TokStream LaTeXState m Tok
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m Tok
    forall (m :: * -> *). PandocMonad m => LP m Tok
    singleChar ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m (Maybe [Tok])
    -> ParsecT TokStream LaTeXState m Text
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m (Maybe [Tok])
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced -- ignore args
          return $ case c of
            Text
    "l" -> Alignment
    AlignLeft
            Text
    "r" -> Alignment
    AlignRight
            Text
    "c" -> Alignment
    AlignCenter
            Text
    "*" -> Alignment
    AlignDefault
            Text
    _   -> Alignment
    AlignDefault

    plainify :: Blocks -> Blocks
    plainify :: Blocks -> Blocks
    plainify Blocks
    bs = case Blocks -> [Block]
    forall a. Many a -> [a]
    toList Blocks
    bs of
                    [Para [Inline]
    ils] -> Inlines -> Blocks
    plain ([Inline] -> Inlines
    forall a. [a] -> Many a
    fromList [Inline]
    ils)
                    [Block]
    _          -> Blocks
    bs

    multirowCell :: PandocMonad m => LP m Blocks -> LP m Cell
    multirowCell :: forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    multirowCell LP m Blocks
    block = Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "multirow" LP m Tok
    -> ParsecT TokStream LaTeXState m Cell
    -> ParsecT TokStream LaTeXState m Cell
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> do
      -- Full prototype for \multirow macro is:
      --     \multirow[vpos]{nrows}[bigstruts]{width}[vmove]{text}
      -- However, everything except `nrows` and `text` make
      -- sense in the context of the Pandoc AST
      _ <- ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m (Maybe Alignment)
    forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
    optional (ParsecT TokStream LaTeXState m Alignment
     -> ParsecT TokStream LaTeXState m (Maybe Alignment))
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m (Maybe Alignment)
    forall a b. (a -> b) -> a -> b
    $ Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    '[' LP m Tok
    -> ParsecT TokStream LaTeXState m Alignment
    -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> ParsecT TokStream LaTeXState m Alignment
    forall (m :: * -> *). PandocMonad m => LP m Alignment
    cellAlignment ParsecT TokStream LaTeXState m Alignment
    -> LP m Tok -> ParsecT TokStream LaTeXState m Alignment
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* Char -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
    symbol Char
    ']'   -- vertical position
      nrows <- fmap (fromMaybe 1 . safeRead . untokenize) braced
      _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']')  -- bigstrut-related
      _ <- symbol '{' *> manyTill anyTok (symbol '}')             -- Cell width
      _ <- optional $ symbol '[' *> manyTill anyTok (symbol ']')  -- Length used for fine-tuning
      content <- symbol '{' *> (plainify . mconcat <$> many block) <* symbol '}'
      return $ cell AlignDefault (RowSpan nrows) (ColSpan 1) content

    multicolumnCell :: PandocMonad m => LP m Blocks -> LP m Cell
    multicolumnCell :: forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    multicolumnCell LP m Blocks
    block = Text -> LP m Tok
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
    controlSeq Text
    "multicolumn" LP m Tok
    -> ParsecT TokStream LaTeXState m Cell
    -> ParsecT TokStream LaTeXState m Cell
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> do
      span' <- ([Tok] -> Int)
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m Int
    forall a b.
    (a -> b)
    -> ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (Int -> Maybe Int -> Int
    forall a. a -> Maybe a -> a
    fromMaybe Int
    1 (Maybe Int -> Int) -> ([Tok] -> Maybe Int) -> [Tok] -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Int
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead (Text -> Maybe Int) -> ([Tok] -> Text) -> [Tok] -> Maybe Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize) ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced
      alignment <- symbol '{' *> cellAlignment <* symbol '}'

      let singleCell = do
            content <- Blocks -> Blocks
    plainify (Blocks -> Blocks) -> ([Blocks] -> Blocks) -> [Blocks] -> Blocks
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Blocks] -> Blocks
    forall a. Monoid a => [a] -> a
    mconcat ([Blocks] -> Blocks)
    -> ParsecT TokStream LaTeXState m [Blocks] -> LP m Blocks
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Blocks -> ParsecT TokStream LaTeXState m [Blocks]
    forall a.
    ParsecT TokStream LaTeXState m a
    -> ParsecT TokStream LaTeXState m [a]
    forall (f :: * -> *) a. Alternative f => f a -> f [a]
    many LP m Blocks
    block
            return $ cell alignment (RowSpan 1) (ColSpan span') content

      -- Two possible contents: either a \multirow cell, or content.
      -- E.g. \multicol{1}{c}{\multirow{2}{1em}{content}}
      -- Note that a \multirow cell can be nested in a \multicolumn,
      -- but not the other way around. See #6603
      let nestedCell = do
            (Cell _ _ (RowSpan rs) _ bs) <- LP m Blocks -> ParsecT TokStream LaTeXState m Cell
    forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
    multirowCell LP m Blocks
    block
            return $ cell
                      alignment
                      (RowSpan rs)
                      (ColSpan span')
                      (fromList bs)

      symbol '{' *> (nestedCell <|> singleCell) <* symbol '}'

    -- LaTeX tables are stored with empty cells underneath multirow cells
    -- denoting the grid spaces taken up by them. More specifically, if a
    -- cell spans m rows, then it will overwrite all the cells in the
    -- columns it spans for (m-1) rows underneath it, requiring padding
    -- cells in these places. These padding cells need to be removed for
    -- proper table reading. See #6603.
    --
    -- These fixTable functions do not otherwise fix up malformed
    -- input tables: that is left to the table builder.
    fixTableHead :: TableHead -> TableHead
    fixTableHead :: TableHead -> TableHead
    fixTableHead (TableHead Attr
    attr [Row]
    rows) = Attr -> [Row] -> TableHead
    TableHead Attr
    attr [Row]
    rows'
      where
        rows' :: [Row]
    rows' = [Row] -> [Row]
    fixTableRows [Row]
    rows

    fixTableBody :: TableBody -> TableBody
    fixTableBody :: TableBody -> TableBody
    fixTableBody (TableBody Attr
    attr RowHeadColumns
    rhc [Row]
    th [Row]
    tb)
      = Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
    TableBody Attr
    attr RowHeadColumns
    rhc [Row]
    th' [Row]
    tb'
      where
        th' :: [Row]
    th' = [Row] -> [Row]
    fixTableRows [Row]
    th
        tb' :: [Row]
    tb' = [Row] -> [Row]
    fixTableRows [Row]
    tb

    fixTableRows :: [Row] -> [Row]
    fixTableRows :: [Row] -> [Row]
    fixTableRows = [Maybe (ColSpan, RowSpan)] -> [Row] -> [Row]
    fixTableRows' ([Maybe (ColSpan, RowSpan)] -> [Row] -> [Row])
    -> [Maybe (ColSpan, RowSpan)] -> [Row] -> [Row]
    forall a b. (a -> b) -> a -> b
    $ Maybe (ColSpan, RowSpan) -> [Maybe (ColSpan, RowSpan)]
    forall a. a -> [a]
    repeat Maybe (ColSpan, RowSpan)
    forall a. Maybe a
    Nothing
      where
        fixTableRows' :: [Maybe (ColSpan, RowSpan)] -> [Row] -> [Row]
    fixTableRows' [Maybe (ColSpan, RowSpan)]
    oldHang (Row Attr
    attr [Cell]
    cells : [Row]
    rs)
          = let ([Maybe (ColSpan, RowSpan)]
    newHang, [Cell]
    cells') = [Maybe (ColSpan, RowSpan)]
    -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
    fixTableRow [Maybe (ColSpan, RowSpan)]
    oldHang [Cell]
    cells
                rs' :: [Row]
    rs'               = [Maybe (ColSpan, RowSpan)] -> [Row] -> [Row]
    fixTableRows' [Maybe (ColSpan, RowSpan)]
    newHang [Row]
    rs
            in Attr -> [Cell] -> Row
    Row Attr
    attr [Cell]
    cells' Row -> [Row] -> [Row]
    forall a. a -> [a] -> [a]
    : [Row]
    rs'
        fixTableRows' [Maybe (ColSpan, RowSpan)]
    _ [] = []

    -- The overhang is represented as Just (relative cell dimensions) or
    -- Nothing for an empty grid space.
    fixTableRow :: [Maybe (ColSpan, RowSpan)] -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
    fixTableRow :: [Maybe (ColSpan, RowSpan)]
    -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
    fixTableRow [Maybe (ColSpan, RowSpan)]
    oldHang [Cell]
    cells
      -- If there's overhang, drop cells until their total width meets the
      -- width of the occupied grid spaces (or we run out)
      | (ColSpan
    n, [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)]
    prefHang, [Maybe (ColSpan, RowSpan)]
    restHang) <- [Maybe (ColSpan, RowSpan)]
    -> (ColSpan,
        [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)],
        [Maybe (ColSpan, RowSpan)])
    splitHang [Maybe (ColSpan, RowSpan)]
    oldHang
      , ColSpan
    n ColSpan -> ColSpan -> Bool
    forall a. Ord a => a -> a -> Bool
    > ColSpan
    0
      = let cells' :: [Cell]
    cells' = (Cell -> ColSpan) -> ColSpan -> [Cell] -> [Cell]
    forall {t} {t}. (Ord t, Num t) => (t -> t) -> t -> [t] -> [t]
    dropToWidth Cell -> ColSpan
    getCellW ColSpan
    n [Cell]
    cells
            ([Maybe (ColSpan, RowSpan)]
    restHang', [Cell]
    cells'') = [Maybe (ColSpan, RowSpan)]
    -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
    fixTableRow [Maybe (ColSpan, RowSpan)]
    restHang [Cell]
    cells'
        in ([Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)]
    prefHang [Maybe (ColSpan, RowSpan)]
    restHang', [Cell]
    cells'')
      -- Otherwise record the overhang of a pending cell and fix the rest
      -- of the row
      | c :: Cell
    c@(Cell Attr
    _ Alignment
    _ RowSpan
    h ColSpan
    w [Block]
    _):[Cell]
    cells' <- [Cell]
    cells
      = let h' :: RowSpan
    h' = RowSpan -> RowSpan -> RowSpan
    forall a. Ord a => a -> a -> a
    max RowSpan
    1 RowSpan
    h
            w' :: ColSpan
    w' = ColSpan -> ColSpan -> ColSpan
    forall a. Ord a => a -> a -> a
    max ColSpan
    1 ColSpan
    w
            oldHang' :: [Maybe (ColSpan, RowSpan)]
    oldHang' = (Maybe (ColSpan, RowSpan) -> ColSpan)
    -> ColSpan
    -> [Maybe (ColSpan, RowSpan)]
    -> [Maybe (ColSpan, RowSpan)]
    forall {t} {t}. (Ord t, Num t) => (t -> t) -> t -> [t] -> [t]
    dropToWidth Maybe (ColSpan, RowSpan) -> ColSpan
    forall {b}. Maybe (ColSpan, b) -> ColSpan
    getHangW ColSpan
    w' [Maybe (ColSpan, RowSpan)]
    oldHang
            ([Maybe (ColSpan, RowSpan)]
    newHang, [Cell]
    cells'') = [Maybe (ColSpan, RowSpan)]
    -> [Cell] -> ([Maybe (ColSpan, RowSpan)], [Cell])
    fixTableRow [Maybe (ColSpan, RowSpan)]
    oldHang' [Cell]
    cells'
        in (ColSpan -> RowSpan -> [Maybe (ColSpan, RowSpan)]
    forall {b}. (Ord b, Num b) => ColSpan -> b -> [Maybe (ColSpan, b)]
    toHang ColSpan
    w' RowSpan
    h' [Maybe (ColSpan, RowSpan)]
    -> [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)]
    forall a. Semigroup a => a -> a -> a
    <> [Maybe (ColSpan, RowSpan)]
    newHang, Cell
    c Cell -> [Cell] -> [Cell]
    forall a. a -> [a] -> [a]
    : [Cell]
    cells'')
      | Bool
    otherwise
      = ([Maybe (ColSpan, RowSpan)]
    oldHang, [])
      where
        getCellW :: Cell -> ColSpan
    getCellW (Cell Attr
    _ Alignment
    _ RowSpan
    _ ColSpan
    w [Block]
    _) = ColSpan
    w
        getHangW :: Maybe (ColSpan, b) -> ColSpan
    getHangW = ColSpan
    -> ((ColSpan, b) -> ColSpan) -> Maybe (ColSpan, b) -> ColSpan
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe ColSpan
    1 (ColSpan, b) -> ColSpan
    forall a b. (a, b) -> a
    fst
        getCS :: ColSpan -> Int
    getCS (ColSpan Int
    n) = Int
    n

        toHang :: ColSpan -> b -> [Maybe (ColSpan, b)]
    toHang ColSpan
    c b
    r
          | b
    r b -> b -> Bool
    forall a. Ord a => a -> a -> Bool
    > b
    1     = [(ColSpan, b) -> Maybe (ColSpan, b)
    forall a. a -> Maybe a
    Just (ColSpan
    c, b
    r)]
          | Bool
    otherwise = Int -> Maybe (ColSpan, b) -> [Maybe (ColSpan, b)]
    forall a. Int -> a -> [a]
    replicate (ColSpan -> Int
    getCS ColSpan
    c) Maybe (ColSpan, b)
    forall a. Maybe a
    Nothing

        -- Take the prefix of the overhang list representing filled grid
        -- spaces. Also return the remainder and the length of this prefix.
        splitHang :: [Maybe (ColSpan, RowSpan)]
    -> (ColSpan,
        [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)],
        [Maybe (ColSpan, RowSpan)])
    splitHang = ColSpan
    -> ([Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)])
    -> [Maybe (ColSpan, RowSpan)]
    -> (ColSpan,
        [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)],
        [Maybe (ColSpan, RowSpan)])
    forall {b} {c}.
    (Ord b, Num b) =>
    ColSpan
    -> ([Maybe (ColSpan, b)] -> c)
    -> [Maybe (ColSpan, b)]
    -> (ColSpan, [Maybe (ColSpan, b)] -> c, [Maybe (ColSpan, b)])
    splitHang' ColSpan
    0 [Maybe (ColSpan, RowSpan)] -> [Maybe (ColSpan, RowSpan)]
    forall a. a -> a
    id

        splitHang' :: ColSpan
    -> ([Maybe (ColSpan, b)] -> c)
    -> [Maybe (ColSpan, b)]
    -> (ColSpan, [Maybe (ColSpan, b)] -> c, [Maybe (ColSpan, b)])
    splitHang' !ColSpan
    n [Maybe (ColSpan, b)] -> c
    l (Just (ColSpan
    c, b
    r):[Maybe (ColSpan, b)]
    xs)
          = ColSpan
    -> ([Maybe (ColSpan, b)] -> c)
    -> [Maybe (ColSpan, b)]
    -> (ColSpan, [Maybe (ColSpan, b)] -> c, [Maybe (ColSpan, b)])
    splitHang' (ColSpan
    n ColSpan -> ColSpan -> ColSpan
    forall a. Num a => a -> a -> a
    + ColSpan
    c) ([Maybe (ColSpan, b)] -> c
    l ([Maybe (ColSpan, b)] -> c)
    -> ([Maybe (ColSpan, b)] -> [Maybe (ColSpan, b)])
    -> [Maybe (ColSpan, b)]
    -> c
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (ColSpan -> b -> [Maybe (ColSpan, b)]
    forall {b}. (Ord b, Num b) => ColSpan -> b -> [Maybe (ColSpan, b)]
    toHang ColSpan
    c (b
    rb -> b -> b
    forall a. Num a => a -> a -> a
    -b
    1) [Maybe (ColSpan, b)]
    -> [Maybe (ColSpan, b)] -> [Maybe (ColSpan, b)]
    forall a. [a] -> [a] -> [a]
    ++)) [Maybe (ColSpan, b)]
    xs
        splitHang' ColSpan
    n [Maybe (ColSpan, b)] -> c
    l [Maybe (ColSpan, b)]
    xs = (ColSpan
    n, [Maybe (ColSpan, b)] -> c
    l, [Maybe (ColSpan, b)]
    xs)

        -- Drop list items until the total width of the dropped items
        -- exceeds the passed width.
        dropToWidth :: (t -> t) -> t -> [t] -> [t]
    dropToWidth t -> t
    _     t
    n [t]
    l | t
    n t -> t -> Bool
    forall a. Ord a => a -> a -> Bool
    < t
    1 = [t]
    l
        dropToWidth t -> t
    wproj t
    n (t
    c:[t]
    cs)    = (t -> t) -> t -> [t] -> [t]
    dropToWidth t -> t
    wproj (t
    n t -> t -> t
    forall a. Num a => a -> a -> a
    - t -> t
    wproj t
    c) [t]
    cs
        dropToWidth t -> t
    _     t
    _ []        = []

    simpTable :: PandocMonad m
              => LP m Blocks
              -> LP m Inlines
              -> Text
              -> Bool
              -> LP m Blocks
    simpTable :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Blocks -> LP m Inlines -> Text -> Bool -> LP m Blocks
    simpTable LP m Blocks
    block LP m Inlines
    inline Text
    envname Bool
    hasWidthParameter = LP m Blocks -> LP m Blocks
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (LP m Blocks -> LP m Blocks) -> LP m Blocks -> LP m Blocks
    forall a b. (a -> b) -> a -> b
    $ do
      Bool
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    hasWidthParameter (ParsecT TokStream LaTeXState m ()
     -> ParsecT TokStream LaTeXState m ())
    -> ParsecT TokStream LaTeXState m ()
    -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ () () -> LP m Inlines -> ParsecT TokStream LaTeXState m ()
    forall a b.
    a
    -> ParsecT TokStream LaTeXState m b
    -> ParsecT TokStream LaTeXState m a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ LP m Inlines -> LP m Inlines
    forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
    tokWith LP m Inlines
    inline
      ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    skipopts
      colspecs <- LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
    forall (m :: * -> *).
    PandocMonad m =>
    LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
    parseAligns
      let (aligns, widths, prefsufs) = unzip3 colspecs
      optional $ controlSeq "caption" *> setCaption inline
      spaces
      optional label
      spaces
      optional lbreak
      spaces
      skipMany hline
      spaces
      header' <- option [] . try . fmap (:[]) $
                 parseTableRow block inline envname prefsufs <*
                   lbreak <* many1 hline
      spaces
      rows <- sepEndBy (parseTableRow block inline envname prefsufs)
                        (lbreak <* optional (skipMany hline))
      spaces
      optional $ controlSeq "caption" *> setCaption inline
      spaces
      optional label
      spaces
      optional lbreak
      spaces
      lookAhead $ controlSeq "end" -- make sure we're at end
      let th  = TableHead -> TableHead
    fixTableHead (TableHead -> TableHead) -> TableHead -> TableHead
    forall a b. (a -> b) -> a -> b
    $ Attr -> [Row] -> TableHead
    TableHead Attr
    nullAttr [Row]
    header'
      let tbs = [TableBody -> TableBody
    fixTableBody (TableBody -> TableBody) -> TableBody -> TableBody
    forall a b. (a -> b) -> a -> b
    $ Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
    TableBody Attr
    nullAttr RowHeadColumns
    0 [] [Row]
    rows]
      let tf  = Attr -> [Row] -> TableFoot
    TableFoot Attr
    nullAttr []
      return $ table emptyCaption (zip aligns widths) th tbs tf

    addTableCaption :: PandocMonad m => Blocks -> LP m Blocks
    addTableCaption :: forall (m :: * -> *). PandocMonad m => Blocks -> LP m Blocks
    addTableCaption = (Block -> ParsecT TokStream LaTeXState m Block)
    -> Blocks -> ParsecT TokStream LaTeXState m Blocks
    forall a b (m :: * -> *).
    (Walkable a b, Monad m, Applicative m, Functor m) =>
    (a -> m a) -> b -> m b
    forall (m :: * -> *).
    (Monad m, Applicative m, Functor m) =>
    (Block -> m Block) -> Blocks -> m Blocks
    walkM Block -> ParsecT TokStream LaTeXState m Block
    forall {m :: * -> *}.
    Monad m =>
    Block -> ParsecT TokStream LaTeXState m Block
    go
      where go :: Block -> ParsecT TokStream LaTeXState m Block
    go (Table Attr
    attr Caption
    c [ColSpec]
    spec TableHead
    th [TableBody]
    tb TableFoot
    tf) = do
              st <- ParsecT TokStream LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
              let capt = Caption -> Maybe Caption -> Caption
    forall a. a -> Maybe a -> a
    fromMaybe Caption
    c (Maybe Caption -> Caption) -> Maybe Caption -> Caption
    forall a b. (a -> b) -> a -> b
    $ LaTeXState -> Maybe Caption
    sCaption LaTeXState
    st
              let mblabel = LaTeXState -> Maybe Text
    sLastLabel LaTeXState
    st
              case mblabel of
                Maybe Text
    Nothing -> () -> ParsecT TokStream LaTeXState m ()
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
                Just Text
    lab -> do
                         num <- (LaTeXState -> DottedNum) -> LP m DottedNum
    forall (m :: * -> *).
    Monad m =>
    (LaTeXState -> DottedNum) -> LP m DottedNum
    getNextNumber LaTeXState -> DottedNum
    sLastTableNum
                         setState
                           st{ sLastTableNum = num
                             , sLabels = M.insert lab
                                        [Str (renderDottedNum num)]
                                        (sLabels st) }
              -- add num to caption?
              let attr' = case (Attr
    attr, Maybe Text
    mblabel) of
                            ((Text
    _,[Text]
    classes,[(Text, Text)]
    kvs), Just Text
    ident) ->
                               (Text
    ident,[Text]
    classes,[(Text, Text)]
    kvs)
                            (Attr, Maybe Text)
    _ -> Attr
    attr
              return $ maybe id removeLabel mblabel
                     $ Table attr' capt spec th tb tf
            go Block
    x = Block -> ParsecT TokStream LaTeXState m Block
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Block
    x