aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2024-06-01 20:56:11 +0200
committerAlbert Krewinkel <[email protected]>2024-06-01 21:10:29 +0200
commit83de55bb357f66984edaf1cd699402dbe4cde5c0 (patch)
treeace4cb05a91d2b5cb9b75c1eabee68f97923f2bc /pandoc-lua-engine
parent075ed1052f1739a96317b692b5aa61c03e6ac833 (diff)
Lua: Set `pandoc.List` as default metatable for JSON lists
Lists created by `pandoc.json.decode` now behave like lists generated via `pandoc.List`. This also ensures that `pandoc.List` tables are encoded as JSON arrays when passed to `pandoc.json.encode`. Fixes: #9834
Diffstat (limited to 'pandoc-lua-engine')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs8
-rw-r--r--pandoc-lua-engine/test/lua/module/pandoc-json.lua19
2 files changed, 20 insertions, 7 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
index e0dd830b2..1e2b628fd 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Init.hs
@@ -28,7 +28,7 @@ import Text.Pandoc.Data (readDataFile)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Logging (LogMessage (ScriptingWarning))
import Text.Pandoc.Lua.Global (Global (..), setGlobals)
-import Text.Pandoc.Lua.Marshal.List (newListMetatable, pushListModule)
+import Text.Pandoc.Lua.Marshal.List (pushPandocList, pushListModule)
import Text.Pandoc.Lua.PandocLua (PandocLua (..), liftPandocLua)
import Text.Pandoc.Lua.SourcePos (luaSourcePos)
import qualified Data.ByteString.Char8 as Char8
@@ -120,8 +120,8 @@ initLuaState :: PandocLua ()
initLuaState = do
liftPandocLua Lua.openlibs
setWarnFunction
- initJsonMetatable
initPandocModule
+ initJsonMetatable
installLpegSearcher
setGlobalModules
loadInitScript "init.lua"
@@ -210,7 +210,9 @@ initLuaState = do
-- from/via JSON arrays.
initJsonMetatable :: PandocLua ()
initJsonMetatable = liftPandocLua $ do
- newListMetatable HsLua.Aeson.jsonarray (pure ())
+ pushPandocList (const pushnil) []
+ getmetatable top
+ setfield registryindex HsLua.Aeson.jsonarray
Lua.pop 1
-- | Evaluate a @'PandocLua'@ computation, running all contained Lua
diff --git a/pandoc-lua-engine/test/lua/module/pandoc-json.lua b/pandoc-lua-engine/test/lua/module/pandoc-json.lua
index 46400d572..2b0adadca 100644
--- a/pandoc-lua-engine/test/lua/module/pandoc-json.lua
+++ b/pandoc-lua-engine/test/lua/module/pandoc-json.lua
@@ -1,6 +1,7 @@
--
-- Tests for the system module
--
+local pandoc = require 'pandoc'
local json = require 'pandoc.json'
local tasty = require 'tasty'
@@ -33,19 +34,26 @@ return {
local obj = setmetatable(
{title = 23},
{
- __tojson = function (obj)
+ __tojson = function (_)
return '"Nichts ist so wie es scheint"'
end
}
)
assert.are_same(json.encode(obj), [["Nichts ist so wie es scheint"]])
end),
- test('Inline (Space)', function ()
+ test('pandoc.List', function ()
+ local list = pandoc.List {'foo', 'bar', 'baz'}
assert.are_equal(
- json.encode(pandoc.Space()),
- '{"t":"Space"}'
+ json.encode(list),
+ '["foo","bar","baz"]'
)
end),
+ test('Inline (Space)', function ()
+ assert.are_equal(
+ json.encode(pandoc.Space()),
+ '{"t":"Space"}'
+ )
+ end),
test('Block (HorizontalRule)', function ()
assert.are_equal(
json.encode(pandoc.HorizontalRule()),
@@ -85,6 +93,9 @@ return {
test('object', function ()
assert.are_same(json.decode '{"a":5}', {a = 5})
end),
+ test('list of strings', function ()
+ assert.are_equal(json.decode '["foo", "bar"]', pandoc.List{"foo", "bar"})
+ end),
test('Inline (Space)', function ()
assert.are_equal(json.decode '{"t":"Space"}', pandoc.Space())
end),