Lua: ensure that tables marshaled via JSON arrays behave like Lists.

This allows to invoke methods like `map` and `includes` on lists like
`PANDOC_WRITER_OPTIONS.extensions`.
This commit is contained in:
Albert Krewinkel 2022-06-23 22:49:32 +02:00
parent 2abf56f508
commit 6d351df1ed
No known key found for this signature in database
GPG key ID: 388DC0B21F631124
3 changed files with 12 additions and 3 deletions

View file

@ -540,9 +540,10 @@ library
build-depends: template-haskell
if flag(lua53)
build-depends: hslua >= 2.1 && < 2.2,
hslua-aeson >= 2.1 && < 2.3
hslua-aeson >= 2.2.1 && < 2.3
else
build-depends: hslua >= 2.2.1 && < 2.3
, hslua-aeson >= 2.2.1 && < 2.3
if flag(embed_data_files)
cpp-options: -DEMBED_DATA_FILES
other-modules: Text.Pandoc.Data

View file

@ -21,11 +21,12 @@ import Data.Maybe (catMaybes)
import HsLua as Lua hiding (status, try)
import Text.Pandoc.Class.PandocMonad (PandocMonad, readDataFile)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Lua.Marshal.List (pushListModule)
import Text.Pandoc.Lua.Marshal.List (newListMetatable, pushListModule)
import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua, runPandocLua)
import qualified Data.ByteString.Char8 as Char8
import qualified Data.Text as T
import qualified Lua.LPeg as LPeg
import qualified HsLua.Aeson
import qualified HsLua.Module.DocLayout as Module.Layout
import qualified HsLua.Module.Path as Module.Path
import qualified HsLua.Module.Text as Module.Text
@ -66,6 +67,7 @@ loadedModules =
initLuaState :: PandocLua ()
initLuaState = do
liftPandocLua Lua.openlibs
initJsonMetatable
initPandocModule
installLpegSearcher
setGlobalModules
@ -143,3 +145,9 @@ initLuaState = do
Lua.pushHaskellFunction $ Lua.state >>= liftIO . LPeg.lpeg_searcher
Lua.rawseti (Lua.nth 2) . (+1) . fromIntegral =<< Lua.rawlen (Lua.nth 2)
Lua.pop 1 -- remove 'package.searchers' from stack
-- | Setup the metatable that's assigned to Lua tables that were created
-- from/via JSON arrays.
initJsonMetatable :: PandocLua ()
initJsonMetatable = liftPandocLua $ do
newListMetatable HsLua.Aeson.jsonarray (pure ())

View file

@ -12,7 +12,7 @@ extra-deps:
- emojis-0.1.2
- lpeg-1.0.3
- hslua-2.2.1
- hslua-aeson-2.2.0
- hslua-aeson-2.2.1
- hslua-classes-2.2.0
- hslua-core-2.2.1
- hslua-marshalling-2.2.1