{-
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 <- forall r (m :: * -> *). MonadReader r m => m r
ask
  ShortCaption
idxStr <- Either Text Text
-> [(Text, Text)] -> ShortCaption -> SPrefix -> WS ShortCaption
replaceAttr (forall a b. b -> Either a b
Right Text
label) [(Text, Text)]
attrs ShortCaption
title SPrefix
SPfxTbl
  let title' :: ShortCaption
title' =
        case Options -> Maybe Format
outFormat Options
opts of
            Maybe Format
f | Maybe Format -> Bool
isLatexFormat Maybe Format
f ->
              Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"latex") (Text -> Text
mkLaTeXLabel Text
label) forall a. a -> [a] -> [a]
: ShortCaption
title
            Maybe Format
_  -> forall a b.
MkTemplate a b =>
ShortCaption -> ShortCaption -> b -> [a]
applyTemplate ShortCaption
idxStr ShortCaption
title 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
btitleforall a. a -> [a] -> [a]
:[Block]
rest)
  forall (m :: * -> *) a. Monad m => a -> m (ReplacedResult a)
replaceNoRecurse forall a b. (a -> b) -> a -> b
$ (Maybe Attr
mtattr forall a b. a -> (a -> b) -> b
&
    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) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure 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]