{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Text.Pandoc.Lua.Init
( initLua
, userInit
) where
import Control.Monad (when)
import Control.Monad.Catch (throwM)
import HsLua as Lua hiding (status)
import Text.Pandoc.Class (report)
import Text.Pandoc.Data (readDataFile)
import Text.Pandoc.Error (PandocError (PandocLuaError))
import Text.Pandoc.Logging (LogMessage (ScriptingWarning))
import Text.Pandoc.Lua.Module (initModules)
import Text.Pandoc.Lua.PandocLua (PandocLua (..), liftPandocLua)
import Text.Pandoc.Lua.SourcePos (luaSourcePos)
import qualified Data.Text as T
import qualified Text.Pandoc.UTF8 as UTF8
initLua :: PandocLua ()
initLua :: PandocLua ()
initLua = do
LuaE PandocError () -> PandocLua ()
forall a. LuaE PandocError a -> PandocLua a
liftPandocLua LuaE PandocError ()
forall e. LuaE e ()
Lua.openlibs
PandocLua ()
setWarnFunction
PandocLua ()
initModules
LuaE PandocError () -> PandocLua ()
forall a. LuaE PandocError a -> PandocLua a
liftPandocLua LuaE PandocError ()
userInit
userInit :: LuaE PandocError ()
userInit :: LuaE PandocError ()
userInit = LuaE PandocError ()
runInitScript
runInitScript :: LuaE PandocError ()
runInitScript :: LuaE PandocError ()
runInitScript = FilePath -> LuaE PandocError ()
runDataFileScript FilePath
"init.lua"
runDataFileScript :: FilePath -> LuaE PandocError ()
runDataFileScript :: FilePath -> LuaE PandocError ()
runDataFileScript FilePath
scriptFile = do
script <- PandocLua ByteString -> LuaE PandocError ByteString
forall a. PandocLua a -> LuaE PandocError a
unPandocLua (PandocLua ByteString -> LuaE PandocError ByteString)
-> PandocLua ByteString -> LuaE PandocError ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> PandocLua ByteString
forall (m :: * -> *). PandocMonad m => FilePath -> m ByteString
readDataFile FilePath
scriptFile
status <- Lua.dostring script
when (status /= Lua.OK) $ do
err <- popException
let prefix = Text
"Couldn't load '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> FilePath -> Text
T.pack FilePath
scriptFile Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"':\n"
throwM . PandocLuaError . (prefix <>) $ case err of
PandocLuaError Text
msg -> Text
msg
PandocError
_ -> FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ PandocError -> FilePath
forall a. Show a => a -> FilePath
show PandocError
err
setWarnFunction :: PandocLua ()
setWarnFunction :: PandocLua ()
setWarnFunction = LuaE PandocError () -> PandocLua ()
forall a. LuaE PandocError a -> PandocLua a
liftPandocLua (LuaE PandocError () -> PandocLua ())
-> ((ByteString -> LuaE PandocError ()) -> LuaE PandocError ())
-> (ByteString -> LuaE PandocError ())
-> PandocLua ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> LuaE PandocError ()) -> LuaE PandocError ()
forall e. LuaError e => (ByteString -> LuaE e ()) -> LuaE e ()
setwarnf' ((ByteString -> LuaE PandocError ()) -> PandocLua ())
-> (ByteString -> LuaE PandocError ()) -> PandocLua ()
forall a b. (a -> b) -> a -> b
$ \ByteString
msg -> do
pos <- Int -> LuaE PandocError (Maybe SourcePos)
forall e. LuaError e => Int -> LuaE e (Maybe SourcePos)
luaSourcePos Int
3
unPandocLua . report $ ScriptingWarning (UTF8.toText msg) pos