{-# 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
blocks 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
blocks 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
blocks 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
blocks 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
blocks LP m Inlines
inline Text
"tabular" Bool
False)
  ]

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
  Bool
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")
  ParsecT TokStream LaTeXState m Text
-> ParsecT TokStream LaTeXState m (Maybe Text)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT TokStream LaTeXState m Text
forall (m :: * -> *). PandocMonad m => LP m Text
rawopt
  Bool
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hasParenArg (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 (Maybe ())
-> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT TokStream LaTeXState m (Maybe ())
 -> ParsecT TokStream LaTeXState m ())
-> ParsecT TokStream LaTeXState m (Maybe ())
-> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> 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 ()
-> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) a.
(PandocMonad m, Monoid a) =>
LP m a -> LP m a
parenWrapped (() ()
-> 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
<$ ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
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 () -> 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 ()
skipopts 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 Bool
macrosExpanded [Tok]
inp <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  case [Tok]
inp of
       (Tok SourcePos
spos TokType
Word Text
t : [Tok]
rest) ->
         TokStream -> LP m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput (TokStream -> LP m ()) -> TokStream -> LP 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) -> [Char] -> [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 -> [Char]
T.unpack Text
t) [Tok] -> [Tok] -> [Tok]
forall a. Semigroup a => a -> a -> a
<> [Tok]
rest
       [Tok]
_ -> () -> LP 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
        [Tok]
ts <- ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
        let isLinewidth :: Tok -> Bool
isLinewidth (Tok SourcePos
_ (CtrlSeq Text
"linewidth") Text
_) = Bool
True
            isLinewidth Tok
_ = Bool
False
        case (Tok -> Bool) -> [Tok] -> ([Tok], [Tok])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break Tok -> Bool
isLinewidth [Tok]
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
        [Tok]
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
        ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        Alignment
al <- ParsecT TokStream LaTeXState m Alignment
alignChar
        Maybe Double
width <- ParsecT TokStream LaTeXState m (Maybe Double)
colWidth ParsecT TokStream LaTeXState m (Maybe Double)
-> ParsecT TokStream LaTeXState m (Maybe Double)
-> ParsecT TokStream LaTeXState m (Maybe Double)
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
<|> Maybe Double
-> ParsecT TokStream LaTeXState m (Maybe Double)
-> ParsecT TokStream LaTeXState m (Maybe Double)
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe Double
forall a. Maybe a
Nothing (do Text
s <- [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]
braced
                                                 SourcePos
pos <- ParsecT TokStream LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
                                                 LogMessage -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> ParsecT TokStream LaTeXState m ())
-> LogMessage -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
SkippedContent Text
s SourcePos
pos
                                                 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)
        ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        [Tok]
suff <- [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]
alignSuffix
        (Alignment, Maybe Double, ([Tok], [Tok]))
-> ParsecT
     TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Alignment
al, Maybe Double
width, ([Tok]
pref, [Tok]
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
        Text
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
        ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        [Tok]
spec <- ParsecT TokStream LaTeXState m [Tok]
forall (m :: * -> *). PandocMonad m => LP m [Tok]
bracedOrToken
        case Text -> Maybe Int
forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
ds of
             Just Int
n  -> do
               TokStream Bool
_ [Tok]
ts <- ParsecT TokStream LaTeXState m TokStream
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
               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
False ([[Tok]] -> [Tok]
forall a. Monoid a => [a] -> a
mconcat (Int -> [Tok] -> [[Tok]]
forall a. Int -> a -> [a]
replicate Int
n [Tok]
spec) [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
ts)
             Maybe Int
Nothing -> [Char] -> ParsecT TokStream LaTeXState m ()
forall a. [Char] -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. MonadFail m => [Char] -> m a
Prelude.fail ([Char] -> ParsecT TokStream LaTeXState m ())
-> [Char] -> ParsecT TokStream LaTeXState m ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Could not parse " [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> Text -> [Char]
T.unpack Text
ds [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" 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
  [(Alignment, Maybe Double, ([Tok], [Tok]))]
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)
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
egroup
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  [(Alignment, ColWidth, ([Tok], [Tok]))]
-> ParsecT
     TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Alignment, ColWidth, ([Tok], [Tok]))]
 -> ParsecT
      TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))])
-> [(Alignment, ColWidth, ([Tok], [Tok]))]
-> ParsecT
     TokStream LaTeXState m [(Alignment, ColWidth, ([Tok], [Tok]))]
forall a b. (a -> b) -> a -> b
$ ((Alignment, Maybe Double, ([Tok], [Tok]))
 -> (Alignment, ColWidth, ([Tok], [Tok])))
-> [(Alignment, Maybe Double, ([Tok], [Tok]))]
-> [(Alignment, ColWidth, ([Tok], [Tok]))]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment, Maybe Double, ([Tok], [Tok]))
-> (Alignment, ColWidth, ([Tok], [Tok]))
forall {a} {c}. (a, Maybe Double, c) -> (a, ColWidth, c)
toSpec [(Alignment, Maybe Double, ([Tok], [Tok]))]
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
blocks 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
        SourcePos
prefpos <- ParsecT TokStream LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
        [Tok]
contents <- [[Tok]] -> [Tok]
forall a. Monoid a => [a] -> a
mconcat ([[Tok]] -> [Tok])
-> ParsecT TokStream LaTeXState m [[Tok]]
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
            ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [[Tok]]
forall a.
ParsecT TokStream LaTeXState m a
-> ParsecT TokStream LaTeXState m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ( ((), [Tok]) -> [Tok]
forall a b. (a, b) -> b
snd (((), [Tok]) -> [Tok])
-> ParsecT TokStream LaTeXState m ((), [Tok])
-> ParsecT TokStream LaTeXState m [Tok]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m ((), [Tok])
forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw
                     ((ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (Text -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"parbox") ParsecT TokStream LaTeXState m Tok
-> 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 (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                       LP m Blocks -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Blocks
blocks) -- #5711
                      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 Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ((Tok -> Bool) -> ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
canContainAmp) ParsecT TokStream LaTeXState m Tok
-> 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 (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> LP m Inlines -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Inlines
inline)
                      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 Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (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 ()
-> ParsecT TokStream LaTeXState m ()
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
>> LP m Inlines -> ParsecT TokStream LaTeXState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Inlines
inline))
                  ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState 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
<|>
                   (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 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
<$ ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
amp 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
<$ ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak 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
<|> Text -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
envname)
                       Int
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
1 ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok) )

        SourcePos
suffpos <- ParsecT TokStream LaTeXState m SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
        [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 [] (Int
-> ParsecT TokStream LaTeXState m Tok
-> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
1 ParsecT TokStream LaTeXState m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
amp)
        [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Tok] -> ParsecT TokStream LaTeXState m [Tok])
-> [Tok] -> ParsecT TokStream LaTeXState m [Tok]
forall a b. (a -> b) -> a -> b
$ (Tok -> Tok) -> [Tok] -> [Tok]
forall a b. (a -> b) -> [a] -> [b]
map (SourcePos -> Tok -> Tok
setpos SourcePos
prefpos) [Tok]
pref [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ [Tok]
contents [Tok] -> [Tok] -> [Tok]
forall a. [a] -> [a] -> [a]
++ (Tok -> Tok) -> [Tok] -> [Tok]
forall a b. (a -> b) -> [a] -> [b]
map (SourcePos -> Tok -> Tok
setpos SourcePos
suffpos) [Tok]
suff
  [[Tok]]
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
  [Cell]
cells <- ([Tok] -> ParsecT TokStream LaTeXState m Cell)
-> [[Tok]] -> ParsecT TokStream LaTeXState m [Cell]
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 (ParsecT TokStream LaTeXState m Cell
-> [Tok] -> ParsecT TokStream LaTeXState m Cell
forall (m :: * -> *) a. PandocMonad m => LP m a -> [Tok] -> LP m a
parseFromToks (LP m Blocks -> ParsecT TokStream LaTeXState m Cell
forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
parseTableCell LP m Blocks
blocks)) [[Tok]]
rawcells
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  Row -> LP m Row
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Row -> LP m Row) -> Row -> LP m Row
forall a b. (a -> b) -> a -> b
$ Attr -> [Cell] -> Row
Row Attr
nullAttr [Cell]
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
blocks = 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
cell' <-   LP m Blocks -> LP m Cell
forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
multicolumnCell LP m Blocks
blocks
         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
blocks
         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
  (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 = False }
  LP m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  Cell -> LP m Cell
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Cell
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) -> LP m Blocks -> LP m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Blocks
blocks)


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
      Text
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
      Alignment -> ParsecT TokStream LaTeXState m Alignment
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Alignment -> ParsecT TokStream LaTeXState m Alignment)
-> Alignment -> ParsecT TokStream LaTeXState m Alignment
forall a b. (a -> b) -> a -> b
$ case Text
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
blocks = 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
  Maybe Alignment
_ <- 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
  Int
nrows <- ([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
  Maybe [Tok]
_ <- 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]
 -> ParsecT TokStream LaTeXState m (Maybe [Tok]))
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m (Maybe [Tok])
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 [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 (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok -> LP m Tok -> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')  -- bigstrut-related
  [Tok]
_ <- Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP 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 (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok -> LP m Tok -> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}')             -- Cell width
  Maybe [Tok]
_ <- 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]
 -> ParsecT TokStream LaTeXState m (Maybe [Tok]))
-> ParsecT TokStream LaTeXState m [Tok]
-> ParsecT TokStream LaTeXState m (Maybe [Tok])
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 [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 (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Tok -> LP m Tok -> ParsecT TokStream LaTeXState m [Tok]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')  -- Length used for fine-tuning
  Blocks
content <- Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' LP m Tok -> 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
*> (Blocks -> Blocks
plainify (Blocks -> Blocks) -> LP m Blocks -> LP m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Blocks
blocks) LP m Blocks -> LP m Tok -> LP m Blocks
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
'}'
  Cell -> ParsecT TokStream LaTeXState m Cell
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Cell -> ParsecT TokStream LaTeXState m Cell)
-> Cell -> ParsecT TokStream LaTeXState m Cell
forall a b. (a -> b) -> a -> b
$ Alignment -> RowSpan -> ColSpan -> Blocks -> Cell
cell Alignment
AlignDefault (Int -> RowSpan
RowSpan Int
nrows) (Int -> ColSpan
ColSpan Int
1) Blocks
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
blocks = 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
  Int
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
alignment <- 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
'}'

  let singleCell :: ParsecT TokStream LaTeXState m Cell
singleCell = do
        Blocks
content <- Blocks -> Blocks
plainify (Blocks -> Blocks) -> LP m Blocks -> LP m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Blocks
blocks
        Cell -> ParsecT TokStream LaTeXState m Cell
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Cell -> ParsecT TokStream LaTeXState m Cell)
-> Cell -> ParsecT TokStream LaTeXState m Cell
forall a b. (a -> b) -> a -> b
$ Alignment -> RowSpan -> ColSpan -> Blocks -> Cell
cell Alignment
alignment (Int -> RowSpan
RowSpan Int
1) (Int -> ColSpan
ColSpan Int
span') Blocks
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 :: ParsecT TokStream LaTeXState m Cell
nestedCell = do
        (Cell Attr
_ Alignment
_ (RowSpan Int
rs) ColSpan
_ [Block]
bs) <- LP m Blocks -> ParsecT TokStream LaTeXState m Cell
forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
multirowCell LP m Blocks
blocks
        Cell -> ParsecT TokStream LaTeXState m Cell
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Cell -> ParsecT TokStream LaTeXState m Cell)
-> Cell -> ParsecT TokStream LaTeXState m Cell
forall a b. (a -> b) -> a -> b
$ Alignment -> RowSpan -> ColSpan -> Blocks -> Cell
cell
                  Alignment
alignment
                  (Int -> RowSpan
RowSpan Int
rs)
                  (Int -> ColSpan
ColSpan Int
span')
                  ([Block] -> Blocks
forall a. [a] -> Many a
fromList [Block]
bs)

  Char -> LP m Tok
forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' 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 (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (ParsecT TokStream LaTeXState m Cell
nestedCell ParsecT TokStream LaTeXState m Cell
-> ParsecT TokStream LaTeXState m Cell
-> ParsecT TokStream LaTeXState 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
<|> ParsecT TokStream LaTeXState m Cell
singleCell) ParsecT TokStream LaTeXState m Cell
-> LP m Tok -> ParsecT TokStream LaTeXState m Cell
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
'}'

-- 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
blocks 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
  [(Alignment, ColWidth, ([Tok], [Tok]))]
colspecs <- LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
forall (m :: * -> *).
PandocMonad m =>
LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
parseAligns
  let ([Alignment]
aligns, [ColWidth]
widths, [([Tok], [Tok])]
prefsufs) = [(Alignment, ColWidth, ([Tok], [Tok]))]
-> ([Alignment], [ColWidth], [([Tok], [Tok])])
forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 [(Alignment, ColWidth, ([Tok], [Tok]))]
colspecs
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m (Maybe ()))
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall a b. (a -> b) -> a -> b
$ Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"caption" LP m Tok
-> 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
*> LP m Inlines -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m ()
setCaption LP m Inlines
inline
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
label
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  LP m Tok -> ParsecT TokStream LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  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 ()
forall (m :: * -> *). PandocMonad m => LP m ()
hline
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  [Row]
header' <- [Row]
-> ParsecT TokStream LaTeXState m [Row]
-> ParsecT TokStream LaTeXState m [Row]
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 [Row]
 -> ParsecT TokStream LaTeXState m [Row])
-> (ParsecT TokStream LaTeXState m Row
    -> ParsecT TokStream LaTeXState m [Row])
-> ParsecT TokStream LaTeXState m Row
-> ParsecT TokStream LaTeXState m [Row]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParsecT TokStream LaTeXState m [Row]
-> ParsecT TokStream LaTeXState m [Row]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT TokStream LaTeXState m [Row]
 -> ParsecT TokStream LaTeXState m [Row])
-> (ParsecT TokStream LaTeXState m Row
    -> ParsecT TokStream LaTeXState m [Row])
-> ParsecT TokStream LaTeXState m Row
-> ParsecT TokStream LaTeXState m [Row]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Row -> [Row])
-> ParsecT TokStream LaTeXState m Row
-> ParsecT TokStream LaTeXState m [Row]
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 (Row -> [Row] -> [Row]
forall a. a -> [a] -> [a]
:[]) (ParsecT TokStream LaTeXState m Row
 -> ParsecT TokStream LaTeXState m [Row])
-> ParsecT TokStream LaTeXState m Row
-> ParsecT TokStream LaTeXState m [Row]
forall a b. (a -> b) -> a -> b
$
             LP m Blocks
-> LP m Inlines
-> Text
-> [([Tok], [Tok])]
-> ParsecT TokStream LaTeXState m Row
forall (m :: * -> *).
PandocMonad m =>
LP m Blocks -> LP m Inlines -> Text -> [([Tok], [Tok])] -> LP m Row
parseTableRow LP m Blocks
blocks LP m Inlines
inline Text
envname [([Tok], [Tok])]
prefsufs ParsecT TokStream LaTeXState m Row
-> LP m Tok -> ParsecT TokStream LaTeXState m Row
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
<*
               LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak ParsecT TokStream LaTeXState m Row
-> ParsecT TokStream LaTeXState m [()]
-> ParsecT TokStream LaTeXState m Row
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 [()]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
hline
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  [Row]
rows <- ParsecT TokStream LaTeXState m Row
-> LP m Tok -> ParsecT TokStream LaTeXState m [Row]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
sepEndBy (LP m Blocks
-> LP m Inlines
-> Text
-> [([Tok], [Tok])]
-> ParsecT TokStream LaTeXState m Row
forall (m :: * -> *).
PandocMonad m =>
LP m Blocks -> LP m Inlines -> Text -> [([Tok], [Tok])] -> LP m Row
parseTableRow LP m Blocks
blocks LP m Inlines
inline Text
envname [([Tok], [Tok])]
prefsufs)
                    (LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak 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 ()
-> ParsecT TokStream LaTeXState m ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
hline))
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT TokStream LaTeXState m ()
 -> ParsecT TokStream LaTeXState m (Maybe ()))
-> ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall a b. (a -> b) -> a -> b
$ Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"caption" LP m Tok
-> 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
*> LP m Inlines -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m ()
setCaption LP m Inlines
inline
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  ParsecT TokStream LaTeXState m ()
-> ParsecT TokStream LaTeXState m (Maybe ())
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
label
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  LP m Tok -> ParsecT TokStream LaTeXState m (Maybe Tok)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional LP m Tok
forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak
  ParsecT TokStream LaTeXState m ()
forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  LP m Tok -> LP m Tok
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (LP m Tok -> LP m Tok) -> LP m Tok -> LP m Tok
forall a b. (a -> b) -> a -> b
$ Text -> LP m Tok
forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"end" -- make sure we're at end
  let th :: TableHead
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]
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 :: TableFoot
tf  = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []
  Blocks -> LP m Blocks
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> LP m Blocks) -> Blocks -> LP m Blocks
forall a b. (a -> b) -> a -> b
$ Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Blocks
table Caption
emptyCaption ([Alignment] -> [ColWidth] -> [ColSpec]
forall a b. [a] -> [b] -> [(a, b)]
zip [Alignment]
aligns [ColWidth]
widths) TableHead
th [TableBody]
tbs TableFoot
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
          LaTeXState
st <- ParsecT TokStream LaTeXState m LaTeXState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
          let capt :: Caption
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 :: Maybe Text
mblabel = LaTeXState -> Maybe Text
sLastLabel LaTeXState
st
          case Maybe Text
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
                     DottedNum
num <- (LaTeXState -> DottedNum) -> LP m DottedNum
forall (m :: * -> *).
Monad m =>
(LaTeXState -> DottedNum) -> LP m DottedNum
getNextNumber LaTeXState -> DottedNum
sLastTableNum
                     LaTeXState -> ParsecT TokStream LaTeXState m ()
forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState
                       LaTeXState
st{ sLastTableNum = num
                         , sLabels = M.insert lab
                                    [Str (renderDottedNum num)]
                                    (sLabels st) }
          -- add num to caption?
          let attr' :: Attr
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
          Block -> ParsecT TokStream LaTeXState m Block
forall a. a -> ParsecT TokStream LaTeXState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Block -> ParsecT TokStream LaTeXState m Block)
-> Block -> ParsecT TokStream LaTeXState m Block
forall a b. (a -> b) -> a -> b
$ Attr -> Block -> Block
addAttrDiv Attr
attr'
                 (Block -> Block) -> Block -> Block
forall a b. (a -> b) -> a -> b
$ (Block -> Block)
-> (Text -> Block -> Block) -> Maybe Text -> Block -> Block
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Block -> Block
forall a. a -> a
id Text -> Block -> Block
forall a. Walkable [Inline] a => Text -> a -> a
removeLabel Maybe Text
mblabel
                 (Block -> Block) -> Block -> Block
forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
nullAttr Caption
capt [ColSpec]
spec TableHead
th [TableBody]
tb TableFoot
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

-- TODO: For now we add a Div to contain table attributes, since
-- most writers don't do anything yet with attributes on Table.
-- This can be removed when that changes.
addAttrDiv :: Attr -> Block -> Block
addAttrDiv :: Attr -> Block -> Block
addAttrDiv (Text
"",[],[]) Block
b = Block
b
addAttrDiv Attr
attr Block
b       = Attr -> [Block] -> Block
Div Attr
attr [Block
b]