{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{- |
   Module      : Text.Pandoc.Lua.Module.Utils
   Copyright   : Copyright © 2017-2022 Albert Krewinkel
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
   Stability   : alpha

Utility module for Lua, exposing internal helper functions.
-}
module Text.Pandoc.Lua.Module.Utils
  ( documentedModule
  , sha1
  ) where

import Control.Applicative ((<|>))
import Control.Monad ((<$!>))
import Data.Data (showConstr, toConstr)
import Data.Default (def)
import Data.Maybe (fromMaybe)
import Data.Version (Version)
import HsLua as Lua
import HsLua.Module.Version (peekVersionFuzzy, pushVersion)
import Text.Pandoc.Citeproc (getReferences, processCitations)
import Text.Pandoc.Definition
import Text.Pandoc.Error (PandocError)
import Text.Pandoc.Lua.Marshal.AST
import Text.Pandoc.Lua.Marshal.Reference
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua))

import qualified Data.Digest.Pure.SHA as SHA
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Text.Pandoc.Builder as B
import qualified Text.Pandoc.Filter.JSON as JSONFilter
import qualified Text.Pandoc.Shared as Shared
import qualified Text.Pandoc.UTF8 as UTF8
import qualified Text.Pandoc.Writers.Shared as Shared

-- | Push the "pandoc.utils" module to the Lua stack.
documentedModule :: Module PandocError
documentedModule :: Module PandocError
documentedModule = Module
  { moduleName :: Name
moduleName = Name
"pandoc.utils"
  , moduleDescription :: Text
moduleDescription = Text
"pandoc utility functions"
  , moduleFields :: [Field PandocError]
moduleFields = []
  , moduleOperations :: [(Operation, DocumentedFunction PandocError)]
moduleOperations = []
  , moduleFunctions :: [DocumentedFunction PandocError]
moduleFunctions =
    [ forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"blocks_to_inlines"
      ### (\blks mSep -> do
              let sep = maybe Shared.defaultBlocksSeparator B.fromList mSep
              return $ B.toList (Shared.blocksToInlinesWithSep sep blks))
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList forall e. LuaError e => Peeker e Block
peekBlock) Text
"list of blocks"
            Text
"blocks" Text
""
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Parameter e a -> Parameter e (Maybe a)
opt (forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList forall e. LuaError e => Peeker e Inline
peekInline) Text
"list of inlines" Text
"inline" Text
"")
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => Pusher e [Inline]
pushInlines Text
"list of inlines" Text
""

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"citeproc"
      ### unPandocLua . processCitations
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e Pandoc
peekPandoc Text
"Pandoc" Text
"doc" Text
"document"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => Pusher e Pandoc
pushPandoc Text
"Pandoc" Text
"processed document"
      #?  T.unwords
          [ "Process the citations in the file, replacing them with "
          , "rendered citations and adding a bibliography. "
          , "See the manual section on citation rendering for details."
          ]

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"equals"
      ### equal
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"AST element" Text
"elem1" Text
""
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"AST element" Text
"elem2" Text
""
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. Pusher e Bool
pushBool Text
"boolean" Text
"true iff elem1 == elem2"

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"make_sections"
      ### liftPure3 Shared.makeSections
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. Peeker e Bool
peekBool Text
"boolean" Text
"numbering" Text
"add header numbers"
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (\StackIndex
i -> (forall a. Maybe a
Nothing forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall e. Peeker e ()
peekNil StackIndex
i) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall a. a -> Maybe a
Just forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> forall a e. (Integral a, Read a) => Peeker e a
peekIntegral StackIndex
i))
                    Text
"integer or nil" Text
"baselevel" Text
""
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList forall e. LuaError e => Peeker e Block
peekBlock) Text
"list of blocks"
            Text
"blocks" Text
"document blocks to process"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => Pusher e [Block]
pushBlocks Text
"list of Blocks"
            Text
"processes blocks"

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"normalize_date"
      ### liftPure Shared.normalizeDate
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. Peeker e Text
peekText Text
"string" Text
"date" Text
"the date string"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall e. LuaE e ()
pushnil forall e. Pusher e Text
pushText) Text
"string or nil"
            Text
"normalized date, or nil if normalization failed."
      #? T.unwords
      [ "Parse a date and convert (if possible) to \"YYYY-MM-DD\" format. We"
      , "limit years to the range 1601-9999 (ISO 8601 accepts greater than"
      , "or equal to 1583, but MS Word only accepts dates starting 1601)."
      , "Returns nil instead of a string if the conversion failed."
      ]

    , forall e. DocumentedFunction e
sha1

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"Version"
      ### liftPure (id @Version)
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e Version
peekVersionFuzzy
            Text
"version string, list of integers, or integer"
            Text
"v" Text
"version description"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => Pusher e Version
pushVersion Text
"Version" Text
"new Version object"
      #? "Creates a Version object."

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"references"
      ### (unPandocLua . getReferences Nothing)
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e Pandoc
peekPandoc Text
"Pandoc" Text
"doc" Text
"document"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult (forall e a. LuaError e => Pusher e a -> Pusher e [a]
pushPandocList forall e. LuaError e => Pusher e (Reference Inlines)
pushReference) Text
"table"
            Text
"lift of references"
      #? mconcat
         [ "Get references defined inline in the metadata and via an external "
         , "bibliography.  Only references that are actually cited in the "
         , "document (either with a genuine citation or with `nocite`) are "
         , "returned. URL variables are converted to links."
         ]

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"run_json_filter"
      ### (\doc filterPath margs -> do
              args <- case margs of
                        Just xs -> return xs
                        Nothing -> do
                          Lua.getglobal "FORMAT"
                          (forcePeek ((:[]) <$!> peekString top) <* pop 1)
              JSONFilter.apply def args filterPath doc
          )
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e Pandoc
peekPandoc Text
"Pandoc" Text
"doc" Text
"input document"
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. Peeker e String
peekString Text
"filepath" Text
"filter_path" Text
"path to filter"
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Parameter e a -> Parameter e (Maybe a)
opt (forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList forall e. Peeker e String
peekString) Text
"list of strings"
               Text
"args" Text
"arguments to pass to the filter")
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => Pusher e Pandoc
pushPandoc Text
"Pandoc" Text
"filtered document"

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"stringify"
      ### stringify
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"AST element" Text
"elem" Text
"some pandoc AST element"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. Pusher e Text
pushText Text
"string" Text
"stringified element"

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"from_simple_table"
      ### from_simple_table
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e SimpleTable
peekSimpleTable Text
"SimpleTable" Text
"simple_tbl" Text
""
      forall e.
HsFnPrecursor e (LuaE e NumResults) -> Text -> DocumentedFunction e
=?> Text
"Simple table"

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"to_roman_numeral"
      ### liftPure Shared.toRomanNumeral
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall a e. (Integral a, Read a) => Peeker e a
peekIntegral @Int) Text
"integer" Text
"n" Text
"number smaller than 4000"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. Pusher e Text
pushText Text
"string" Text
"roman numeral"
      #? "Converts a number < 4000 to uppercase roman numeral."

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"to_simple_table"
      ### to_simple_table
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall e. LuaError e => Peeker e Block
peekTable Text
"Block" Text
"tbl" Text
"a table"
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. LuaError e => SimpleTable -> LuaE e ()
pushSimpleTable Text
"SimpleTable" Text
"SimpleTable object"
      #? "Converts a table into an old/simple table."

    , forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"type"
      ### (\idx -> getmetafield idx "__name" >>= \case
              TypeString -> fromMaybe mempty <$> tostring top
              _ -> ltype idx >>= typename)
      forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"any" Text
"object" Text
""
      forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. Pusher e ByteString
pushByteString Text
"string" Text
"type of the given value"
    #? ("Pandoc-friendly version of Lua's default `type` function, " <>
        "returning the type of a value. If the argument has a " <>
        "string-valued metafield `__name`, then it gives that string. " <>
        "Otherwise it behaves just like the normal `type` function.")
    ]
  }

-- | Documented Lua function to compute the hash of a string.
sha1 :: DocumentedFunction e
sha1 :: forall e. DocumentedFunction e
sha1 = forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"sha1"
  ### liftPure (SHA.showDigest . SHA.sha1)
  forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
BSL.fromStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Peeker e ByteString
peekByteString) Text
"string" Text
"input" Text
""
  forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult forall e. String -> LuaE e ()
pushString Text
"string" Text
"hexadecimal hash value"
  #? "Compute the hash of the given string value."


-- | Convert pandoc structure to a string with formatting removed.
-- Footnotes are skipped (since we don't want their contents in link
-- labels).
stringify :: LuaError e => StackIndex -> LuaE e T.Text
stringify :: forall e. LuaError e => StackIndex -> LuaE e Text
stringify StackIndex
idx = forall e a. LuaError e => Peek e a -> LuaE e a
forcePeek forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Name -> Peek e a -> Peek e a
retrieving Name
"stringifyable element" forall a b. (a -> b) -> a -> b
$
  forall e a. LuaError e => [Peeker e a] -> Peeker e a
choice
  [ (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Walkable Inline a => a -> Text
Shared.stringify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e Pandoc
peekPandoc)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Walkable Inline a => a -> Text
Shared.stringify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e Inline
peekInline)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Walkable Inline a => a -> Text
Shared.stringify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e Block
peekBlock)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Walkable Inline a => a -> Text
Shared.stringify forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e Citation
peekCitation)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MetaValue -> Text
stringifyMetaValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e MetaValue
peekMetaValue)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const Text
"") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e Attr
peekAttr)
  , (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const Text
"") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. LuaError e => Peeker e ListAttributes
peekListAttributes)
  ] StackIndex
idx
 where
  stringifyMetaValue :: MetaValue -> T.Text
  stringifyMetaValue :: MetaValue -> Text
stringifyMetaValue MetaValue
mv = case MetaValue
mv of
    MetaBool Bool
b   -> Text -> Text
T.toLower forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (forall a. Show a => a -> String
show Bool
b)
    MetaString Text
s -> Text
s
    MetaList [MetaValue]
xs  -> forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map MetaValue -> Text
stringifyMetaValue [MetaValue]
xs
    MetaMap Map Text MetaValue
m    -> forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (MetaValue -> Text
stringifyMetaValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) (forall k a. Map k a -> [(k, a)]
Map.toList Map Text MetaValue
m)
    MetaValue
_            -> forall a. Walkable Inline a => a -> Text
Shared.stringify MetaValue
mv

-- | Converts an old/simple table into a normal table block element.
from_simple_table :: SimpleTable -> LuaE PandocError NumResults
from_simple_table :: SimpleTable -> LuaE PandocError NumResults
from_simple_table (SimpleTable [Inline]
capt [Alignment]
aligns [Double]
widths [[Block]]
head' [[[Block]]]
body) = do
  forall a e. (Pushable a, LuaError e) => a -> LuaE e ()
Lua.push forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table
    Attr
nullAttr
    (Maybe [Inline] -> [Block] -> Caption
Caption forall a. Maybe a
Nothing [[Inline] -> Block
Plain [Inline]
capt | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
capt)])
    (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Alignment
a Double
w -> (Alignment
a, Double -> ColWidth
toColWidth Double
w)) [Alignment]
aligns [Double]
widths)
    (Attr -> [Row] -> TableHead
TableHead Attr
nullAttr [[[Block]] -> Row
blockListToRow [[Block]]
head' | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
head') ])
    [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map [[Block]] -> Row
blockListToRow [[[Block]]]
body | Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[[Block]]]
body)]
    (Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr [])
  forall (m :: * -> *) a. Monad m => a -> m a
return (CInt -> NumResults
NumResults CInt
1)
  where
    blockListToRow :: [[Block]] -> Row
    blockListToRow :: [[Block]] -> Row
blockListToRow = Attr -> [Cell] -> Row
Row Attr
nullAttr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Blocks -> Cell
B.simpleCell forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> Many a
B.fromList)

    toColWidth :: Double -> ColWidth
    toColWidth :: Double -> ColWidth
toColWidth Double
0 = ColWidth
ColWidthDefault
    toColWidth Double
w = Double -> ColWidth
ColWidth Double
w

-- | Converts a table into an old/simple table.
to_simple_table :: Block -> LuaE PandocError SimpleTable
to_simple_table :: Block -> LuaE PandocError SimpleTable
to_simple_table = \case
  Table Attr
_attr Caption
caption [ColSpec]
specs TableHead
thead [TableBody]
tbodies TableFoot
tfoot -> do
    let ([Inline]
capt, [Alignment]
aligns, [Double]
widths, [[Block]]
headers, [[[Block]]]
rows) =
          Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> ([Inline], [Alignment], [Double], [[Block]], [[[Block]]])
Shared.toLegacyTable Caption
caption [ColSpec]
specs TableHead
thead [TableBody]
tbodies TableFoot
tfoot
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [Inline]
-> [Alignment]
-> [Double]
-> [[Block]]
-> [[[Block]]]
-> SimpleTable
SimpleTable [Inline]
capt [Alignment]
aligns [Double]
widths [[Block]]
headers [[[Block]]]
rows
  Block
blk -> forall e a. LuaError e => String -> LuaE e a
Lua.failLua forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
         [ String
"Expected Table, got ", Constr -> String
showConstr (forall a. Data a => a -> Constr
toConstr Block
blk), String
"." ]

peekTable :: LuaError e => Peeker e Block
peekTable :: forall e. LuaError e => Peeker e Block
peekTable StackIndex
idx = forall e. LuaError e => Peeker e Block
peekBlock StackIndex
idx forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  t :: Block
t@(Table {}) -> forall (m :: * -> *) a. Monad m => a -> m a
return Block
t
  Block
b -> forall a e. ByteString -> Peek e a
Lua.failPeek forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
       [ ByteString
"Expected Table, got "
       , String -> ByteString
UTF8.fromString forall a b. (a -> b) -> a -> b
$ Constr -> String
showConstr (forall a. Data a => a -> Constr
toConstr Block
b)
       , ByteString
"." ]