aboutsummaryrefslogtreecommitdiff
path: root/pandoc-cli/lua
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2023-03-15 18:25:17 +0100
committerJohn MacFarlane <[email protected]>2023-03-17 09:18:41 -0700
commita23d14079c056af3c63d7903dfd17b30c1e1b8d6 (patch)
treefacba68e31ef530b3b1bd521ea7e783b9cfa279c /pandoc-cli/lua
parent74328eba7e8a3d6ce921fbfa17e2153193460dc2 (diff)
pandoc-cli: add a Lua REPL
Diffstat (limited to 'pandoc-cli/lua')
-rw-r--r--pandoc-cli/lua/PandocCLI/Lua.hs29
1 files changed, 24 insertions, 5 deletions
diff --git a/pandoc-cli/lua/PandocCLI/Lua.hs b/pandoc-cli/lua/PandocCLI/Lua.hs
index 368f9cf49..07259af04 100644
--- a/pandoc-cli/lua/PandocCLI/Lua.hs
+++ b/pandoc-cli/lua/PandocCLI/Lua.hs
@@ -11,6 +11,9 @@ module PandocCLI.Lua (runLuaInterpreter, getEngine) where
import Control.Monad ((<=<))
import HsLua.CLI (EnvBehavior (..), Settings (..), runStandalone)
+import System.Environment (lookupEnv)
+import System.IO.Temp (withSystemTempFile)
+import System.IO (hClose)
import Text.Pandoc.Class (runIOorExplode)
import Text.Pandoc.Error (handleError)
import Text.Pandoc.Lua (runLua, runLuaNoEnv, getEngine)
@@ -18,16 +21,32 @@ import Text.Pandoc.Version (pandocVersionText)
-- | Runs pandoc as a Lua interpreter that is (mostly) compatible with
-- the default @lua@ program shipping with Lua.
+--
+-- The filename for the history of the REPL is taken from the
+-- @PANDOC_REPL_HISTORY@ environment variable if possible. Otherwise a
+-- new temporary file is used; it is removed after the REPL finishes.
runLuaInterpreter :: String -- ^ Program name
-> [String] -- ^ Command line arguments
-> IO ()
runLuaInterpreter progName args = do
- let settings = Settings
- { settingsVersionInfo = "\nEmbedded in pandoc " <> pandocVersionText
- , settingsRunner = runner
- }
- runStandalone settings progName args
+ -- We need some kind of temp
+ mbhistfile <- lookupEnv "PANDOC_REPL_HISTORY"
+ case mbhistfile of
+ Just histfile -> runStandaloneWithHistory histfile
+ Nothing -> withSystemTempFile "pandoc-hist" $ \fp handle -> do
+ -- We cannot pass a handle to the repl; the file will be re-opened
+ -- there.
+ hClose handle
+ runStandaloneWithHistory fp
where
+ runStandaloneWithHistory histfile = do
+ let settings = Settings
+ { settingsVersionInfo = "\nEmbedded in pandoc " <>
+ pandocVersionText
+ , settingsRunner = runner
+ , settingsHistory = Just histfile
+ }
+ runStandalone settings progName args
runner envBehavior =
let runLua' = case envBehavior of
IgnoreEnvVars -> runLuaNoEnv