summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2018-01-07 13:43:03 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2018-01-07 13:43:03 +0100
commit043740d32baa9bf5c58409c2a8ace5a196283fa8 (patch)
treeef5dc4c1adef655e689c8b6bb13c0877068c03b6
parent458e633bc40e1dc1dd2547fe1cc77677599c26d5 (diff)
Lua: make pandoc version available as PANDOC_VERSION
The current pandoc version is made available to Lua programs in the global PANDOC_VERSION. It contains the version as a list of numbers.
-rw-r--r--src/Text/Pandoc/Lua/Init.hs4
-rw-r--r--test/Tests/Lua.hs38
2 files changed, 29 insertions, 13 deletions
diff --git a/src/Text/Pandoc/Lua/Init.hs b/src/Text/Pandoc/Lua/Init.hs
index 25869bf91..5e879114f 100644
--- a/src/Text/Pandoc/Lua/Init.hs
+++ b/src/Text/Pandoc/Lua/Init.hs
@@ -35,8 +35,10 @@ module Text.Pandoc.Lua.Init
import Control.Monad.Trans (MonadIO (..))
import Data.IORef (newIORef, readIORef)
+import Data.Version (Version (versionBranch))
import Foreign.Lua (Lua, LuaException (..))
import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
+import Paths_pandoc (version)
import Text.Pandoc.Class (PandocIO, getCommonState, getUserDataDir, getMediaBag,
setMediaBag)
import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
@@ -75,5 +77,7 @@ initLuaState :: LuaPackageParams -> Lua ()
initLuaState luaPkgParams = do
Lua.openlibs
Lua.preloadTextModule "text"
+ Lua.push (versionBranch version)
+ Lua.setglobal "PANDOC_VERSION"
installPandocPackageSearcher luaPkgParams
loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
diff --git a/test/Tests/Lua.hs b/test/Tests/Lua.hs
index 6f495a3ca..058d5722b 100644
--- a/test/Tests/Lua.hs
+++ b/test/Tests/Lua.hs
@@ -13,6 +13,7 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
+import Text.Pandoc.Shared (pandocVersion)
import qualified Foreign.Lua as Lua
@@ -106,6 +107,14 @@ tests = map (localOption (QuickCheckTests 20))
, plain (str "stringify: OK")
, plain (str "to_roman_numeral: OK")
])
+
+ , testCase "Pandoc version is set" . runPandocLua' $ do
+ Lua.getglobal' "table.concat"
+ Lua.getglobal "PANDOC_VERSION"
+ Lua.push ("." :: String) -- seperator
+ Lua.call 2 1
+ Lua.liftIO . assertEqual "pandoc version is wrong" pandocVersion
+ =<< Lua.peek Lua.stackTop
]
assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
@@ -121,18 +130,21 @@ roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
roundtripEqual x = (x ==) <$> roundtripped
where
roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
- roundtripped = runIOorExplode $ do
- setUserDataDir (Just "../data")
- res <- runPandocLua $ do
- oldSize <- Lua.gettop
- Lua.push x
- size <- Lua.gettop
- when (size - oldSize /= 1) $
- error ("not exactly one additional element on the stack: " ++ show size)
- res <- Lua.peekEither (-1)
- case res of
- Left _ -> error "could not read from stack"
- Right y -> return y
+ roundtripped = runPandocLua' $ do
+ oldSize <- Lua.gettop
+ Lua.push x
+ size <- Lua.gettop
+ when (size - oldSize /= 1) $
+ error ("not exactly one additional element on the stack: " ++ show size)
+ res <- Lua.peekEither (-1)
case res of
- Left e -> error (show e)
+ Left _ -> error "could not read from stack"
Right y -> return y
+
+runPandocLua' :: Lua.Lua a -> IO a
+runPandocLua' op = runIOorExplode $ do
+ setUserDataDir (Just "../data")
+ res <- runPandocLua op
+ case res of
+ Left e -> error (show e)
+ Right x -> return x