mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-25 09:47:06 +00:00
'make pandoc.wasm' will compile a wasm version of pandoc. This also adds a 'repl' flag to pandoc-lua-engine, so that support for a Lua repl can be disabled for the wasm build. A new wasm directory contains: - pandoc.js, a JavaScript wrapper for pandoc.wasm - index.html + index.js, a web app exposing all of pandoc's functionality in a GUI interface - a set of illustrative examples that can be loaded from the web app Most of the code in index.html and index.js has been produced in interaction with Claude code.
70 lines
2.4 KiB
Haskell
70 lines
2.4 KiB
Haskell
{-# LANGUAGE CPP #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{- |
|
|
Module : PandocCLI.Lua
|
|
Copyright : © 2022-2024 Albert Krewinkel
|
|
License : GPL-2.0-or-later
|
|
Maintainer : Albert Krewinkel <albert+pandoc@tarleb.com>
|
|
|
|
Functions to run the pandoc Lua scripting engine.
|
|
-}
|
|
module PandocCLI.Lua (runLuaInterpreter, getEngine) where
|
|
|
|
#ifdef REPL
|
|
import Control.Monad ((<=<))
|
|
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)
|
|
import Text.Pandoc.Version (pandocVersionText)
|
|
import HsLua.CLI (EnvBehavior (..), Settings (..), runStandalone)
|
|
#else
|
|
import Text.Pandoc.Lua (getEngine)
|
|
import System.IO (stderr, hPutStrLn)
|
|
import System.Exit (exitWith, ExitCode(..))
|
|
#endif
|
|
|
|
-- | 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 ()
|
|
#ifdef REPL
|
|
runLuaInterpreter progName args = do
|
|
-- 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 <>
|
|
" Copyright (C) 2006-2024 John MacFarlane"
|
|
, settingsRunner = runner
|
|
, settingsHistory = Just histfile
|
|
}
|
|
runStandalone settings progName args
|
|
runner envBehavior =
|
|
let runLua' = case envBehavior of
|
|
IgnoreEnvVars -> runLuaNoEnv
|
|
ConsultEnvVars -> runLua
|
|
in handleError <=< runIOorExplode . runLua'
|
|
#else
|
|
runLuaInterpreter _ _ = do
|
|
hPutStrLn stderr "Pandoc not compiled with Lua interpreter support."
|
|
exitWith $ ExitFailure 4
|
|
#endif
|