{-
pandoc-crossref is a pandoc filter for numbering figures,
equations, tables and cross-references to them.
Copyright (C) 2015  Nikolay Yakimov <root@livid.pp.ru>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-}

{-# 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]