git.iko.soy/typeable/pandoc
on master help sign in
Custom writer: Raise `PandocLuaException` instead of using 'error'.

Eventually we'll change the return type so that no exception is involved, but at least this can be trapped.

commit030d3b597de696a074ff143958f359de268d2078 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 18 Jan 2015, 22:04 (12 years ago) parentab8b00e
1 file changed+9−2
src/Text/Pandoc/Writers/Custom.hs+9−2
src/Text/Pandoc/Writers/Custom.hs+9−2
@@ -1,5 +1,5 @@
11{-# LANGUAGE OverlappingInstances, FlexibleInstances, OverloadedStrings,
2− ScopedTypeVariables #-}
2+ ScopedTypeVariables, DeriveDataTypeable #-}
33{-# OPTIONS_GHC -fno-warn-orphans #-}
44{- Copyright (C) 2012-2014 John MacFarlane <jgm@berkeley.edu>
55
@@ -35,6 +35,7 @@ import Text.Pandoc.Definition
3535import Text.Pandoc.Options
3636import Data.List ( intersperse )
3737import Data.Char ( toLower )
38+import Data.Typeable
3839import Scripting.Lua (LuaState, StackValue, callfunc)
3940import Text.Pandoc.Writers.Shared
4041import qualified Scripting.Lua as Lua
@@ -43,6 +44,7 @@ import Data.ByteString (ByteString)
4344import qualified Data.ByteString.Char8 as C8
4445import Data.Monoid
4546import Control.Monad (when)
47+import Control.Exception
4648import qualified Data.Map as M
4749import Text.Pandoc.Templates
4850
@@ -146,6 +148,11 @@ instance StackValue Citation where
146148 peek = undefined
147149 valuetype _ = Lua.TTABLE
148150
151+data PandocLuaException = PandocLuaException String
152+ deriving (Show, Typeable)
153+
154+instance Exception PandocLuaException
155+
149156-- | Convert Pandoc to custom markup.
150157writeCustom :: FilePath -> WriterOptions -> Pandoc -> IO String
151158writeCustom luaFile opts doc@(Pandoc meta _) = do
@@ -156,7 +163,7 @@ writeCustom luaFile opts doc@(Pandoc meta _) = do
156163 -- check for error in lua script (later we'll change the return type
157164 -- to handle this more gracefully):
158165 when (status /= 0) $
159− Lua.tostring lua 1 >>= error
166+ Lua.tostring lua 1 >>= throw . PandocLuaException
160167 Lua.call lua 0 0
161168 -- TODO - call hierarchicalize, so we have that info
162169 rendered <- docToCustom lua opts doc