@@ -19,9 +19,11 @@ module Text.Pandoc.Lua.Marshaling.AST
1919import Control.Applicative ((<|>))
2020import Foreign.Lua (Lua, Peekable, Pushable, StackIndex)
2121import Text.Pandoc.Definition
22+import Text.Pandoc.Error (PandocError)
2223import Text.Pandoc.Lua.Util (defineHowTo, pushViaConstructor)
2324import Text.Pandoc.Lua.Marshaling.CommonState ()
2425
26+import qualified Control.Monad.Catch as Catch
2527import qualified Foreign.Lua as Lua
2628import qualified Text.Pandoc.Lua.Util as LuaUtil
2729
@@ -131,7 +133,7 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
131133 Lua.TypeBoolean -> MetaBool <$> Lua.peek idx
132134 Lua.TypeString -> MetaString <$> Lua.peek idx
133135 Lua.TypeTable -> do
134− tag <- Lua.try $ LuaUtil.getTag idx
136+ tag <- try $ LuaUtil.getTag idx
135137 case tag of
136138 Right "MetaBlocks" -> MetaBlocks <$> elementContent
137139 Right "MetaBool" -> MetaBool <$> elementContent
@@ -139,7 +141,7 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
139141 Right "MetaInlines" -> MetaInlines <$> elementContent
140142 Right "MetaList" -> MetaList <$> elementContent
141143 Right "MetaString" -> MetaString <$> elementContent
142− Right t -> Lua.throwException ("Unknown meta tag: " <> t)
144+ Right t -> Lua.throwMessage ("Unknown meta tag: " <> t)
143145 Left _ -> do
144146 -- no meta value tag given, try to guess.
145147 len <- Lua.rawlen idx
@@ -148,7 +150,7 @@ peekMetaValue idx = defineHowTo "get MetaValue" $ do
148150 else (MetaInlines <$> Lua.peek idx)
149151 <|> (MetaBlocks <$> Lua.peek idx)
150152 <|> (MetaList <$> Lua.peek idx)
151− _ -> Lua.throwException "could not get meta value"
153+ _ -> Lua.throwMessage "could not get meta value"
152154
153155-- | Push a block element to the top of the Lua stack.
154156pushBlock :: Block -> Lua ()
@@ -199,7 +201,7 @@ peekBlock idx = defineHowTo "get Block value" $ do
199201 tbodies
200202 tfoot)
201203 <$> elementContent
202− _ -> Lua.throwException ("Unknown block type: " <> tag)
204+ _ -> Lua.throwMessage ("Unknown block type: " <> tag)
203205 where
204206 -- Get the contents of an AST element.
205207 elementContent :: Peekable a => Lua a
@@ -344,12 +346,15 @@ peekInline idx = defineHowTo "get Inline value" $ do
344346 "Strong" -> Strong <$> elementContent
345347 "Subscript" -> Subscript <$> elementContent
346348 "Superscript"-> Superscript <$> elementContent
347− _ -> Lua.throwException ("Unknown inline type: " <> tag)
349+ _ -> Lua.throwMessage ("Unknown inline type: " <> tag)
348350 where
349351 -- Get the contents of an AST element.
350352 elementContent :: Peekable a => Lua a
351353 elementContent = LuaUtil.rawField idx "c"
352354
355+try :: Lua a -> Lua (Either PandocError a)
356+try = Catch.try
357+
353358withAttr :: (Attr -> a -> b) -> (LuaAttr, a) -> b
354359withAttr f (attributes, x) = f (fromLuaAttr attributes) x
355360