{-# LANGUAGE Rank2Types, OverloadedStrings, FlexibleContexts #-}
module Text.Pandoc.CrossRef.References.Blocks.Table where
import Control.Monad.Reader.Class
import Text.Pandoc.Definition
import Text.Pandoc.Shared (blocksToInlines)
import Data.Function ((&))
import Text.Pandoc.CrossRef.References.Monad
import Text.Pandoc.CrossRef.References.Blocks.Util
import Text.Pandoc.CrossRef.Util.Options
import Text.Pandoc.CrossRef.Util.Template
import Text.Pandoc.CrossRef.Util.Util
runTable :: Attr -> Maybe Attr -> Maybe ShortCaption -> Block -> [Block] -> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> WS (ReplacedResult Block)
runTable :: Attr
-> Maybe Attr
-> Maybe ShortCaption
-> Block
-> [Block]
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> WS (ReplacedResult Block)
runTable (Text
label, [Text]
clss, [(Text, Text)]
attrs) Maybe Attr
mtattr Maybe ShortCaption
short Block
btitle [Block]
rest [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot = do
Options
opts <- WS Options
forall r (m :: * -> *). MonadReader r m => m r
ask
ShortCaption
idxStr <- Either Text Text
-> [(Text, Text)] -> ShortCaption -> SPrefix -> WS ShortCaption
replaceAttr (Text -> Either Text Text
forall a b. b -> Either a b
Right Text
label) [(Text, Text)]
attrs ShortCaption
title SPrefix
SPfxTbl
let title' :: ShortCaption
title'
| Options -> Bool
isLatexFormat Options
opts = Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Text
mkLaTeXLabel Text
label) Inline -> ShortCaption -> ShortCaption
forall a. a -> [a] -> [a]
: ShortCaption
title
| Bool
otherwise = ShortCaption -> ShortCaption -> Template -> ShortCaption
forall a b.
MkTemplate a b =>
ShortCaption -> ShortCaption -> b -> [a]
applyTemplate ShortCaption
idxStr ShortCaption
title (Template -> ShortCaption) -> Template -> ShortCaption
forall a b. (a -> b) -> a -> b
$ Options -> Template
tableTemplate Options
opts
caption' :: Caption
caption' = Maybe ShortCaption -> [Block] -> Caption
Caption Maybe ShortCaption
short (ShortCaption -> ShortCaption -> Block -> Block
walkReplaceInlines ShortCaption
title' ShortCaption
title Block
btitleBlock -> [Block] -> [Block]
forall a. a -> [a] -> [a]
:[Block]
rest)
label' :: Text
label' | Options -> Bool
isLatexFormat Options
opts = Text
""
| Bool
otherwise = Text
label
Block -> WS (ReplacedResult Block)
forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse (Block -> WS (ReplacedResult Block))
-> Block -> WS (ReplacedResult Block)
forall a b. (a -> b) -> a -> b
$ (Maybe Attr
mtattr Maybe Attr
-> (Maybe Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block)
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
forall a b. a -> (a -> b) -> b
&
(Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Block)
-> (Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block)
-> Maybe Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
(Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table (Text
label, [Text]
clss, Options -> ShortCaption -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts ShortCaption
idxStr [(Text, Text)]
attrs))
(\Attr
tattr Caption
a [ColSpec]
b TableHead
c [TableBody]
d -> Attr -> [Block] -> Block
Div (Text
label', [Text]
clss, Options -> ShortCaption -> [(Text, Text)] -> [(Text, Text)]
setLabel Options
opts ShortCaption
idxStr [(Text, Text)]
attrs) ([Block] -> Block) -> (TableFoot -> [Block]) -> TableFoot -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> [Block]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Block -> [Block]) -> (TableFoot -> Block) -> TableFoot -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
tattr Caption
a [ColSpec]
b TableHead
c [TableBody]
d)
)
Caption
caption' [ColSpec]
colspec TableHead
header [TableBody]
cells TableFoot
foot
where title :: ShortCaption
title = [Block] -> ShortCaption
blocksToInlines [Block
btitle]