diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-04-17 18:02:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-17 18:02:25 -0700 |
commit | 0d2b8e3fe1d6a27aac082be7711b7156783b3051 (patch) | |
tree | 459122371d6b88a7756eee954b81f2bba4bdfdca /src/Text/Pandoc/Writers/Custom.hs | |
parent | 8f40b4ba14fce10199a059a281c9bd10c884241d (diff) | |
parent | 62cf21cbaa9ac3fbc2ba7218a3037208364c80a4 (diff) | |
download | pandoc-0d2b8e3fe1d6a27aac082be7711b7156783b3051.tar.gz |
Merge pull request #6211 from tarleb/lua-pandocerror
API change: create PandocLua type, use PandocError for exceptions
Diffstat (limited to 'src/Text/Pandoc/Writers/Custom.hs')
-rw-r--r-- | src/Text/Pandoc/Writers/Custom.hs | 27 |
1 files changed, 9 insertions, 18 deletions
diff --git a/src/Text/Pandoc/Writers/Custom.hs b/src/Text/Pandoc/Writers/Custom.hs index 2be64d56f..50a013dfd 100644 --- a/src/Text/Pandoc/Writers/Custom.hs +++ b/src/Text/Pandoc/Writers/Custom.hs @@ -20,17 +20,14 @@ import Data.List (intersperse) import qualified Data.Map as M import qualified Data.Text as T import Data.Text (Text, pack) -import Data.Typeable import Foreign.Lua (Lua, Pushable) import Text.DocLayout (render, literal) import Text.Pandoc.Class.PandocIO (PandocIO) import Text.Pandoc.Definition -import Text.Pandoc.Lua (Global (..), LuaException (LuaException), - runLua, setGlobals) +import Text.Pandoc.Lua (Global (..), runLua, setGlobals) import Text.Pandoc.Lua.Util (addField, dofileWithTraceback) import Text.Pandoc.Options import Text.Pandoc.Templates (renderTemplate) -import qualified Text.Pandoc.UTF8 as UTF8 import Text.Pandoc.Writers.Shared import qualified Foreign.Lua as Lua @@ -81,11 +78,6 @@ instance (Pushable a, Pushable b) => Pushable (KeyValue a b) where Lua.push v Lua.rawset (Lua.nthFromTop 3) -data PandocLuaException = PandocLuaException Text - deriving (Show, Typeable) - -instance Exception PandocLuaException - -- | Convert Pandoc to custom markup. writeCustom :: FilePath -> WriterOptions -> Pandoc -> PandocIO Text writeCustom luaFile opts doc@(Pandoc meta _) = do @@ -97,21 +89,20 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do stat <- dofileWithTraceback luaFile -- check for error in lua script (later we'll change the return type -- to handle this more gracefully): - when (stat /= Lua.OK) $ - Lua.tostring' (-1) >>= throw . PandocLuaException . UTF8.toText + when (stat /= Lua.OK) + Lua.throwTopMessage rendered <- docToCustom opts doc context <- metaToContext opts (fmap (literal . pack) . blockListToCustom) (fmap (literal . pack) . inlineListToCustom) meta return (pack rendered, context) - let (body, context) = case res of - Left (LuaException msg) -> throw (PandocLuaException msg) - Right x -> x - return $ - case writerTemplate opts of - Nothing -> body - Just tpl -> render Nothing $ + case res of + Left msg -> throw msg + Right (body, context) -> return $ + case writerTemplate opts of + Nothing -> body + Just tpl -> render Nothing $ renderTemplate tpl $ setField "body" body context docToCustom :: WriterOptions -> Pandoc -> Lua String |