@@ -34,11 +34,11 @@ module Text.Pandoc.Lua.StackInstances () where
35import Control.Applicative ((<|>))
35import Control.Applicative ((<|>))
36import Foreign.Lua (Lua, Type (..), FromLuaStack (peek), ToLuaStack (push),
36import Foreign.Lua (Lua, Type (..), FromLuaStack (peek), ToLuaStack (push),
37− StackIndex, peekEither, throwLuaError)
37+ StackIndex, throwLuaError, tryLua)
38−import Foreign.Lua.Api (getfield, ltype, newtable, pop, rawlen)
38+import Foreign.Lua.Api (getmetatable, ltype, newtable, pop, rawget, rawlen)
39import Text.Pandoc.Definition
39import Text.Pandoc.Definition
40import Text.Pandoc.Lua.SharedInstances ()
40import Text.Pandoc.Lua.SharedInstances ()
41−import Text.Pandoc.Lua.Util (addValue, getTable, pushViaConstructor)
41+import Text.Pandoc.Lua.Util (addValue, adjustIndexBy, getTable, pushViaConstructor)
42import Text.Pandoc.Shared (safeRead)
42import Text.Pandoc.Shared (safeRead)
44instance ToLuaStack Pandoc where
44instance ToLuaStack Pandoc where
@@ -46,6 +46,7 @@ instance ToLuaStack Pandoc where
47 addValue "blocks" blocks
47 addValue "blocks" blocks
48 addValue "meta" meta
48 addValue "meta" meta
49instance FromLuaStack Pandoc where
50instance FromLuaStack Pandoc where
50 peek idx = do
51 peek idx = do
51 blocks <- getTable idx "blocks"
52 blocks <- getTable idx "blocks"
@@ -151,7 +152,7 @@ peekMetaValue idx = do
151 TypeBoolean -> MetaBool <$> peek idx
152 TypeBoolean -> MetaBool <$> peek idx
152 TypeString -> MetaString <$> peek idx
153 TypeString -> MetaString <$> peek idx
153 TypeTable -> do
154 TypeTable -> do
154− tag <- getfield idx "t" *> peekEither (-1) <* pop 1
155+ tag <- tryLua $ getTag idx
155 case tag of
156 case tag of
156 Right "MetaBlocks" -> MetaBlocks <$> elementContent
157 Right "MetaBlocks" -> MetaBlocks <$> elementContent
157 Right "MetaBool" -> MetaBool <$> elementContent
158 Right "MetaBool" -> MetaBool <$> elementContent
@@ -192,7 +193,7 @@ pushBlock = \case
192-- | Return the value at the given index as block if possible.
193-- | Return the value at the given index as block if possible.
193peekBlock :: StackIndex -> Lua Block
194peekBlock :: StackIndex -> Lua Block
194peekBlock idx = do
195peekBlock idx = do
195− tag <- getTable idx "t"
196+ tag <- getTag idx
196 case tag of
197 case tag of
197 "BlockQuote" -> BlockQuote <$> elementContent
198 "BlockQuote" -> BlockQuote <$> elementContent
198 "BulletList" -> BulletList <$> elementContent
199 "BulletList" -> BulletList <$> elementContent
@@ -243,7 +244,7 @@ pushInline = \case
243-- | Return the value at the given index as inline if possible.
244-- | Return the value at the given index as inline if possible.
244peekInline :: StackIndex -> Lua Inline
245peekInline :: StackIndex -> Lua Inline
245peekInline idx = do
246peekInline idx = do
246− tag <- getTable idx "t"
247+ tag <- getTag idx
247 case tag of
248 case tag of
248 "Cite" -> (uncurry Cite) <$> elementContent
249 "Cite" -> (uncurry Cite) <$> elementContent
249 "Code" -> (withAttr Code) <$> elementContent
250 "Code" -> (withAttr Code) <$> elementContent
@@ -272,6 +273,19 @@ peekInline idx = do
272 elementContent :: FromLuaStack a => Lua a
273 elementContent :: FromLuaStack a => Lua a
273 elementContent = getTable idx "c"
274 elementContent = getTable idx "c"
276+getTag :: StackIndex -> Lua String
278+ hasMT <- getmetatable idx
286+ rawget (idx `adjustIndexBy` 1)
275withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
289withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
276withAttr f (attributes, x) = f (fromLuaAttr attributes) x
290withAttr f (attributes, x) = f (fromLuaAttr attributes) x
@@ -281,5 +295,6 @@ newtype LuaAttr = LuaAttr { fromLuaAttr :: Attr }
281instance ToLuaStack LuaAttr where
295instance ToLuaStack LuaAttr where
282 push (LuaAttr (id', classes, kv)) =
296 push (LuaAttr (id', classes, kv)) =
283 pushViaConstructor "Attr" id' classes kv
297 pushViaConstructor "Attr" id' classes kv
284instance FromLuaStack LuaAttr where
299instance FromLuaStack LuaAttr where
285 peek idx = LuaAttr <$> peek idx
300 peek idx = LuaAttr <$> peek idx