|
Packit Service |
d2f85f |
{-
|
|
Packit Service |
d2f85f |
Copyright © 2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
d2f85f |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
d2f85f |
the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
d2f85f |
(at your option) any later version.
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
d2f85f |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
d2f85f |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
d2f85f |
GNU General Public License for more details.
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
d2f85f |
along with this program; if not, write to the Free Software
|
|
Packit Service |
d2f85f |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
d2f85f |
-}
|
|
Packit Service |
d2f85f |
{- |
|
|
Packit Service |
d2f85f |
Module : Text.Pandoc.Lua
|
|
Packit Service |
d2f85f |
Copyright : Copyright © 2017 Albert Krewinkel
|
|
Packit Service |
d2f85f |
License : GNU GPL, version 2 or above
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
|
|
Packit Service |
d2f85f |
Stability : alpha
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
Functions to initialize the Lua interpreter.
|
|
Packit Service |
d2f85f |
-}
|
|
Packit Service |
d2f85f |
module Text.Pandoc.Lua.Init
|
|
Packit Service |
d2f85f |
( LuaException (..)
|
|
Packit Service |
d2f85f |
, LuaPackageParams (..)
|
|
Packit Service |
d2f85f |
, runPandocLua
|
|
Packit Service |
d2f85f |
, initLuaState
|
|
Packit Service |
d2f85f |
, luaPackageParams
|
|
Packit Service |
d2f85f |
) where
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
import Control.Monad.Trans (MonadIO (..))
|
|
Packit Service |
d2f85f |
import Data.IORef (newIORef, readIORef)
|
|
Packit Service |
d2f85f |
import Foreign.Lua (Lua, LuaException (..))
|
|
Packit Service |
d2f85f |
import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
|
|
Packit Service |
d2f85f |
import Text.Pandoc.Class (PandocIO, getCommonState, getUserDataDir, getMediaBag,
|
|
Packit Service |
d2f85f |
setMediaBag)
|
|
Packit Service |
d2f85f |
import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
|
|
Packit Service |
d2f85f |
installPandocPackageSearcher)
|
|
Packit Service |
d2f85f |
import Text.Pandoc.Lua.Util (loadScriptFromDataDir)
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
import qualified Foreign.Lua as Lua
|
|
Packit Service |
d2f85f |
import qualified Foreign.Lua.Module.Text as Lua
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
-- | Run the lua interpreter, using pandoc's default way of environment
|
|
Packit Service |
d2f85f |
-- initalization.
|
|
Packit Service |
d2f85f |
runPandocLua :: Lua a -> PandocIO (Either LuaException a)
|
|
Packit Service |
d2f85f |
runPandocLua luaOp = do
|
|
Packit Service |
d2f85f |
luaPkgParams <- luaPackageParams
|
|
Packit Service |
d2f85f |
enc <- liftIO $ getForeignEncoding <* setForeignEncoding utf8
|
|
Packit Service |
d2f85f |
res <- liftIO $ Lua.runLuaEither (initLuaState luaPkgParams *> luaOp)
|
|
Packit Service |
d2f85f |
liftIO $ setForeignEncoding enc
|
|
Packit Service |
d2f85f |
newMediaBag <- liftIO (readIORef (luaPkgMediaBag luaPkgParams))
|
|
Packit Service |
d2f85f |
setMediaBag newMediaBag
|
|
Packit Service |
d2f85f |
return res
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
-- | Generate parameters required to setup pandoc's lua environment.
|
|
Packit Service |
d2f85f |
luaPackageParams :: PandocIO LuaPackageParams
|
|
Packit Service |
d2f85f |
luaPackageParams = do
|
|
Packit Service |
d2f85f |
commonState <- getCommonState
|
|
Packit Service |
d2f85f |
datadir <- getUserDataDir
|
|
Packit Service |
d2f85f |
mbRef <- liftIO . newIORef =<< getMediaBag
|
|
Packit Service |
d2f85f |
return LuaPackageParams
|
|
Packit Service |
d2f85f |
{ luaPkgCommonState = commonState
|
|
Packit Service |
d2f85f |
, luaPkgDataDir = datadir
|
|
Packit Service |
d2f85f |
, luaPkgMediaBag = mbRef
|
|
Packit Service |
d2f85f |
}
|
|
Packit Service |
d2f85f |
|
|
Packit Service |
d2f85f |
-- Initialize the lua state with all required values
|
|
Packit Service |
d2f85f |
initLuaState :: LuaPackageParams -> Lua ()
|
|
Packit Service |
d2f85f |
initLuaState luaPkgParams = do
|
|
Packit Service |
d2f85f |
Lua.openlibs
|
|
Packit Service |
d2f85f |
Lua.preloadTextModule "text"
|
|
Packit Service |
d2f85f |
installPandocPackageSearcher luaPkgParams
|
|
Packit Service |
d2f85f |
loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
|