{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Writers.LaTeX.Table
   Copyright   : Copyright (C) 2006-2021 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Output LaTeX formatted tables.
-}
module Text.Pandoc.Writers.LaTeX.Table
  ( tableToLaTeX
  ) where
import Control.Monad.State.Strict
import Data.List (intersperse)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import Text.Pandoc.Definition
import Text.DocLayout
  ( Doc, braces, cr, empty, hcat, hsep, isEmpty, literal, nest
  , text, vcat, ($$) )
import Text.Pandoc.Shared (blocksToInlines, splitBy, tshow)
import Text.Pandoc.Walk (walk)
import Text.Pandoc.Writers.LaTeX.Caption (getCaption)
import Text.Pandoc.Writers.LaTeX.Notes (notesToLaTeX)
import Text.Pandoc.Writers.LaTeX.Types
  ( LW, WriterState (stBeamer, stExternalNotes, stInMinipage, stMultiRow
                    , stNotes, stTable) )
import Text.Printf (printf)
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.Writers.AnnotatedTable as Ann

tableToLaTeX :: PandocMonad m
             => ([Inline] -> LW m (Doc Text))
             -> ([Block]  -> LW m (Doc Text))
             -> Ann.Table
             -> LW m (Doc Text)
tableToLaTeX :: ([Inline] -> LW m (Doc Text))
-> ([Block] -> LW m (Doc Text)) -> Table -> LW m (Doc Text)
tableToLaTeX [Inline] -> LW m (Doc Text)
inlnsToLaTeX [Block] -> LW m (Doc Text)
blksToLaTeX Table
tbl = do
  let (Ann.Table Attr
_attr Caption
caption [ColSpec]
_specs TableHead
thead [TableBody]
tbodies TableFoot
tfoot) = Table
tbl
  CaptionDocs Doc Text
capt Doc Text
captNotes <- ([Inline] -> LW m (Doc Text)) -> Caption -> LW m CaptionDocs
forall (m :: * -> *).
PandocMonad m =>
([Inline] -> LW m (Doc Text)) -> Caption -> LW m CaptionDocs
captionToLaTeX [Inline] -> LW m (Doc Text)
inlnsToLaTeX Caption
caption
  let removeNote :: Inline -> Inline
removeNote (Note [Block]
_) = Attr -> [Inline] -> Inline
Span (Text
"", [], []) []
      removeNote Inline
x        = Inline
x
  Doc Text
firsthead <- if Doc Text -> Bool
forall a. Doc a -> Bool
isEmpty Doc Text
capt Bool -> Bool -> Bool
|| TableHead -> Bool
isEmptyHead TableHead
thead
               then Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
empty
               else (Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ String -> Doc Text
forall a. HasChars a => String -> Doc a
text String
"\\endfirsthead") (Doc Text -> Doc Text) -> LW m (Doc Text) -> LW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                    ([Block] -> LW m (Doc Text)) -> TableHead -> LW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
BlocksWriter m -> TableHead -> LW m (Doc Text)
headToLaTeX [Block] -> LW m (Doc Text)
blksToLaTeX TableHead
thead
  Doc Text
head' <- if TableHead -> Bool
isEmptyHead TableHead
thead
           then Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
"\\toprule"
           -- avoid duplicate notes in head and firsthead:
           else ([Block] -> LW m (Doc Text)) -> TableHead -> LW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
BlocksWriter m -> TableHead -> LW m (Doc Text)
headToLaTeX [Block] -> LW m (Doc Text)
blksToLaTeX
                (if Doc Text -> Bool
forall a. Doc a -> Bool
isEmpty Doc Text
firsthead
                 then TableHead
thead
                 else (Inline -> Inline) -> TableHead -> TableHead
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
removeNote TableHead
thead)
  [Doc Text]
rows' <- ([Cell] -> LW m (Doc Text))
-> [[Cell]] -> StateT WriterState m [Doc Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (([Block] -> LW m (Doc Text))
-> CellType -> [Cell] -> LW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
BlocksWriter m -> CellType -> [Cell] -> LW m (Doc Text)
rowToLaTeX [Block] -> LW m (Doc Text)
blksToLaTeX CellType
BodyCell) ([[Cell]] -> StateT WriterState m [Doc Text])
-> [[Cell]] -> StateT WriterState m [Doc Text]
forall a b. (a -> b) -> a -> b
$
                [[[Cell]]] -> [[Cell]]
forall a. Monoid a => [a] -> a
mconcat ((TableBody -> [[Cell]]) -> [TableBody] -> [[[Cell]]]
forall a b. (a -> b) -> [a] -> [b]
map TableBody -> [[Cell]]
bodyRows [TableBody]
tbodies) [[Cell]] -> [[Cell]] -> [[Cell]]
forall a. Semigroup a => a -> a -> a
<> TableFoot -> [[Cell]]
footRows TableFoot
tfoot
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ stTable :: Bool
stTable = Bool
True }
  Doc Text
notes <- [Doc Text] -> Doc Text
notesToLaTeX ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text] -> LW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (WriterState -> [Doc Text]) -> StateT WriterState m [Doc Text]
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> [Doc Text]
stNotes
  Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return
    (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
forall a b. (a -> b) -> a -> b
$  Doc Text
"\\begin{longtable}[]" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
          Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces (Doc Text
"@{}" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Table -> Doc Text
colDescriptors Table
tbl Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"@{}")
          -- the @{} removes extra space at beginning and end
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
capt
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
firsthead
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
head'
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"\\endhead"
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat [Doc Text]
rows'
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"\\bottomrule"
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"\\end{longtable}"
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
captNotes
    Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
notes

-- | Creates column descriptors for the table.
colDescriptors :: Ann.Table -> Doc Text
colDescriptors :: Table -> Doc Text
colDescriptors (Ann.Table Attr
_attr Caption
_caption [ColSpec]
specs TableHead
thead [TableBody]
tbodies TableFoot
tfoot) =
  let ([Alignment]
aligns, [ColWidth]
widths) = [ColSpec] -> ([Alignment], [ColWidth])
forall a b. [(a, b)] -> ([a], [b])
unzip [ColSpec]
specs

      defaultWidthsOnly :: Bool
defaultWidthsOnly = (ColWidth -> Bool) -> [ColWidth] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (ColWidth -> ColWidth -> Bool
forall a. Eq a => a -> a -> Bool
== ColWidth
ColWidthDefault) [ColWidth]
widths
      isSimpleTable :: Bool
isSimpleTable = ([Cell] -> Bool) -> [[Cell]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Cell -> Bool) -> [Cell] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Cell -> Bool
isSimpleCell) ([[Cell]] -> Bool) -> [[Cell]] -> Bool
forall a b. (a -> b) -> a -> b
$ [[[Cell]]] -> [[Cell]]
forall a. Monoid a => [a] -> a
mconcat
                      [ TableHead -> [[Cell]]
headRows TableHead
thead
                      , (TableBody -> [[Cell]]) -> [TableBody] -> [[Cell]]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap TableBody -> [[Cell]]
bodyRows [TableBody]
tbodies
                      , TableFoot -> [[Cell]]
footRows TableFoot
tfoot
                      ]

      relativeWidths :: [Double]
relativeWidths = if Bool
defaultWidthsOnly
                       then Int -> Double -> [Double]
forall a. Int -> a -> [a]
replicate ([ColSpec] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ColSpec]
specs)
                            (Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([ColSpec] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ColSpec]
specs))
                       else (ColWidth -> Double) -> [ColWidth] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map ColWidth -> Double
toRelWidth [ColWidth]
widths
  in if Bool
defaultWidthsOnly Bool -> Bool -> Bool
&& Bool
isSimpleTable
     then [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
hcat ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
forall a b. (a -> b) -> a -> b
$ (Alignment -> Doc Text) -> [Alignment] -> [Doc Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> (Alignment -> Text) -> Alignment -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Alignment -> Text
colAlign) [Alignment]
aligns
     else (Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>) (Doc Text -> Doc Text)
-> ([Text] -> Doc Text) -> [Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc Text -> Doc Text
forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2 (Doc Text -> Doc Text)
-> ([Text] -> Doc Text) -> [Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat ([Doc Text] -> Doc Text)
-> ([Text] -> [Doc Text]) -> [Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Doc Text) -> [Text] -> [Doc Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal ([Text] -> Doc Text) -> [Text] -> Doc Text
forall a b. (a -> b) -> a -> b
$
          (Alignment -> Double -> Text) -> [Alignment] -> [Double] -> [Text]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Int -> Alignment -> Double -> Text
toColDescriptor ([ColSpec] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ColSpec]
specs))
                  [Alignment]
aligns
                  [Double]
relativeWidths
  where
    toColDescriptor :: Int -> Alignment -> Double -> Text
    toColDescriptor :: Int -> Alignment -> Double -> Text
toColDescriptor Int
numcols Alignment
align Double
width =
      String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ String -> String -> Int -> Double -> String
forall r. PrintfType r => String -> r
printf
      String
">{%s\\arraybackslash}p{(\\columnwidth - %d\\tabcolsep) * \\real{%0.2f}}"
      (Text -> String
T.unpack (Alignment -> Text
alignCommand Alignment
align))
      ((Int
numcols Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
      Double
width

    isSimpleCell :: Cell -> Bool
isSimpleCell (Ann.Cell NonEmpty ColSpec
_ ColNumber
_ (Cell Attr
_attr Alignment
_align RowSpan
_rowspan ColSpan
_colspan [Block]
blocks)) =
      case [Block]
blocks of
        [Para [Inline]
_]  -> Bool
True
        [Plain [Inline]
_] -> Bool
True
        []        -> Bool
True
        [Block]
_         -> Bool
False

    toRelWidth :: ColWidth -> Double
toRelWidth ColWidth
ColWidthDefault = Double
0
    toRelWidth (ColWidth Double
w)    = Double
w

alignCommand :: Alignment -> Text
alignCommand :: Alignment -> Text
alignCommand = \case
  Alignment
AlignLeft    -> Text
"\\raggedright"
  Alignment
AlignRight   -> Text
"\\raggedleft"
  Alignment
AlignCenter  -> Text
"\\centering"
  Alignment
AlignDefault -> Text
"\\raggedright"

colAlign :: Alignment -> Text
colAlign :: Alignment -> Text
colAlign = \case
  Alignment
AlignLeft    -> Text
"l"
  Alignment
AlignRight   -> Text
"r"
  Alignment
AlignCenter  -> Text
"c"
  Alignment
AlignDefault -> Text
"l"

data CaptionDocs =
  CaptionDocs
  { CaptionDocs -> Doc Text
captionCommand :: Doc Text
  , CaptionDocs -> Doc Text
captionNotes :: Doc Text
  }

captionToLaTeX :: PandocMonad m
               => ([Inline] -> LW m (Doc Text))
               -> Caption
               -> LW m CaptionDocs
captionToLaTeX :: ([Inline] -> LW m (Doc Text)) -> Caption -> LW m CaptionDocs
captionToLaTeX [Inline] -> LW m (Doc Text)
inlnsToLaTeX (Caption Maybe [Inline]
_maybeShort [Block]
longCaption) = do
  let caption :: [Inline]
caption = [Block] -> [Inline]
blocksToInlines [Block]
longCaption
  (Doc Text
captionText, Doc Text
captForLof, Doc Text
captNotes) <- ([Inline] -> LW m (Doc Text))
-> Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
forall (m :: * -> *).
PandocMonad m =>
([Inline] -> LW m (Doc Text))
-> Bool -> [Inline] -> LW m (Doc Text, Doc Text, Doc Text)
getCaption [Inline] -> LW m (Doc Text)
inlnsToLaTeX Bool
False [Inline]
caption
  CaptionDocs -> LW m CaptionDocs
forall (m :: * -> *) a. Monad m => a -> m a
return (CaptionDocs -> LW m CaptionDocs)
-> CaptionDocs -> LW m CaptionDocs
forall a b. (a -> b) -> a -> b
$ CaptionDocs :: Doc Text -> Doc Text -> CaptionDocs
CaptionDocs
    { captionNotes :: Doc Text
captionNotes = Doc Text
captNotes
    , captionCommand :: Doc Text
captionCommand = if Doc Text -> Bool
forall a. Doc a -> Bool
isEmpty Doc Text
captionText
                       then Doc Text
forall a. Doc a
empty
                       else Doc Text
"\\caption" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
captForLof Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
                            Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
captionText Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\tabularnewline"
    }

type BlocksWriter m = [Block] -> LW m (Doc Text)

headToLaTeX :: PandocMonad m
            => BlocksWriter m
            -> Ann.TableHead
            -> LW m (Doc Text)
headToLaTeX :: BlocksWriter m -> TableHead -> LW m (Doc Text)
headToLaTeX BlocksWriter m
blocksWriter (Ann.TableHead Attr
_attr [HeaderRow]
headerRows) = do
  [Doc Text]
rowsContents <- (HeaderRow -> LW m (Doc Text))
-> [HeaderRow] -> StateT WriterState m [Doc Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (BlocksWriter m -> CellType -> [Cell] -> LW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
BlocksWriter m -> CellType -> [Cell] -> LW m (Doc Text)
rowToLaTeX BlocksWriter m
blocksWriter CellType
HeaderCell ([Cell] -> LW m (Doc Text))
-> (HeaderRow -> [Cell]) -> HeaderRow -> LW m (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeaderRow -> [Cell]
headerRowCells)
                       [HeaderRow]
headerRows
  Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text
"\\toprule" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat [Doc Text]
rowsContents Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"\\midrule")

-- | Converts a row of table cells into a LaTeX row.
rowToLaTeX :: PandocMonad m
           => BlocksWriter m
           -> CellType
           -> [Ann.Cell]
           -> LW m (Doc Text)
rowToLaTeX :: BlocksWriter m -> CellType -> [Cell] -> LW m (Doc Text)
rowToLaTeX BlocksWriter m
blocksWriter CellType
celltype [Cell]
row = do
  [Doc Text]
cellsDocs <- (Cell -> LW m (Doc Text))
-> [Cell] -> StateT WriterState m [Doc Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (BlocksWriter m -> CellType -> Cell -> LW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
BlocksWriter m -> CellType -> Cell -> LW m (Doc Text)
cellToLaTeX BlocksWriter m
blocksWriter CellType
celltype) ([Cell] -> [Cell]
fillRow [Cell]
row)
  Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
hsep (Doc Text -> [Doc Text] -> [Doc Text]
forall a. a -> [a] -> [a]
intersperse Doc Text
"&" [Doc Text]
cellsDocs) Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
" \\\\"

-- | Pads row with empty cells to adjust for rowspans above this row.
fillRow :: [Ann.Cell] -> [Ann.Cell]
fillRow :: [Cell] -> [Cell]
fillRow = Int -> [Cell] -> [Cell]
go Int
0
  where
    go :: Int -> [Cell] -> [Cell]
go Int
_ [] = []
    go Int
n (acell :: Cell
acell@(Ann.Cell NonEmpty ColSpec
_spec (Ann.ColNumber Int
colnum) Cell
cell):[Cell]
cells) =
      let (Cell Attr
_ Alignment
_ RowSpan
_ (ColSpan Int
colspan) [Block]
_) = Cell
cell
      in (Int -> Cell) -> [Int] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Cell
mkEmptyCell [Int
n .. Int
colnum Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] [Cell] -> [Cell] -> [Cell]
forall a. [a] -> [a] -> [a]
++
         Cell
acell Cell -> [Cell] -> [Cell]
forall a. a -> [a] -> [a]
: Int -> [Cell] -> [Cell]
go (Int
colnum Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
colspan) [Cell]
cells

    mkEmptyCell :: Int -> Ann.Cell
    mkEmptyCell :: Int -> Cell
mkEmptyCell Int
colnum =
      NonEmpty ColSpec -> ColNumber -> Cell -> Cell
Ann.Cell ((Alignment
AlignDefault, ColWidth
ColWidthDefault)ColSpec -> [ColSpec] -> NonEmpty ColSpec
forall a. a -> [a] -> NonEmpty a
:|[])
               (Int -> ColNumber
Ann.ColNumber Int
colnum)
               Cell
B.emptyCell

isEmptyHead :: Ann.TableHead -> Bool
isEmptyHead :: TableHead -> Bool
isEmptyHead (Ann.TableHead Attr
_attr []) = Bool
True
isEmptyHead (Ann.TableHead Attr
_attr [HeaderRow]
rows) = (HeaderRow -> Bool) -> [HeaderRow] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ([Cell] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Cell] -> Bool) -> (HeaderRow -> [Cell]) -> HeaderRow -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeaderRow -> [Cell]
headerRowCells) [HeaderRow]
rows

-- | Gets all cells in a header row.
headerRowCells :: Ann.HeaderRow -> [Ann.Cell]
headerRowCells :: HeaderRow -> [Cell]
headerRowCells (Ann.HeaderRow Attr
_attr RowNumber
_rownum [Cell]
cells) = [Cell]
cells

-- | Gets all cells in a body row.
bodyRowCells :: Ann.BodyRow -> [Ann.Cell]
bodyRowCells :: BodyRow -> [Cell]
bodyRowCells (Ann.BodyRow Attr
_attr RowNumber
_rownum [Cell]
rowhead [Cell]
cells) = [Cell]
rowhead [Cell] -> [Cell] -> [Cell]
forall a. Semigroup a => a -> a -> a
<> [Cell]
cells

-- | Gets a list of rows of the table body, where a row is a simple
-- list of cells.
bodyRows :: Ann.TableBody -> [[Ann.Cell]]
bodyRows :: TableBody -> [[Cell]]
bodyRows (Ann.TableBody Attr
_attr RowHeadColumns
_rowheads [HeaderRow]
headerRows [BodyRow]
rows) =
  (HeaderRow -> [Cell]) -> [HeaderRow] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
map HeaderRow -> [Cell]
headerRowCells [HeaderRow]
headerRows [[Cell]] -> [[Cell]] -> [[Cell]]
forall a. Semigroup a => a -> a -> a
<> (BodyRow -> [Cell]) -> [BodyRow] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
map BodyRow -> [Cell]
bodyRowCells [BodyRow]
rows

-- | Gets a list of rows of the table head, where a row is a simple
-- list of cells.
headRows :: Ann.TableHead -> [[Ann.Cell]]
headRows :: TableHead -> [[Cell]]
headRows (Ann.TableHead Attr
_attr [HeaderRow]
rows) = (HeaderRow -> [Cell]) -> [HeaderRow] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
map HeaderRow -> [Cell]
headerRowCells [HeaderRow]
rows

-- | Gets a list of rows from the foot, where a row is a simple list
-- of cells.
footRows :: Ann.TableFoot -> [[Ann.Cell]]
footRows :: TableFoot -> [[Cell]]
footRows (Ann.TableFoot Attr
_attr [HeaderRow]
rows) = (HeaderRow -> [Cell]) -> [HeaderRow] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
map HeaderRow -> [Cell]
headerRowCells [HeaderRow]
rows

-- For simple latex tables (without minipages or parboxes),
-- we need to go to some lengths to get line breaks working:
-- as LineBreak bs = \vtop{\hbox{\strut as}\hbox{\strut bs}}.
fixLineBreaks :: Block -> Block
fixLineBreaks :: Block -> Block
fixLineBreaks = ([Inline] -> [Inline]) -> Block -> Block
forall a b. Walkable a b => (a -> a) -> b -> b
walk [Inline] -> [Inline]
fixLineBreaks'

fixLineBreaks' :: [Inline] -> [Inline]
fixLineBreaks' :: [Inline] -> [Inline]
fixLineBreaks' [Inline]
ils = case (Inline -> Bool) -> [Inline] -> [[Inline]]
forall a. (a -> Bool) -> [a] -> [[a]]
splitBy (Inline -> Inline -> Bool
forall a. Eq a => a -> a -> Bool
== Inline
LineBreak) [Inline]
ils of
                       []     -> []
                       [[Inline]
xs]   -> [Inline]
xs
                       [[Inline]]
chunks -> Format -> Text -> Inline
RawInline Format
"tex" Text
"\\vtop{" Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:
                                 ([Inline] -> [Inline]) -> [[Inline]] -> [Inline]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap [Inline] -> [Inline]
tohbox [[Inline]]
chunks [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<>
                                 [Format -> Text -> Inline
RawInline Format
"tex" Text
"}"]
  where tohbox :: [Inline] -> [Inline]
tohbox [Inline]
ys = Format -> Text -> Inline
RawInline Format
"tex" Text
"\\hbox{\\strut " Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
ys [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<>
                    [Format -> Text -> Inline
RawInline Format
"tex" Text
"}"]

-- We also change display math to inline math, since display
-- math breaks in simple tables.
displayMathToInline :: Inline -> Inline
displayMathToInline :: Inline -> Inline
displayMathToInline (Math MathType
DisplayMath Text
x) = MathType -> Text -> Inline
Math MathType
InlineMath Text
x
displayMathToInline Inline
x                    = Inline
x

cellToLaTeX :: PandocMonad m
            => BlocksWriter m
            -> CellType
            -> Ann.Cell
            -> LW m (Doc Text)
cellToLaTeX :: BlocksWriter m -> CellType -> Cell -> LW m (Doc Text)
cellToLaTeX BlocksWriter m
blockListToLaTeX CellType
celltype Cell
annotatedCell = do
  let (Ann.Cell NonEmpty ColSpec
_specs ColNumber
_colnum Cell
cell) = Cell
annotatedCell
  let (Cell Attr
_attr Alignment
align RowSpan
rowspan ColSpan
colspan [Block]
blocks) = Cell
cell
  Bool
beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
stBeamer
  Bool
externalNotes <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
stExternalNotes
  Bool
inMinipage <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
stInMinipage
  -- See #5367 -- footnotehyper/footnote don't work in beamer,
  -- so we need to produce the notes outside the table...
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ stExternalNotes :: Bool
stExternalNotes = Bool
beamer }
  let isPlainOrPara :: Block -> Bool
isPlainOrPara = \case
        Para{}  -> Bool
True
        Plain{} -> Bool
True
        Block
_       -> Bool
False
  Doc Text
result <-
    if (Block -> Bool) -> [Block] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Block -> Bool
isPlainOrPara [Block]
blocks
       then
         BlocksWriter m
blockListToLaTeX BlocksWriter m -> BlocksWriter m
forall a b. (a -> b) -> a -> b
$ (Block -> Block) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Block -> Block
fixLineBreaks ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ (Inline -> Inline) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
displayMathToInline [Block]
blocks
       else do
         (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ stInMinipage :: Bool
stInMinipage = Bool
True }
         Doc Text
cellContents <- BlocksWriter m
blockListToLaTeX [Block]
blocks
         (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ stInMinipage :: Bool
stInMinipage = Bool
inMinipage }
         let valign :: Doc Text
valign = String -> Doc Text
forall a. HasChars a => String -> Doc a
text (String -> Doc Text) -> String -> Doc Text
forall a b. (a -> b) -> a -> b
$ case CellType
celltype of
                               CellType
HeaderCell -> String
"[b]"
                               CellType
BodyCell   -> String
"[t]"
         let halign :: Doc Text
halign = Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ Alignment -> Text
alignCommand Alignment
align
         Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"\\begin{minipage}" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
valign Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
                  Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
"\\linewidth" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
halign Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
                  Doc Text
cellContents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
                  Doc Text
"\\end{minipage}"
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ stExternalNotes :: Bool
stExternalNotes = Bool
externalNotes }
  Bool -> StateT WriterState m () -> StateT WriterState m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RowSpan
rowspan RowSpan -> RowSpan -> Bool
forall a. Eq a => a -> a -> Bool
/= Int -> RowSpan
RowSpan Int
1) (StateT WriterState m () -> StateT WriterState m ())
-> StateT WriterState m () -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$
    (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
st -> WriterState
st{ stMultiRow :: Bool
stMultiRow = Bool
True })
  let inMultiColumn :: Doc Text -> Doc Text
inMultiColumn Doc Text
x = case ColSpan
colspan of
                          (ColSpan Int
1) -> Doc Text
x
                          (ColSpan Int
n) -> Doc Text
"\\multicolumn"
                                         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Int -> Text
forall a. Show a => a -> Text
tshow Int
n))
                                         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ Alignment -> Text
colAlign Alignment
align)
                                         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
x
  let inMultiRow :: Doc Text -> Doc Text
inMultiRow Doc Text
x = case RowSpan
rowspan of
                       (RowSpan Int
1) -> Doc Text
x
                       (RowSpan Int
n) -> let nrows :: Doc Text
nrows = Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Int -> Text
forall a. Show a => a -> Text
tshow Int
n)
                                      in Doc Text
"\\multirow" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
nrows
                                         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
"*" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
braces Doc Text
x
  Doc Text -> LW m (Doc Text)
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> LW m (Doc Text))
-> (Doc Text -> Doc Text) -> Doc Text -> LW m (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Text -> Doc Text
inMultiColumn (Doc Text -> Doc Text)
-> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Text -> Doc Text
inMultiRow (Doc Text -> LW m (Doc Text)) -> Doc Text -> LW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
result

data CellType
  = HeaderCell
  | BodyCell