aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Lua
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2017-10-03 13:13:45 +0200
committerAlbert Krewinkel <albert@zeitkraut.de>2017-10-03 13:13:45 +0200
commit9b750f7d879b1da386e49ecfd51ef9d023dc5d66 (patch)
tree32744ca5b095299b5775cec02a3f4b79c4f085b5 /src/Text/Pandoc/Lua
parent5307868de52e76009b5d705b43fa75a7eaebf9d3 (diff)
downloadpandoc-9b750f7d879b1da386e49ecfd51ef9d023dc5d66.tar.gz
Lua.PandocModule: promote addFunction to top level
This reduces some boilerplate.
Diffstat (limited to 'src/Text/Pandoc/Lua')
-rw-r--r--src/Text/Pandoc/Lua/PandocModule.hs24
1 files changed, 10 insertions, 14 deletions
diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs
index 6a84a4350..c689edc4e 100644
--- a/src/Text/Pandoc/Lua/PandocModule.hs
+++ b/src/Text/Pandoc/Lua/PandocModule.hs
@@ -41,6 +41,7 @@ import Data.IORef
import Data.Maybe (fromMaybe)
import Data.Text (pack)
import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
+import Foreign.Lua.FunctionCalling (ToHaskellFunction)
import Text.Pandoc.Class (readDataFile, runIO,
runIOorExplode, setUserDataDir, CommonState(..),
putCommonState, fetchItem, setMediaBag)
@@ -62,15 +63,9 @@ pushPandocModule datadir = do
script <- liftIO (pandocModuleScript datadir)
status <- Lua.loadstring script
unless (status /= Lua.OK) $ Lua.call 0 1
- Lua.push "__read"
- Lua.pushHaskellFunction readDoc
- Lua.rawset (-3)
- Lua.push "sha1"
- Lua.pushHaskellFunction sha1HashFn
- Lua.rawset (-3)
- Lua.push "pipe"
- Lua.pushHaskellFunction pipeFn
- Lua.rawset (-3)
+ addFunction "pipe" pipeFn
+ addFunction "_read" readDoc
+ addFunction "sha1" sha1HashFn
-- | Get the string representation of the pandoc module
pandocModuleScript :: Maybe FilePath -> IO String
@@ -102,11 +97,12 @@ pushMediaBagModule commonState mediaBagRef = do
addFunction "list" (mediaDirectoryFn mediaBagRef)
addFunction "fetch" (fetch commonState mediaBagRef)
return ()
- where
- addFunction name fn = do
- Lua.push name
- Lua.pushHaskellFunction fn
- Lua.rawset (-3)
+
+addFunction :: ToHaskellFunction a => String -> a -> Lua ()
+addFunction name fn = do
+ Lua.push name
+ Lua.pushHaskellFunction fn
+ Lua.rawset (-3)
sha1HashFn :: BL.ByteString
-> Lua NumResults