{-# 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)
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 :: forall e.
Name
-> Text
-> [Field e]
-> [DocumentedFunction e]
-> [(Operation, DocumentedFunction e)]
-> Module e
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 =
    [ Name
-> ([Block] -> Maybe [Inline] -> LuaE PandocError [Inline])
-> HsFnPrecursor
     PandocError
     ([Block] -> Maybe [Inline] -> LuaE PandocError [Inline])
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))
      HsFnPrecursor
  PandocError
  ([Block] -> Maybe [Inline] -> LuaE PandocError [Inline])
-> Parameter PandocError [Block]
-> HsFnPrecursor
     PandocError (Maybe [Inline] -> LuaE PandocError [Inline])
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError [Block]
-> Text -> Text -> Text -> Parameter PandocError [Block]
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter (Peeker PandocError Block -> Peeker PandocError [Block]
forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList Peeker PandocError Block
forall e. LuaError e => Peeker e Block
peekBlock) Text
"list of blocks"
            Text
"blocks" Text
""
      HsFnPrecursor
  PandocError (Maybe [Inline] -> LuaE PandocError [Inline])
-> Parameter PandocError (Maybe [Inline])
-> HsFnPrecursor PandocError (LuaE PandocError [Inline])
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError [Inline]
-> Text -> Text -> Text -> Parameter PandocError (Maybe [Inline])
forall e a.
Peeker e a -> Text -> Text -> Text -> Parameter e (Maybe a)
optionalParameter (Peeker PandocError Inline -> Peeker PandocError [Inline]
forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList Peeker PandocError Inline
forall e. LuaError e => Peeker e Inline
peekInline) Text
"list of inlines"
            Text
"inline" Text
""
      HsFnPrecursor PandocError (LuaE PandocError [Inline])
-> FunctionResults PandocError [Inline]
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError [Inline]
-> Text -> Text -> FunctionResults PandocError [Inline]
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher PandocError [Inline]
forall e. LuaError e => Pusher e [Inline]
pushInlines Text
"list of inlines" Text
""

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

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

    , Name
-> (Text -> LuaE PandocError (Maybe Text))
-> HsFnPrecursor
     PandocError (Text -> LuaE PandocError (Maybe Text))
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"normalize_date"
      ### liftPure Shared.normalizeDate
      HsFnPrecursor PandocError (Text -> LuaE PandocError (Maybe Text))
-> Parameter PandocError Text
-> HsFnPrecursor PandocError (LuaE PandocError (Maybe Text))
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Text
-> Text -> Text -> Text -> Parameter PandocError Text
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker PandocError Text
forall e. Peeker e Text
peekText Text
"string" Text
"date" Text
"the date string"
      HsFnPrecursor PandocError (LuaE PandocError (Maybe Text))
-> FunctionResults PandocError (Maybe Text)
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError (Maybe Text)
-> Text -> Text -> FunctionResults PandocError (Maybe Text)
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult (LuaE PandocError ()
-> (Text -> LuaE PandocError ()) -> Pusher PandocError (Maybe Text)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe LuaE PandocError ()
forall e. LuaE e ()
pushnil Text -> LuaE PandocError ()
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."
      ]

    , DocumentedFunction PandocError
forall e. DocumentedFunction e
sha1

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

    , Name
-> (Pandoc -> LuaE PandocError [Reference Inlines])
-> HsFnPrecursor
     PandocError (Pandoc -> LuaE PandocError [Reference Inlines])
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"references"
      ### (unPandocLua . getReferences Nothing)
      HsFnPrecursor
  PandocError (Pandoc -> LuaE PandocError [Reference Inlines])
-> Parameter PandocError Pandoc
-> HsFnPrecursor PandocError (LuaE PandocError [Reference Inlines])
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Pandoc
-> Text -> Text -> Text -> Parameter PandocError Pandoc
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker PandocError Pandoc
forall e. LuaError e => Peeker e Pandoc
peekPandoc Text
"Pandoc" Text
"doc" Text
"document"
      HsFnPrecursor PandocError (LuaE PandocError [Reference Inlines])
-> FunctionResults PandocError [Reference Inlines]
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError [Reference Inlines]
-> Text -> Text -> FunctionResults PandocError [Reference Inlines]
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult (Pusher PandocError (Reference Inlines)
-> Pusher PandocError [Reference Inlines]
forall e a. LuaError e => Pusher e a -> Pusher e [a]
pushPandocList Pusher PandocError (Reference Inlines)
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."
         ]

    , Name
-> (Pandoc -> String -> Maybe [String] -> LuaE PandocError Pandoc)
-> HsFnPrecursor
     PandocError
     (Pandoc -> String -> Maybe [String] -> LuaE PandocError Pandoc)
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
          )
      HsFnPrecursor
  PandocError
  (Pandoc -> String -> Maybe [String] -> LuaE PandocError Pandoc)
-> Parameter PandocError Pandoc
-> HsFnPrecursor
     PandocError (String -> Maybe [String] -> LuaE PandocError Pandoc)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Pandoc
-> Text -> Text -> Text -> Parameter PandocError Pandoc
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker PandocError Pandoc
forall e. LuaError e => Peeker e Pandoc
peekPandoc Text
"Pandoc" Text
"doc" Text
"input document"
      HsFnPrecursor
  PandocError (String -> Maybe [String] -> LuaE PandocError Pandoc)
-> Parameter PandocError String
-> HsFnPrecursor
     PandocError (Maybe [String] -> LuaE PandocError Pandoc)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError String
-> Text -> Text -> Text -> Parameter PandocError String
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker PandocError String
forall e. Peeker e String
peekString Text
"filepath" Text
"filter_path" Text
"path to filter"
      HsFnPrecursor
  PandocError (Maybe [String] -> LuaE PandocError Pandoc)
-> Parameter PandocError (Maybe [String])
-> HsFnPrecursor PandocError (LuaE PandocError Pandoc)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError [String]
-> Text -> Text -> Text -> Parameter PandocError (Maybe [String])
forall e a.
Peeker e a -> Text -> Text -> Text -> Parameter e (Maybe a)
optionalParameter (Peeker PandocError String -> Peeker PandocError [String]
forall a e. LuaError e => Peeker e a -> Peeker e [a]
peekList Peeker PandocError String
forall e. Peeker e String
peekString) Text
"list of strings"
            Text
"args" Text
"arguments to pass to the filter"
      HsFnPrecursor PandocError (LuaE PandocError Pandoc)
-> FunctionResults PandocError Pandoc
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError Pandoc
-> Text -> Text -> FunctionResults PandocError Pandoc
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher PandocError Pandoc
forall e. LuaError e => Pusher e Pandoc
pushPandoc Text
"Pandoc" Text
"filtered document"

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

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

    , Name
-> (Int -> LuaE PandocError Text)
-> HsFnPrecursor PandocError (Int -> LuaE PandocError Text)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"to_roman_numeral"
      ### liftPure Shared.toRomanNumeral
      HsFnPrecursor PandocError (Int -> LuaE PandocError Text)
-> Parameter PandocError Int
-> HsFnPrecursor PandocError (LuaE PandocError Text)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError Int
-> Text -> Text -> Text -> Parameter PandocError Int
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"
      HsFnPrecursor PandocError (LuaE PandocError Text)
-> FunctionResults PandocError Text
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> (Text -> LuaE PandocError ())
-> Text -> Text -> FunctionResults PandocError Text
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Text -> LuaE PandocError ()
forall e. Pusher e Text
pushText Text
"string" Text
"roman numeral"
      #? "Converts a number < 4000 to uppercase roman numeral."

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

    , Name
-> (StackIndex -> LuaE PandocError ByteString)
-> HsFnPrecursor
     PandocError (StackIndex -> LuaE PandocError ByteString)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"type"
      ### (\idx -> getmetafield idx "__name" >>= \case
              TypeString -> fromMaybe mempty <$> tostring top
              _ -> ltype idx >>= typename)
      HsFnPrecursor
  PandocError (StackIndex -> LuaE PandocError ByteString)
-> Parameter PandocError StackIndex
-> HsFnPrecursor PandocError (LuaE PandocError ByteString)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker PandocError StackIndex
-> Text -> Text -> Text -> Parameter PandocError StackIndex
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter Peeker PandocError StackIndex
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"any" Text
"object" Text
""
      HsFnPrecursor PandocError (LuaE PandocError ByteString)
-> FunctionResults PandocError ByteString
-> DocumentedFunction PandocError
forall e a.
HsFnPrecursor e (LuaE e a)
-> FunctionResults e a -> DocumentedFunction e
=#> Pusher PandocError ByteString
-> Text -> Text -> FunctionResults PandocError ByteString
forall e a. Pusher e a -> Text -> Text -> FunctionResults e a
functionResult Pusher PandocError ByteString
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 = Name
-> (ByteString -> LuaE e String)
-> HsFnPrecursor e (ByteString -> LuaE e String)
forall a e. Name -> a -> HsFnPrecursor e a
defun Name
"sha1"
  ### liftPure (SHA.showDigest . SHA.sha1)
  HsFnPrecursor e (ByteString -> LuaE e String)
-> Parameter e ByteString -> HsFnPrecursor e (LuaE e String)
forall e a b.
HsFnPrecursor e (a -> b) -> Parameter e a -> HsFnPrecursor e b
<#> Peeker e ByteString
-> Text -> Text -> Text -> Parameter e ByteString
forall e a. Peeker e a -> Text -> Text -> Text -> Parameter e a
parameter ((ByteString -> ByteString)
-> Peek e ByteString -> Peek e ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> ByteString
BSL.fromStrict (Peek e ByteString -> Peek e ByteString)
-> (StackIndex -> Peek e ByteString) -> Peeker e ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e ByteString
forall e. Peeker e ByteString
peekByteString) Text
"string" Text
"input" Text
""
  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 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 = Peek e Text -> LuaE e Text
forall e a. LuaError e => Peek e a -> LuaE e a
forcePeek (Peek e Text -> LuaE e Text)
-> (Peek e Text -> Peek e Text) -> Peek e Text -> LuaE e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Peek e Text -> Peek e Text
forall e a. Name -> Peek e a -> Peek e a
retrieving Name
"stringifyable element" (Peek e Text -> LuaE e Text) -> Peek e Text -> LuaE e Text
forall a b. (a -> b) -> a -> b
$
  [Peeker e Text] -> Peeker e Text
forall e a. LuaError e => [Peeker e a] -> Peeker e a
choice
  [ ((Pandoc -> Text) -> Peek e Pandoc -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pandoc -> Text
forall a. Walkable Inline a => a -> Text
Shared.stringify (Peek e Pandoc -> Peek e Text)
-> (StackIndex -> Peek e Pandoc) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Pandoc
forall e. LuaError e => Peeker e Pandoc
peekPandoc)
  , ((Inline -> Text) -> Peek e Inline -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Inline -> Text
forall a. Walkable Inline a => a -> Text
Shared.stringify (Peek e Inline -> Peek e Text)
-> (StackIndex -> Peek e Inline) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Inline
forall e. LuaError e => Peeker e Inline
peekInline)
  , ((Block -> Text) -> Peek e Block -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Block -> Text
forall a. Walkable Inline a => a -> Text
Shared.stringify (Peek e Block -> Peek e Text)
-> (StackIndex -> Peek e Block) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Block
forall e. LuaError e => Peeker e Block
peekBlock)
  , ((Citation -> Text) -> Peek e Citation -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Citation -> Text
forall a. Walkable Inline a => a -> Text
Shared.stringify (Peek e Citation -> Peek e Text)
-> (StackIndex -> Peek e Citation) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Citation
forall e. LuaError e => Peeker e Citation
peekCitation)
  , ((MetaValue -> Text) -> Peek e MetaValue -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap MetaValue -> Text
stringifyMetaValue (Peek e MetaValue -> Peek e Text)
-> (StackIndex -> Peek e MetaValue) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e MetaValue
forall e. LuaError e => Peeker e MetaValue
peekMetaValue)
  , ((Attr -> Text) -> Peek e Attr -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Attr -> Text
forall a b. a -> b -> a
const Text
"") (Peek e Attr -> Peek e Text)
-> (StackIndex -> Peek e Attr) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e Attr
forall e. LuaError e => Peeker e Attr
peekAttr)
  , ((ListAttributes -> Text) -> Peek e ListAttributes -> Peek e Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> ListAttributes -> Text
forall a b. a -> b -> a
const Text
"") (Peek e ListAttributes -> Peek e Text)
-> (StackIndex -> Peek e ListAttributes) -> Peeker e Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StackIndex -> Peek e ListAttributes
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 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (Bool -> String
forall a. Show a => a -> String
show Bool
b)
    MetaString Text
s -> Text
s
    MetaList [MetaValue]
xs  -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (MetaValue -> Text) -> [MetaValue] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map MetaValue -> Text
stringifyMetaValue [MetaValue]
xs
    MetaMap Map Text MetaValue
m    -> [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ ((Text, MetaValue) -> Text) -> [(Text, MetaValue)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (MetaValue -> Text
stringifyMetaValue (MetaValue -> Text)
-> ((Text, MetaValue) -> MetaValue) -> (Text, MetaValue) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, MetaValue) -> MetaValue
forall a b. (a, b) -> b
snd) (Map Text MetaValue -> [(Text, MetaValue)]
forall k a. Map k a -> [(k, a)]
Map.toList Map Text MetaValue
m)
    MetaValue
_            -> MetaValue -> Text
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
  Block -> LuaE PandocError ()
forall a e. (Pushable a, LuaError e) => a -> LuaE e ()
Lua.push (Block -> LuaE PandocError ()) -> Block -> LuaE PandocError ()
forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table
    Attr
nullAttr
    (Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
forall a. Maybe a
Nothing [[Inline] -> Block
Plain [Inline]
capt | Bool -> Bool
not ([Inline] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
capt)])
    ((Alignment -> Double -> ColSpec)
-> [Alignment] -> [Double] -> [ColSpec]
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 ([[Block]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
head') ])
    [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] ([Row] -> TableBody) -> [Row] -> TableBody
forall a b. (a -> b) -> a -> b
$ ([[Block]] -> Row) -> [[[Block]]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map [[Block]] -> Row
blockListToRow [[[Block]]]
body | Bool -> Bool
not ([[[Block]]] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[[Block]]]
body)]
    (Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr [])
  NumResults -> LuaE PandocError NumResults
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 ([Cell] -> Row) -> ([[Block]] -> [Cell]) -> [[Block]] -> Row
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Block] -> Cell) -> [[Block]] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map (Blocks -> Cell
B.simpleCell (Blocks -> Cell) -> ([Block] -> Blocks) -> [Block] -> Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> Blocks
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
    SimpleTable -> LuaE PandocError SimpleTable
forall (m :: * -> *) a. Monad m => a -> m a
return (SimpleTable -> LuaE PandocError SimpleTable)
-> SimpleTable -> LuaE PandocError SimpleTable
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 -> String -> LuaE PandocError SimpleTable
forall e a. LuaError e => String -> LuaE e a
Lua.failLua (String -> LuaE PandocError SimpleTable)
-> String -> LuaE PandocError SimpleTable
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall a. Monoid a => [a] -> a
mconcat
         [ String
"Expected Table, got ", Constr -> String
showConstr (Block -> Constr
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 = Peeker e Block
forall e. LuaError e => Peeker e Block
peekBlock StackIndex
idx Peek e Block -> (Block -> Peek e Block) -> Peek e Block
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  t :: Block
t@(Table {}) -> Block -> Peek e Block
forall (m :: * -> *) a. Monad m => a -> m a
return Block
t
  Block
b -> ByteString -> Peek e Block
forall a e. ByteString -> Peek e a
Lua.failPeek (ByteString -> Peek e Block) -> ByteString -> Peek e Block
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat
       [ ByteString
"Expected Table, got "
       , String -> ByteString
UTF8.fromString (String -> ByteString) -> String -> ByteString
forall a b. (a -> b) -> a -> b
$ Constr -> String
showConstr (Block -> Constr
forall a. Data a => a -> Constr
toConstr Block
b)
       , ByteString
"." ]