Lua.PandocModule: promote addFunction to top level
This reduces some boilerplate.
This commit is contained in:
parent
5307868de5
commit
9b750f7d87
2 changed files with 11 additions and 15 deletions
|
@ -782,7 +782,7 @@ M.UpperAlpha = "UpperAlpha"
|
||||||
-- assert(block.content[1].t == "Emph")
|
-- assert(block.content[1].t == "Emph")
|
||||||
function M.read(markup, format)
|
function M.read(markup, format)
|
||||||
format = format or "markdown"
|
format = format or "markdown"
|
||||||
local pd = pandoc.__read(format, markup)
|
local pd = pandoc._read(format, markup)
|
||||||
if type(pd) == "string" then
|
if type(pd) == "string" then
|
||||||
error(pd)
|
error(pd)
|
||||||
else
|
else
|
||||||
|
|
|
@ -41,6 +41,7 @@ import Data.IORef
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
import Data.Text (pack)
|
import Data.Text (pack)
|
||||||
import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
|
import Foreign.Lua (Lua, FromLuaStack, ToLuaStack, NumResults, liftIO)
|
||||||
|
import Foreign.Lua.FunctionCalling (ToHaskellFunction)
|
||||||
import Text.Pandoc.Class (readDataFile, runIO,
|
import Text.Pandoc.Class (readDataFile, runIO,
|
||||||
runIOorExplode, setUserDataDir, CommonState(..),
|
runIOorExplode, setUserDataDir, CommonState(..),
|
||||||
putCommonState, fetchItem, setMediaBag)
|
putCommonState, fetchItem, setMediaBag)
|
||||||
|
@ -62,15 +63,9 @@ pushPandocModule datadir = do
|
||||||
script <- liftIO (pandocModuleScript datadir)
|
script <- liftIO (pandocModuleScript datadir)
|
||||||
status <- Lua.loadstring script
|
status <- Lua.loadstring script
|
||||||
unless (status /= Lua.OK) $ Lua.call 0 1
|
unless (status /= Lua.OK) $ Lua.call 0 1
|
||||||
Lua.push "__read"
|
addFunction "pipe" pipeFn
|
||||||
Lua.pushHaskellFunction readDoc
|
addFunction "_read" readDoc
|
||||||
Lua.rawset (-3)
|
addFunction "sha1" sha1HashFn
|
||||||
Lua.push "sha1"
|
|
||||||
Lua.pushHaskellFunction sha1HashFn
|
|
||||||
Lua.rawset (-3)
|
|
||||||
Lua.push "pipe"
|
|
||||||
Lua.pushHaskellFunction pipeFn
|
|
||||||
Lua.rawset (-3)
|
|
||||||
|
|
||||||
-- | Get the string representation of the pandoc module
|
-- | Get the string representation of the pandoc module
|
||||||
pandocModuleScript :: Maybe FilePath -> IO String
|
pandocModuleScript :: Maybe FilePath -> IO String
|
||||||
|
@ -102,11 +97,12 @@ pushMediaBagModule commonState mediaBagRef = do
|
||||||
addFunction "list" (mediaDirectoryFn mediaBagRef)
|
addFunction "list" (mediaDirectoryFn mediaBagRef)
|
||||||
addFunction "fetch" (fetch commonState mediaBagRef)
|
addFunction "fetch" (fetch commonState mediaBagRef)
|
||||||
return ()
|
return ()
|
||||||
where
|
|
||||||
addFunction name fn = do
|
addFunction :: ToHaskellFunction a => String -> a -> Lua ()
|
||||||
Lua.push name
|
addFunction name fn = do
|
||||||
Lua.pushHaskellFunction fn
|
Lua.push name
|
||||||
Lua.rawset (-3)
|
Lua.pushHaskellFunction fn
|
||||||
|
Lua.rawset (-3)
|
||||||
|
|
||||||
sha1HashFn :: BL.ByteString
|
sha1HashFn :: BL.ByteString
|
||||||
-> Lua NumResults
|
-> Lua NumResults
|
||||||
|
|
Loading…
Add table
Reference in a new issue