{-|
This module exports extra convenient builders, that don't have equivalents
in pandoc-types' Text.Pandoc.Builder.
-}

module Text.Pandoc.Builder.Monadic.Veneer
  (
  -- * Block list builders
    div'
  , span'
  , h1
  , h2
  , h3
  , h4
  , h5
  , tableWithColspec
  , strshow
  ) where

import Text.Pandoc.Builder.Monadic.Verbatim
  ( Builder, Inline(..), Block(..), ColSpec, header, simpleTable
  , divWith, spanWith, nullAttr, str
  )
import Text.Pandoc.Builder.Monadic.Utils

import qualified Data.Text as T

-- | Build a level 1 header.
h1 :: Builder Inline -> Builder Block
h1 :: Builder Inline -> Builder Block
h1 = Int -> Builder Inline -> Builder Block
header Int
1

-- | Build a level 2 header.
h2 :: Builder Inline -> Builder Block
h2 :: Builder Inline -> Builder Block
h2 = Int -> Builder Inline -> Builder Block
header Int
2

-- | Build a level 3 header.
h3 :: Builder Inline -> Builder Block
h3 :: Builder Inline -> Builder Block
h3 = Int -> Builder Inline -> Builder Block
header Int
3

-- | Build a level 4 header.
h4 :: Builder Inline -> Builder Block
h4 :: Builder Inline -> Builder Block
h4 = Int -> Builder Inline -> Builder Block
header Int
4

-- | Build a level 5 header.
h5 :: Builder Inline -> Builder Block
h5 :: Builder Inline -> Builder Block
h5 = Int -> Builder Inline -> Builder Block
header Int
5

-- | Colspans seem to be quite important for controlling
-- table layout, so much so that a version of
-- `Builder.SimpleTable` which takes a [ColSpan] seemed
-- in order.
tableWithColspec :: [ColSpec] -> [Builder Block] -> [[Builder Block]] -> Builder Block
tableWithColspec :: [ColSpec] -> [Builder Block] -> [[Builder Block]] -> Builder Block
tableWithColspec [ColSpec]
colspec [Builder Block]
headers [[Builder Block]]
rows = (Block -> Block) -> Builder Block -> Builder Block
forall a b. (a -> b) -> Builder a -> Builder b
mapBuilder Block -> Block
f (Builder Block -> Builder Block) -> Builder Block -> Builder Block
forall a b. (a -> b) -> a -> b
$ [Builder Block] -> [[Builder Block]] -> Builder Block
simpleTable [Builder Block]
headers [[Builder Block]]
rows
  where
    f :: Block -> Block
    f :: Block -> Block
f (Table Attr
attr Caption
caption [ColSpec]
_ TableHead
tableHead [TableBody]
tableBodies TableFoot
tableFoot)
      = Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
attr Caption
caption [ColSpec]
colspec TableHead
tableHead [TableBody]
tableBodies TableFoot
tableFoot
    f Block
a = Block
a

-- | Build a generic block container.
-- This would be named 'div', but that clashes with prelude's 'div'.
div' :: Builder Block -> Builder Block
div' :: Builder Block -> Builder Block
div' = Attr -> Builder Block -> Builder Block
divWith Attr
nullAttr

-- | Build a generic inline container.
-- This would be named 'span', but that clashes with prelude's 'span'.
span' :: Builder Inline -> Builder Inline
span' :: Builder Inline -> Builder Inline
span' = Attr -> Builder Inline -> Builder Inline
spanWith Attr
nullAttr

-- | Build a v'Str' using a values's `Show` instance.
strshow :: Show a => a -> Builder Inline
strshow :: forall a. Show a => a -> Builder Inline
strshow = Text -> Builder Inline
str (Text -> Builder Inline) -> (a -> Text) -> a -> Builder Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show