@@ -33,13 +33,15 @@ StackValue instances for pandoc types.
3333module Text.Pandoc.Lua.StackInstances () where
3434
3535import Control.Applicative ((<|>))
36+import Control.Monad (when)
3637import Foreign.Lua (FromLuaStack (peek), Lua, LuaInteger, LuaNumber, StackIndex,
3738 ToLuaStack (push), Type (..), throwLuaError, tryLua)
3839import Text.Pandoc.Definition
3940import Text.Pandoc.Lua.Util (adjustIndexBy, getTable, pushViaConstructor)
40−import Text.Pandoc.Shared (safeRead)
41+import Text.Pandoc.Shared (Element (Blk, Sec), safeRead)
4142
4243import qualified Foreign.Lua as Lua
44+import qualified Text.Pandoc.Lua.Util as LuaUtil
4345
4446instance ToLuaStack Pandoc where
4547 push (Pandoc meta blocks) =
@@ -306,3 +308,27 @@ instance ToLuaStack LuaAttr where
306308
307309instance FromLuaStack LuaAttr where
308310 peek idx = LuaAttr <$> peek idx
311+
312+--
313+-- Hierarchical elements
314+--
315+instance ToLuaStack Element where
316+ push (Blk blk) = push blk
317+ push (Sec lvl num attr label contents) = do
318+ Lua.newtable
319+ LuaUtil.addValue "level" lvl
320+ LuaUtil.addValue "numbering" num
321+ LuaUtil.addValue "attr" (LuaAttr attr)
322+ LuaUtil.addValue "label" label
323+ LuaUtil.addValue "contents" contents
324+ pushSecMetaTable
325+ Lua.setmetatable (-2)
326+ where
327+ pushSecMetaTable :: Lua ()
328+ pushSecMetaTable = do
329+ inexistant <- Lua.newmetatable "PandocElementSec"
330+ when inexistant $ do
331+ LuaUtil.addValue "t" "Sec"
332+ Lua.push "__index"
333+ Lua.pushvalue (-2)
334+ Lua.rawset (-3)