{-# LANGUAGE OverloadedStrings    #-}
{-# LANGUAGE LambdaCase           #-}
{- |
Copyright               : © 2021-2022 Albert Krewinkel
SPDX-License-Identifier : MIT
Maintainer              : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>

Marshaling/unmarshaling functions of table 'Cell' values.
-}
module Text.Pandoc.Lua.Marshal.Cell
  ( peekCell
  , peekCellFuzzy
  , pushCell
  , typeCell
  , mkCell
  ) where

import Control.Applicative (optional)
import Control.Monad ((<$!>))
import Data.Maybe (fromMaybe)
import HsLua
import Text.Pandoc.Lua.Marshal.Alignment (peekAlignment, pushAlignment)
import Text.Pandoc.Lua.Marshal.Attr (peekAttr, pushAttr)
import {-# SOURCE #-} Text.Pandoc.Lua.Marshal.Block
  ( peekBlocksFuzzy, pushBlocks )
import Text.Pandoc.Lua.Marshal.Filter (peekFilter)
import Text.Pandoc.Lua.Marshal.Shared (walkBlocksAndInlines)
import Text.Pandoc.Definition

-- | Push a table cell as a table with fields @attr@, @alignment@,
-- @row_span@, @col_span@, and @contents@.
pushCell :: LuaError e => Cell -> LuaE e ()
pushCell :: Cell -> LuaE e ()
pushCell = UDTypeWithList e (DocumentedFunction e) Cell Void
-> Cell -> LuaE e ()
forall e fn a itemtype.
LuaError e =>
UDTypeWithList e fn a itemtype -> a -> LuaE e ()
pushUD UDTypeWithList e (DocumentedFunction e) Cell Void
forall e. LuaError e => DocumentedType e Cell
typeCell

-- | Retrieves a 'Cell' object from the stack.
peekCell :: LuaError e => Peeker e Cell
peekCell :: Peeker e Cell
peekCell = UDTypeWithList e (DocumentedFunction e) Cell Void -> Peeker e Cell
forall e fn a itemtype.
LuaError e =>
UDTypeWithList e fn a itemtype -> Peeker e a
peekUD UDTypeWithList e (DocumentedFunction e) Cell Void
forall e. LuaError e => DocumentedType e Cell
typeCell

-- | Retrieves a 'Cell' from the stack, accepting either a 'pandoc Cell'
-- userdata object or a table with fields @attr@, @alignment@, @row_span@,
-- @col_span@, and @contents@.
peekCellFuzzy :: LuaError e => Peeker e Cell
peekCellFuzzy :: Peeker e Cell
peekCellFuzzy idx :: StackIndex
idx = LuaE e Type -> Peek e Type
forall e a. LuaE e a -> Peek e a
liftLua (StackIndex -> LuaE e Type
forall e. StackIndex -> LuaE e Type
ltype StackIndex
idx) Peek e Type -> (Type -> Peek e Cell) -> Peek e Cell
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  TypeUserdata -> Peeker e Cell
forall e. LuaError e => Peeker e Cell
peekCell StackIndex
idx
  TypeTable -> do
    Attr
attr <- Peeker e Attr -> Name -> Peeker e Attr
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw Peeker e Attr
forall e. LuaError e => Peeker e Attr
peekAttr "attr" StackIndex
idx
    Alignment
algn <- Peeker e Alignment -> Name -> Peeker e Alignment
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw Peeker e Alignment
forall e. Peeker e Alignment
peekAlignment "alignment" StackIndex
idx
    RowSpan
rs   <- Int -> RowSpan
RowSpan (Int -> RowSpan) -> Peek e Int -> Peek e RowSpan
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Peeker e Int -> Name -> Peeker e Int
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral "row_span" StackIndex
idx
    ColSpan
cs   <- Int -> ColSpan
ColSpan (Int -> ColSpan) -> Peek e Int -> Peek e ColSpan
forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Peeker e Int -> Name -> Peeker e Int
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral "col_span" StackIndex
idx
    [Block]
blks <- Peeker e [Block] -> Name -> Peeker e [Block]
forall e a. LuaError e => Peeker e a -> Name -> Peeker e a
peekFieldRaw Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy "contents" StackIndex
idx
    Cell -> Peek e Cell
forall (m :: * -> *) a. Monad m => a -> m a
return (Cell -> Peek e Cell) -> Cell -> Peek e Cell
forall a b. (a -> b) -> a -> b
$! Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
algn RowSpan
rs ColSpan
cs [Block]
blks
  _ -> ByteString -> Peek e Cell
forall a e. ByteString -> Peek e a
failPeek (ByteString -> Peek e Cell) -> Peek e ByteString -> Peek e Cell
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Name -> StackIndex -> Peek e ByteString
forall e. Name -> StackIndex -> Peek e ByteString
typeMismatchMessage "Cell or table" StackIndex
idx

-- | Cell object type.
typeCell :: LuaError e => DocumentedType e Cell
typeCell :: DocumentedType e Cell
typeCell = Name
-> [(Operation, DocumentedFunction e)]
-> [Member e (DocumentedFunction e) Cell]
-> DocumentedType e Cell
forall e a.
LuaError e =>
Name
-> [(Operation, DocumentedFunction e)]
-> [Member e (DocumentedFunction e) a]
-> DocumentedType e a
deftype "pandoc Cell"
  [ Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall e.
Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
operation Operation
Eq (DocumentedFunction e -> (Operation, DocumentedFunction e))
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall a b. (a -> b) -> a -> b
$ Name
-> (Maybe Cell -> Maybe Cell -> LuaE e Bool)
-> HsFnPrecursor e (Maybe Cell -> Maybe Cell -> LuaE e Bool)
forall a e. Name -> a -> HsFnPrecursor e a
defun "__eq"
     ### liftPure2 (\a b -> fromMaybe False ((==) <$> a <*> b))
     HsFnPrecursor e (Maybe Cell -> Maybe Cell -> LuaE e Bool)
-> Parameter e (Maybe Cell)
-> HsFnPrecursor e (Maybe Cell -> LuaE e Bool)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e (Maybe Cell)
-> Text -> Text -> Text -> Parameter e (Maybe Cell)
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (Peek e Cell -> Peek e (Maybe Cell)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peek e Cell -> Peek e (Maybe Cell))
-> (StackIndex -> Peek e Cell) -> Peeker e (Maybe Cell)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Cell
forall e. LuaError e => Peeker e Cell
peekCell) "Cell" "self" ""
     HsFnPrecursor e (Maybe Cell -> LuaE e Bool)
-> Parameter e (Maybe Cell) -> HsFnPrecursor e (LuaE e Bool)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e (Maybe Cell)
-> Text -> Text -> Text -> Parameter e (Maybe Cell)
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (Peek e Cell -> Peek e (Maybe Cell)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Peek e Cell -> Peek e (Maybe Cell))
-> (StackIndex -> Peek e Cell) -> Peeker e (Maybe Cell)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Cell
forall e. LuaError e => Peeker e Cell
peekCell) "any" "object" ""
     HsFnPrecursor e (LuaE e Bool)
-> FunctionResults e Bool -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e Bool -> Text -> Text -> FunctionResults e Bool
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e Bool
forall e. Pusher e Bool
pushBool "boolean" "true iff the two values are equal"
  , Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall e.
Operation
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
operation Operation
Tostring (DocumentedFunction e -> (Operation, DocumentedFunction e))
-> DocumentedFunction e -> (Operation, DocumentedFunction e)
forall a b. (a -> b) -> a -> b
$ (Cell -> LuaE e String) -> HsFnPrecursor e (Cell -> LuaE e String)
forall a e. a -> HsFnPrecursor e a
lambda
    ### liftPure show
    HsFnPrecursor e (Cell -> LuaE e String)
-> Parameter e Cell -> HsFnPrecursor e (LuaE e String)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> (StackIndex -> Peek e Cell)
-> Text -> Text -> Text -> Parameter e Cell
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter StackIndex -> Peek e Cell
forall e. LuaError e => Peeker e Cell
peekCell "Cell" "self" ""
    HsFnPrecursor e (LuaE e String)
-> FunctionResults e String -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e String -> Text -> Text -> FunctionResults e String
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e String
forall e. String -> LuaE e ()
pushString "string" "native Haskell representation"
  ]
  [ Name
-> Text
-> (Pusher e Attr, Cell -> Attr)
-> (Peeker e Attr, Cell -> Attr -> Cell)
-> Member e (DocumentedFunction e) Cell
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property "attr" "cell attributes"
      (Pusher e Attr
forall e. LuaError e => Pusher e Attr
pushAttr, \(Cell attr :: Attr
attr _ _ _ _) -> Attr
attr)
      (Peeker e Attr
forall e. LuaError e => Peeker e Attr
peekAttr, \(Cell _ align :: Alignment
align rs :: RowSpan
rs cs :: ColSpan
cs blks :: [Block]
blks) attr :: Attr
attr ->
                   Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
align RowSpan
rs ColSpan
cs [Block]
blks)
  , Name
-> Text
-> (Pusher e Alignment, Cell -> Alignment)
-> (Peeker e Alignment, Cell -> Alignment -> Cell)
-> Member e (DocumentedFunction e) Cell
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property "alignment" "alignment of cell contents"
      (Pusher e Alignment
forall e. Pusher e Alignment
pushAlignment, \(Cell _ align :: Alignment
align _ _ _) -> Alignment
align)
      (Peeker e Alignment
forall e. Peeker e Alignment
peekAlignment, \(Cell attr :: Attr
attr _ rs :: RowSpan
rs cs :: ColSpan
cs blks :: [Block]
blks) align :: Alignment
align ->
                        Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
align RowSpan
rs ColSpan
cs [Block]
blks)
  , Name
-> Text
-> (Pusher e Int, Cell -> Int)
-> (Peeker e Int, Cell -> Int -> Cell)
-> Member e (DocumentedFunction e) Cell
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property "row_span" "number of rows over which this cell spans"
      (Pusher e Int
forall a e. (Integral a, Show a) => a -> LuaE e ()
pushIntegral, \(Cell _ _ (RowSpan rs :: Int
rs) _ _) -> Int
rs)
      (Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral, \(Cell attr :: Attr
attr align :: Alignment
align _ cs :: ColSpan
cs blks :: [Block]
blks) rs :: Int
rs ->
                       Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
align (Int -> RowSpan
RowSpan Int
rs) ColSpan
cs [Block]
blks)
  , Name
-> Text
-> (Pusher e Int, Cell -> Int)
-> (Peeker e Int, Cell -> Int -> Cell)
-> Member e (DocumentedFunction e) Cell
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property "col_span" "number of columns over which this cell spans"
      (Pusher e Int
forall a e. (Integral a, Show a) => a -> LuaE e ()
pushIntegral, \(Cell _ _ _ (ColSpan rs :: Int
rs) _) -> Int
rs)
      (Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral, \(Cell attr :: Attr
attr align :: Alignment
align rs :: RowSpan
rs _ blks :: [Block]
blks) cs :: Int
cs ->
                       Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
align RowSpan
rs (Int -> ColSpan
ColSpan Int
cs) [Block]
blks)
  , Name
-> Text
-> (Pusher e [Block], Cell -> [Block])
-> (Peeker e [Block], Cell -> [Block] -> Cell)
-> Member e (DocumentedFunction e) Cell
forall e b a fn.
LuaError e =>
Name
-> Text
-> (Pusher e b, a -> b)
-> (Peeker e b, a -> b -> a)
-> Member e fn a
property "contents" "cell contents"
      (Pusher e [Block]
forall e. LuaError e => Pusher e [Block]
pushBlocks, \(Cell _ _ _ _ blks :: [Block]
blks) -> [Block]
blks)
      (Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy, \(Cell attr :: Attr
attr align :: Alignment
align rs :: RowSpan
rs cs :: ColSpan
cs _) blks :: [Block]
blks ->
                          Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
attr Alignment
align RowSpan
rs ColSpan
cs [Block]
blks)

  , AliasIndex
-> Text -> [AliasIndex] -> Member e (DocumentedFunction e) Cell
forall e fn a. AliasIndex -> Text -> [AliasIndex] -> Member e fn a
alias "content"    "alias for contents" ["contents"]
  , AliasIndex
-> Text -> [AliasIndex] -> Member e (DocumentedFunction e) Cell
forall e fn a. AliasIndex -> Text -> [AliasIndex] -> Member e fn a
alias "identifier" "cell ID"         ["attr", "identifier"]
  , AliasIndex
-> Text -> [AliasIndex] -> Member e (DocumentedFunction e) Cell
forall e fn a. AliasIndex -> Text -> [AliasIndex] -> Member e fn a
alias "classes"    "cell classes"    ["attr", "classes"]
  , AliasIndex
-> Text -> [AliasIndex] -> Member e (DocumentedFunction e) Cell
forall e fn a. AliasIndex -> Text -> [AliasIndex] -> Member e fn a
alias "attributes" "cell attributes" ["attr", "attributes"]

  , DocumentedFunction e -> Member e (DocumentedFunction e) Cell
forall e a.
DocumentedFunction e -> Member e (DocumentedFunction e) a
method (DocumentedFunction e -> Member e (DocumentedFunction e) Cell)
-> DocumentedFunction e -> Member e (DocumentedFunction e) Cell
forall a b. (a -> b) -> a -> b
$ Name
-> (Cell -> Filter -> LuaE e Cell)
-> HsFnPrecursor e (Cell -> Filter -> LuaE e Cell)
forall a e. Name -> a -> HsFnPrecursor e a
defun "walk"
    ### flip walkBlocksAndInlines
    HsFnPrecursor e (Cell -> Filter -> LuaE e Cell)
-> Parameter e Cell -> HsFnPrecursor e (Filter -> LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> (StackIndex -> Peek e Cell)
-> Text -> Text -> Text -> Parameter e Cell
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter StackIndex -> Peek e Cell
forall e. LuaError e => Peeker e Cell
peekCell "Cell" "self" ""
    HsFnPrecursor e (Filter -> LuaE e Cell)
-> Parameter e Filter -> HsFnPrecursor e (LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e Filter -> Text -> Text -> Text -> Parameter e Filter
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e Filter
forall e. LuaError e => Peeker e Filter
peekFilter "Filter" "lua_filter" "table of filter functions"
    HsFnPrecursor e (LuaE e Cell)
-> FunctionResults e Cell -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e Cell -> Text -> Text -> FunctionResults e Cell
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e Cell
forall e. LuaError e => Cell -> LuaE e ()
pushCell "Cell" "modified cell"
  ]

-- | Constructor function for 'Cell' values.
mkCell :: LuaError e => DocumentedFunction e
mkCell :: DocumentedFunction e
mkCell = Name
-> ([Block]
    -> Maybe Alignment
    -> Maybe Int
    -> Maybe Int
    -> Maybe Attr
    -> LuaE e Cell)
-> HsFnPrecursor
     e
     ([Block]
      -> Maybe Alignment
      -> Maybe Int
      -> Maybe Int
      -> Maybe Attr
      -> LuaE e Cell)
forall a e. Name -> a -> HsFnPrecursor e a
defun "Cell"
  ### liftPure5 (\blocks mAlign mRowSpan mColSpan mAttr -> Cell
                  (fromMaybe nullAttr mAttr)
                  (fromMaybe AlignDefault mAlign)
                  (maybe 1 RowSpan mRowSpan)
                  (maybe 1 ColSpan mColSpan)
                  blocks)
  HsFnPrecursor
  e
  ([Block]
   -> Maybe Alignment
   -> Maybe Int
   -> Maybe Int
   -> Maybe Attr
   -> LuaE e Cell)
-> Parameter e [Block]
-> HsFnPrecursor
     e
     (Maybe Alignment
      -> Maybe Int -> Maybe Int -> Maybe Attr -> LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e [Block] -> Text -> Text -> Text -> Parameter e [Block]
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e [Block]
forall e. LuaError e => Peeker e [Block]
peekBlocksFuzzy "Blocks" "blocks" "document contents"
  HsFnPrecursor
  e
  (Maybe Alignment
   -> Maybe Int -> Maybe Int -> Maybe Attr -> LuaE e Cell)
-> Parameter e (Maybe Alignment)
-> HsFnPrecursor
     e (Maybe Int -> Maybe Int -> Maybe Attr -> LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e Alignment -> Parameter e (Maybe Alignment)
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e Alignment -> Text -> Text -> Text -> Parameter e Alignment
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e Alignment
forall e. Peeker e Alignment
peekAlignment "integer" "align" "cell alignment")
  HsFnPrecursor
  e (Maybe Int -> Maybe Int -> Maybe Attr -> LuaE e Cell)
-> Parameter e (Maybe Int)
-> HsFnPrecursor e (Maybe Int -> Maybe Attr -> LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e Int -> Parameter e (Maybe Int)
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e Int -> Text -> Text -> Text -> Parameter e Int
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral "integer" "row_span" "rows to span")
  HsFnPrecursor e (Maybe Int -> Maybe Attr -> LuaE e Cell)
-> Parameter e (Maybe Int)
-> HsFnPrecursor e (Maybe Attr -> LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e Int -> Parameter e (Maybe Int)
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e Int -> Text -> Text -> Text -> Parameter e Int
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e Int
forall a e. (Integral a, Read a) => Peeker e a
peekIntegral "integer" "col_span" "columns to span")
  HsFnPrecursor e (Maybe Attr -> LuaE e Cell)
-> Parameter e (Maybe Attr) -> HsFnPrecursor e (LuaE e Cell)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Parameter e Attr -> Parameter e (Maybe Attr)
forall e a. Parameter e a -> Parameter e (Maybe a)
opt (Peeker e Attr -> Text -> Text -> Text -> Parameter e Attr
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker e Attr
forall e. LuaError e => Peeker e Attr
peekAttr "Attr" "attr" "cell attributes")
  HsFnPrecursor e (LuaE e Cell)
-> FunctionResults e Cell -> DocumentedFunction e
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher e Cell -> Text -> Text -> FunctionResults e Cell
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher e Cell
forall e. LuaError e => Cell -> LuaE e ()
pushCell "Cell" "new Cell object"