diff options
author | Albert Krewinkel <albert@zeitkraut.de> | 2018-12-03 08:24:28 +0100 |
---|---|---|
committer | Albert Krewinkel <albert@zeitkraut.de> | 2020-04-17 23:05:44 +0200 |
commit | 62cf21cbaa9ac3fbc2ba7218a3037208364c80a4 (patch) | |
tree | 09c206a6d7046bb437bf1b999c9ddebbd2d5115e /src/Text/Pandoc/Lua/Packages.hs | |
parent | eceb8eaf47e7dc543dc0e2fac154ba965acf7375 (diff) | |
download | pandoc-62cf21cbaa9ac3fbc2ba7218a3037208364c80a4.tar.gz |
API change: use new type PandocLua for all pandoc Lua operations
The new type `PandocLua` is an instance of the `PandocMonad` typeclass
and can thus be used in a way similar to `PandocIO`.
Diffstat (limited to 'src/Text/Pandoc/Lua/Packages.hs')
-rw-r--r-- | src/Text/Pandoc/Lua/Packages.hs | 64 |
1 files changed, 23 insertions, 41 deletions
diff --git a/src/Text/Pandoc/Lua/Packages.hs b/src/Text/Pandoc/Lua/Packages.hs index ad338f4bd..79d42a6d7 100644 --- a/src/Text/Pandoc/Lua/Packages.hs +++ b/src/Text/Pandoc/Lua/Packages.hs @@ -8,37 +8,32 @@ Maintainer : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de> Stability : alpha -Pandoc module for lua. +Pandoc module for Lua. -} module Text.Pandoc.Lua.Packages - ( LuaPackageParams (..) - , installPandocPackageSearcher + ( installPandocPackageSearcher ) where import Control.Monad (forM_) import Data.ByteString (ByteString) -import Foreign.Lua (Lua, NumResults, liftIO) -import Text.Pandoc.Class.PandocIO (runIO) -import Text.Pandoc.Class.PandocMonad (readDataFile, setUserDataDir) +import Foreign.Lua (Lua, NumResults) +import Text.Pandoc.Class.PandocMonad (readDataFile) +import Text.Pandoc.Lua.PandocLua (PandocLua, liftPandocLua) import qualified Foreign.Lua as Lua -import Text.Pandoc.Lua.Module.Pandoc as Pandoc -import Text.Pandoc.Lua.Module.MediaBag as MediaBag -import Text.Pandoc.Lua.Module.System as System -import Text.Pandoc.Lua.Module.Types as Types -import Text.Pandoc.Lua.Module.Utils as Utils - --- | Parameters used to create lua packages/modules. -data LuaPackageParams = LuaPackageParams - { luaPkgDataDir :: Maybe FilePath - } +import qualified Foreign.Lua.Module.Text as Text +import qualified Text.Pandoc.Lua.Module.Pandoc as Pandoc +import qualified Text.Pandoc.Lua.Module.MediaBag as MediaBag +import qualified Text.Pandoc.Lua.Module.System as System +import qualified Text.Pandoc.Lua.Module.Types as Types +import qualified Text.Pandoc.Lua.Module.Utils as Utils -- | Insert pandoc's package loader as the first loader, making it the default. -installPandocPackageSearcher :: LuaPackageParams -> Lua () -installPandocPackageSearcher luaPkgParams = do +installPandocPackageSearcher :: PandocLua () +installPandocPackageSearcher = liftPandocLua $ do Lua.getglobal' "package.searchers" shiftArray - Lua.pushHaskellFunction (pandocPackageSearcher luaPkgParams) + Lua.pushHaskellFunction pandocPackageSearcher Lua.rawseti (Lua.nthFromTop 2) 1 Lua.pop 1 -- remove 'package.searchers' from stack where @@ -47,29 +42,24 @@ installPandocPackageSearcher luaPkgParams = do Lua.rawseti (-2) (i + 1) -- | Load a pandoc module. -pandocPackageSearcher :: LuaPackageParams -> String -> Lua NumResults -pandocPackageSearcher pkgParams pkgName = +pandocPackageSearcher :: String -> PandocLua NumResults +pandocPackageSearcher pkgName = case pkgName of - "pandoc" -> let datadir = luaPkgDataDir pkgParams - in pushWrappedHsFun (Pandoc.pushModule datadir) + "pandoc" -> pushWrappedHsFun Pandoc.pushModule "pandoc.mediabag" -> pushWrappedHsFun MediaBag.pushModule "pandoc.system" -> pushWrappedHsFun System.pushModule "pandoc.types" -> pushWrappedHsFun Types.pushModule - "pandoc.utils" -> let datadir = luaPkgDataDir pkgParams - in pushWrappedHsFun (Utils.pushModule datadir) - _ -> searchPureLuaLoader + "pandoc.utils" -> pushWrappedHsFun Utils.pushModule + "text" -> pushWrappedHsFun Text.pushModule + _ -> searchPureLuaLoader where - pushWrappedHsFun f = do + pushWrappedHsFun f = liftPandocLua $ do Lua.pushHaskellFunction f return 1 searchPureLuaLoader = do let filename = pkgName ++ ".lua" - modScript <- liftIO (dataDirScript (luaPkgDataDir pkgParams) filename) - case modScript of - Just script -> pushWrappedHsFun (loadStringAsPackage pkgName script) - Nothing -> do - Lua.push ("\n\tno file '" ++ filename ++ "' in pandoc's datadir") - return 1 + script <- readDataFile filename + pushWrappedHsFun (loadStringAsPackage pkgName script) loadStringAsPackage :: String -> ByteString -> Lua NumResults loadStringAsPackage pkgName script = do @@ -79,11 +69,3 @@ loadStringAsPackage pkgName script = do else do msg <- Lua.popValue Lua.raiseError ("Error while loading `" <> pkgName <> "`.\n" <> msg) - --- | Get the ByteString representation of the pandoc module. -dataDirScript :: Maybe FilePath -> FilePath -> IO (Maybe ByteString) -dataDirScript datadir moduleFile = do - res <- runIO $ setUserDataDir datadir >> readDataFile moduleFile - return $ case res of - Left _ -> Nothing - Right s -> Just s |