aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2021-12-21 09:40:23 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2021-12-21 09:24:21 -0800
commitd7cab5198269fbbdbc40f54a2ad7aeb83fee619f (patch)
tree2888f841a1c5ac6fe2dec545eff8f1c3e1cf49c4 /src
parentc90802d7d85ba2ae98492701b30cc37bde757b83 (diff)
downloadpandoc-d7cab5198269fbbdbc40f54a2ad7aeb83fee619f.tar.gz
Lua: add new library function `pandoc.utils.type`.
The function behaves like the default `type` function from Lua's standard library, but is aware of pandoc userdata types. A typical use-case would be to determine the type of a metadata value.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Lua/Module/Utils.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Lua/Module/Utils.hs b/src/Text/Pandoc/Lua/Module/Utils.hs
index 439a9a50b..c1bb42410 100644
--- a/src/Text/Pandoc/Lua/Module/Utils.hs
+++ b/src/Text/Pandoc/Lua/Module/Utils.hs
@@ -21,6 +21,7 @@ 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.Class.Peekable (PeekError)
@@ -145,6 +146,17 @@ documentedModule = Module
<#> parameter peekTable "Block" "tbl" "a table"
=#> functionResult pushSimpleTable "SimpleTable" "SimpleTable object"
#? "Converts a table into an old/simple table."
+
+ , defun "type"
+ ### (\idx -> getmetafield idx "__name" >>= \case
+ TypeString -> fromMaybe mempty <$> tostring top
+ _ -> ltype idx >>= typename)
+ <#> parameter pure "any" "object" ""
+ =#> functionResult pushByteString "string" "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.")
]
}