git.iko.soy/typeable/pandoc
on master help sign in
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.

commit043740d32baa9bf5c58409c2a8ace5a196283fa8 authorAlbert Krewinkel <albert@zeitkraut.de> dateSun 7 Jan 2018, 13:43 (9 years ago) parent458e633
2 files changed+29−13
src/Text/Pandoc/Lua/Init.hs+4 test/Tests/Lua.hs+25−13
src/Text/Pandoc/Lua/Init.hs+4
@@ -35,8 +35,10 @@ module Text.Pandoc.Lua.Init
3535
3636import Control.Monad.Trans (MonadIO (..))
3737import Data.IORef (newIORef, readIORef)
38+import Data.Version (Version (versionBranch))
3839import Foreign.Lua (Lua, LuaException (..))
3940import GHC.IO.Encoding (getForeignEncoding, setForeignEncoding, utf8)
41+import Paths_pandoc (version)
4042import Text.Pandoc.Class (PandocIO, getCommonState, getUserDataDir, getMediaBag,
4143 setMediaBag)
4244import Text.Pandoc.Lua.Packages (LuaPackageParams (..),
@@ -75,5 +77,7 @@ initLuaState :: LuaPackageParams -> Lua ()
7577initLuaState luaPkgParams = do
7678 Lua.openlibs
7779 Lua.preloadTextModule "text"
80+ Lua.push (versionBranch version)
81+ Lua.setglobal "PANDOC_VERSION"
7882 installPandocPackageSearcher luaPkgParams
7983 loadScriptFromDataDir (luaPkgDataDir luaPkgParams) "init.lua"
test/Tests/Lua.hs+25−13
@@ -13,6 +13,7 @@ import Text.Pandoc.Builder (bulletList, divWith, doc, doubleQuoted, emph,
1313import Text.Pandoc.Class (runIOorExplode, setUserDataDir)
1414import Text.Pandoc.Definition (Block, Inline, Meta, Pandoc)
1515import Text.Pandoc.Lua (runLuaFilter, runPandocLua)
16+import Text.Pandoc.Shared (pandocVersion)
1617
1718import qualified Foreign.Lua as Lua
1819
@@ -106,6 +107,14 @@ tests = map (localOption (QuickCheckTests 20))
106107 , plain (str "stringify: OK")
107108 , plain (str "to_roman_numeral: OK")
108109 ])
110+
111+ , testCase "Pandoc version is set" . runPandocLua' $ do
112+ Lua.getglobal' "table.concat"
113+ Lua.getglobal "PANDOC_VERSION"
114+ Lua.push ("." :: String) -- seperator
115+ Lua.call 2 1
116+ Lua.liftIO . assertEqual "pandoc version is wrong" pandocVersion
117+ =<< Lua.peek Lua.stackTop
109118 ]
110119
111120assertFilterConversion :: String -> FilePath -> Pandoc -> Pandoc -> Assertion
@@ -121,18 +130,21 @@ roundtripEqual :: (Eq a, Lua.FromLuaStack a, Lua.ToLuaStack a) => a -> IO Bool
121130roundtripEqual x = (x ==) <$> roundtripped
122131 where
123132 roundtripped :: (Lua.FromLuaStack a, Lua.ToLuaStack a) => IO a
124− roundtripped = runIOorExplode $ do
125− setUserDataDir (Just "../data")
126− res <- runPandocLua $ do
127− oldSize <- Lua.gettop
128− Lua.push x
129− size <- Lua.gettop
130− when (size - oldSize /= 1) $
131− error ("not exactly one additional element on the stack: " ++ show size)
132− res <- Lua.peekEither (-1)
133− case res of
134− Left _ -> error "could not read from stack"
135− Right y -> return y
133+ roundtripped = runPandocLua' $ do
134+ oldSize <- Lua.gettop
135+ Lua.push x
136+ size <- Lua.gettop
137+ when (size - oldSize /= 1) $
138+ error ("not exactly one additional element on the stack: " ++ show size)
139+ res <- Lua.peekEither (-1)
136140 case res of
137− Left e -> error (show e)
141+ Left _ -> error "could not read from stack"
138142 Right y -> return y
143+
144+runPandocLua' :: Lua.Lua a -> IO a
145+runPandocLua' op = runIOorExplode $ do
146+ setUserDataDir (Just "../data")
147+ res <- runPandocLua op
148+ case res of
149+ Left e -> error (show e)
150+ Right x -> return x