{-# 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.Readers.LaTeX.Types
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 =
  forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
  [ (Text
"longtable",  forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
"longtable" forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *). PandocMonad m => LP m ()
resetCaption forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>
            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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). PandocMonad m => Blocks -> LP m Blocks
addTableCaption)
  , (Text
"table",  forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
"table" forall a b. (a -> b) -> a -> b
$
          forall (m :: * -> *). PandocMonad m => LP m ()
skipopts forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *). PandocMonad m => LP m ()
resetCaption forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> LP m Blocks
blocks forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *). PandocMonad m => Blocks -> LP m Blocks
addTableCaption)
  , (Text
"tabular*", forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
"tabular*" forall a b. (a -> b) -> a -> b
$ 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", forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
"tabularx" forall a b. (a -> b) -> a -> b
$ 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", forall (m :: * -> *) a. PandocMonad m => Text -> LP m a -> LP m a
env Text
"tabular"  forall a b. (a -> b) -> a -> b
$ 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 = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ do
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"hline" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    (forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"cline" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    -- booktabs rules:
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"toprule" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"bottomrule" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"midrule" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"endhead" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
    forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"endfirsthead"
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall (m :: * -> *). PandocMonad m => LP m Text
rawopt
  forall (m :: * -> *) a. Monad m => a -> m a
return ()

lbreak :: PandocMonad m => LP m Tok
lbreak :: forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak = (forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"\\" forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"tabularnewline")
         forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => LP m ()
skipopts forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => LP m ()
spaces

amp :: PandocMonad m => LP m Tok
amp :: forall (m :: * -> *). PandocMonad m => LP m Tok
amp = 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 <- 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) ->
         forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
macrosExpanded
                  forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (SourcePos -> TokType -> Text -> Tok
Tok SourcePos
spos TokType
Symbol forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
T.singleton) (Text -> String
T.unpack Text
t) forall a. Semigroup a => a -> a -> a
<> [Tok]
rest
       [Tok]
_ -> 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 = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ do
  let maybeBar :: ParsecT TokStream LaTeXState m ()
maybeBar = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany
        (forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => LP m ()
sp forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (() forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'|' forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'@' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced)))
  let cAlign :: ParsecT TokStream LaTeXState m Alignment
cAlign = Alignment
AlignCenter forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'c'
  let lAlign :: ParsecT TokStream LaTeXState m Alignment
lAlign = Alignment
AlignLeft forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'l'
  let rAlign :: ParsecT TokStream LaTeXState m Alignment
rAlign = Alignment
AlignRight forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'r'
  let parAlign :: ParsecT TokStream LaTeXState m Alignment
parAlign = Alignment
AlignLeft forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'p'
  -- aligns from tabularx
  let xAlign :: ParsecT TokStream LaTeXState m Alignment
xAlign = Alignment
AlignLeft forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'X'
  let mAlign :: ParsecT TokStream LaTeXState m Alignment
mAlign = Alignment
AlignLeft forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'm'
  let bAlign :: ParsecT TokStream LaTeXState m Alignment
bAlign = Alignment
AlignLeft forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'b'
  let alignChar :: ParsecT TokStream LaTeXState m Alignment
alignChar = forall (m :: * -> *). PandocMonad m => LP m ()
splitWordTok forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (  ParsecT TokStream LaTeXState m Alignment
cAlign forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Alignment
lAlign forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Alignment
rAlign forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Alignment
parAlign
                                 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Alignment
xAlign forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Alignment
mAlign 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 = forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'>' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  let alignSuffix :: ParsecT TokStream LaTeXState m [Tok]
alignSuffix = forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'<' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  let colWidth :: ParsecT TokStream LaTeXState m (Maybe Double)
colWidth = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ do
        forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{'
        Text
ds <- Text -> Text
trim forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tok] -> Text
untokenize forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"linewidth")
        forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}'
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
ds
  let alignSpec :: ParsecT
  TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
alignSpec = do
        [Tok]
pref <- 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
        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 forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option forall a. Maybe a
Nothing (do Text
s <- [Tok] -> Text
untokenize forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
                                                 SourcePos
pos <- forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
                                                 forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report forall a b. (a -> b) -> a -> b
$ Text -> SourcePos -> LogMessage
SkippedContent Text
s SourcePos
pos
                                                 forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing)
        forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        [Tok]
suff <- 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
        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
        forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'*'
        forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        Text
ds <- Text -> Text
trim forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tok] -> Text
untokenize forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
        forall (m :: * -> *). PandocMonad m => LP m ()
spaces
        [Tok]
spec <- forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
        case forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead Text
ds of
             Just Int
n  -> do
               TokStream Bool
_ [Tok]
ts <- forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
               forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput forall a b. (a -> b) -> a -> b
$ Bool -> [Tok] -> TokStream
TokStream Bool
False (forall a. Monoid a => [a] -> a
mconcat (forall a. Int -> a -> [a]
replicate Int
n [Tok]
spec) forall a. [a] -> [a] -> [a]
++ [Tok]
ts)
             Maybe Int
Nothing -> forall (m :: * -> *) a. MonadFail m => String -> m a
Prelude.fail forall a b. (a -> b) -> a -> b
$ String
"Could not parse " forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
ds forall a. Semigroup a => a -> a -> a
<> String
" as number"
  forall (m :: * -> *). PandocMonad m => LP m Tok
bgroup
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  ParsecT TokStream LaTeXState m ()
maybeBar
  [(Alignment, Maybe Double, ([Tok], [Tok]))]
aligns' <- forall (f :: * -> *) a. Alternative f => f a -> f [a]
many forall a b. (a -> b) -> a -> b
$ forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => LP m ()
spaces forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ParsecT TokStream LaTeXState m ()
starAlign forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                            (ParsecT
  TokStream LaTeXState m (Alignment, Maybe Double, ([Tok], [Tok]))
alignSpec forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT TokStream LaTeXState m ()
maybeBar)
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (m :: * -> *). PandocMonad m => LP m Tok
egroup
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map 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 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
  forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (forall (m :: * -> *). PandocMonad m => LP m ()
spaces forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 <- forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
        [Tok]
contents <- forall a. Monoid a => [a] -> a
mconcat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
            forall (f :: * -> *) a. Alternative f => f a -> f [a]
many ( forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. PandocMonad m => LP m a -> LP m (a, [Tok])
withRaw
                     ((forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"parbox") forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                       forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Blocks
blocks) -- #5711
                      forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                      (forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (forall (m :: * -> *). PandocMonad m => (Tok -> Bool) -> LP m Tok
satisfyTok Tok -> Bool
canContainAmp) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Inlines
inline)
                      forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                      (forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'$') forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Functor f => f a -> f ()
void LP m Inlines
inline))
                  forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                   (do forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy
                         (() forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => LP m Tok
amp forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *). PandocMonad m => Text -> LP m ()
end_ Text
envname)
                       forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
1 forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok) )

        SourcePos
suffpos <- forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition
        forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [] (forall s (m :: * -> *) t u a.
Stream s m t =>
Int -> ParsecT s u m a -> ParsecT s u m [a]
count Int
1 forall (m :: * -> *). PandocMonad m => LP m Tok
amp)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (SourcePos -> Tok -> Tok
setpos SourcePos
prefpos) [Tok]
pref forall a. [a] -> [a] -> [a]
++ [Tok]
contents forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (SourcePos -> Tok -> Tok
setpos SourcePos
suffpos) [Tok]
suff
  [[Tok]]
rawcells <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ([Tok], [Tok]) -> ParsecT TokStream LaTeXState m [Tok]
celltoks [([Tok], [Tok])]
prefsufs
  [Cell]
cells <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall (m :: * -> *) a. PandocMonad m => LP m a -> [Tok] -> LP m a
parseFromToks (forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
parseTableCell LP m Blocks
blocks)) [[Tok]]
rawcells
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (m :: * -> *) a. Monad m => a -> m a
return 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
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sInTableCell :: Bool
sInTableCell = Bool
True }
  Cell
cell' <-   forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
multicolumnCell LP m Blocks
blocks
         forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
multirowCell LP m Blocks
blocks
         forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m Cell
parseSimpleCell
         forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> LP m Cell
parseEmptyCell
  forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState forall a b. (a -> b) -> a -> b
$ \LaTeXState
st -> LaTeXState
st{ sInTableCell :: Bool
sInTableCell = Bool
False }
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  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 = forall (m :: * -> *). PandocMonad m => LP m ()
spaces forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Cell
emptyCell
    parseSimpleCell :: LP m Cell
parseSimpleCell = Blocks -> Cell
simpleCell forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Blocks -> Blocks
plainify 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 = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'|') forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT TokStream LaTeXState m Alignment
alignment forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'|')
  where
    alignment :: ParsecT TokStream LaTeXState m Alignment
alignment = do
      Text
c <- Tok -> Text
untoken forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). PandocMonad m => LP m Tok
singleChar
      forall (m :: * -> *) a. Monad m => a -> m a
return 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 forall a. Many a -> [a]
toList Blocks
bs of
                [Para [Inline]
ils] -> Inlines -> Blocks
plain (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 = forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"multirow" 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
_ <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'[' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *). PandocMonad m => LP m Alignment
cellAlignment forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']'   -- vertical position
  Int
nrows <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tok] -> Text
untokenize) forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Maybe [Tok]
_ <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'[' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')  -- bigstrut-related
  [Tok]
_ <- forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}')             -- Cell width
  Maybe [Tok]
_ <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'[' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 forall (m :: * -> *). PandocMonad m => LP m Tok
anyTok (forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
']')  -- Length used for fine-tuning
  Blocks
content <- forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Blocks -> Blocks
plainify forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Blocks
blocks) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}'
  forall (m :: * -> *) a. Monad m => a -> m a
return 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 = forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"multicolumn" forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> do
  Int
span' <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
safeRead forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Tok] -> Text
untokenize) forall (m :: * -> *). PandocMonad m => LP m [Tok]
braced
  Alignment
alignment <- forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *). PandocMonad m => LP m Alignment
cellAlignment forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'}'

  let singleCell :: ParsecT TokStream LaTeXState m Cell
singleCell = do
        Blocks
content <- Blocks -> Blocks
plainify forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LP m Blocks
blocks
        forall (m :: * -> *) a. Monad m => a -> m a
return 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) <- forall (m :: * -> *). PandocMonad m => LP m Blocks -> LP m Cell
multirowCell LP m Blocks
blocks
        forall (m :: * -> *) a. Monad m => a -> m a
return 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')
                  (forall a. [a] -> Many a
fromList [Block]
bs)

  forall (m :: * -> *). PandocMonad m => Char -> LP m Tok
symbol Char
'{' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (ParsecT TokStream LaTeXState m Cell
nestedCell forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT TokStream LaTeXState m Cell
singleCell) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* 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' forall a b. (a -> b) -> a -> b
$ forall a. a -> [a]
repeat 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' 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 forall a. Ord a => a -> a -> Bool
> ColSpan
0
  = let cells' :: [Cell]
cells' = 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' = forall a. Ord a => a -> a -> a
max RowSpan
1 RowSpan
h
        w' :: ColSpan
w' = forall a. Ord a => a -> a -> a
max ColSpan
1 ColSpan
w
        oldHang' :: [Maybe (ColSpan, RowSpan)]
oldHang' = forall {t} {t}. (Ord t, Num t) => (t -> t) -> t -> [t] -> [t]
dropToWidth 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 (forall {b}. (Ord b, Num b) => ColSpan -> b -> [Maybe (ColSpan, b)]
toHang ColSpan
w' RowSpan
h' forall a. Semigroup a => a -> a -> a
<> [Maybe (ColSpan, RowSpan)]
newHang, Cell
c 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 = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ColSpan
1 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 forall a. Ord a => a -> a -> Bool
> b
1     = [forall a. a -> Maybe a
Just (ColSpan
c, b
r)]
      | Bool
otherwise = forall a. Int -> a -> [a]
replicate (ColSpan -> Int
getCS ColSpan
c) 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 = 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 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 forall a. Num a => a -> a -> a
+ ColSpan
c) ([Maybe (ColSpan, b)] -> c
l forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall {b}. (Ord b, Num b) => ColSpan -> b -> [Maybe (ColSpan, b)]
toHang ColSpan
c (b
rforall a. Num a => a -> a -> a
-b
1) 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 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 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 = forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall a b. (a -> b) -> a -> b
$ do
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
hasWidthParameter forall a b. (a -> b) -> a -> b
$ () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
tokWith LP m Inlines
inline
  forall (m :: * -> *). PandocMonad m => LP m ()
skipopts
  [(Alignment, ColWidth, ([Tok], [Tok]))]
colspecs <- forall (m :: * -> *).
PandocMonad m =>
LP m [(Alignment, ColWidth, ([Tok], [Tok]))]
parseAligns
  let ([Alignment]
aligns, [ColWidth]
widths, [([Tok], [Tok])]
prefsufs) = forall a b c. [(a, b, c)] -> ([a], [b], [c])
unzip3 [(Alignment, ColWidth, ([Tok], [Tok]))]
colspecs
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"caption" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m ()
setCaption LP m Inlines
inline
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall (m :: * -> *). PandocMonad m => LP m ()
label
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany forall (m :: * -> *). PandocMonad m => LP m ()
hline
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  [Row]
header' <- forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> [a] -> [a]
:[]) forall a b. (a -> b) -> a -> b
$
             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 forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<*
               forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 forall (m :: * -> *). PandocMonad m => LP m ()
hline
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  [Row]
rows <- 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 (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)
                    (forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany forall (m :: * -> *). PandocMonad m => LP m ()
hline))
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"caption" forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m ()
setCaption LP m Inlines
inline
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall (m :: * -> *). PandocMonad m => LP m ()
label
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall (m :: * -> *). PandocMonad m => LP m Tok
lbreak
  forall (m :: * -> *). PandocMonad m => LP m ()
spaces
  forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). PandocMonad m => Text -> LP m Tok
controlSeq Text
"end" -- make sure we're at end
  let th :: TableHead
th  = TableHead -> TableHead
fixTableHead forall a b. (a -> b) -> a -> b
$ Attr -> [Row] -> TableHead
TableHead Attr
nullAttr [Row]
header'
  let tbs :: [TableBody]
tbs = [TableBody -> TableBody
fixTableBody 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 []
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Blocks
table Caption
emptyCaption (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 = forall a b (m :: * -> *).
(Walkable a b, Monad m, Applicative m, Functor m) =>
(a -> m a) -> b -> m b
walkM 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 <- forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
          let mblabel :: Maybe Text
mblabel = LaTeXState -> Maybe Text
sLastLabel LaTeXState
st
          Caption
capt <- case (LaTeXState -> Maybe Inlines
sCaption LaTeXState
st, Maybe Text
mblabel) of
                   (Just Inlines
ils, Maybe Text
Nothing)  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Maybe [Inline] -> Blocks -> Caption
caption forall a. Maybe a
Nothing (Inlines -> Blocks
plain Inlines
ils)
                   (Just Inlines
ils, Just Text
lab) -> do
                     DottedNum
num <- forall (m :: * -> *).
Monad m =>
(LaTeXState -> DottedNum) -> LP m DottedNum
getNextNumber LaTeXState -> DottedNum
sLastTableNum
                     forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState
                       LaTeXState
st{ sLastTableNum :: DottedNum
sLastTableNum = DottedNum
num
                         , sLabels :: Map Text [Inline]
sLabels = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
lab
                                    [Text -> Inline
Str (DottedNum -> Text
renderDottedNum DottedNum
num)]
                                    (LaTeXState -> Map Text [Inline]
sLabels LaTeXState
st) }
                     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Maybe [Inline] -> Blocks -> Caption
caption forall a. Maybe a
Nothing (Inlines -> Blocks
plain Inlines
ils) -- add number??
                   (Maybe Inlines
Nothing, Maybe Text
_)  -> forall (m :: * -> *) a. Monad m => a -> m a
return Caption
c
          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
          forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Attr -> Block -> Block
addAttrDiv Attr
attr'
                 forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id forall a. Walkable [Inline] a => Text -> a -> a
removeLabel Maybe Text
mblabel
                 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 = 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]