diff options
| author | Albert Krewinkel <albert@zeitkraut.de> | 2022-10-10 09:12:11 +0200 |
|---|---|---|
| committer | Albert Krewinkel <albert@zeitkraut.de> | 2022-10-10 09:13:09 +0200 |
| commit | 3e1d3b5e5efb269a75550f70156c722de3da56d0 (patch) | |
| tree | 3790e05989bd14044a7c807df1d5dee321e3a308 | |
| parent | 5d858e4119af3154ad8682ffc331115a01428e4c (diff) | |
Lua: use `Reader` type for custom readers.
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs | 27 | ||||
| -rw-r--r-- | src/Text/Pandoc/App.hs | 9 | ||||
| -rw-r--r-- | src/Text/Pandoc/Scripting.hs | 6 |
3 files changed, 17 insertions, 25 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs index 6303dace3..44781c9fb 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs @@ -13,40 +13,39 @@ Supports custom parsers written in Lua which produce a Pandoc AST. -} module Text.Pandoc.Lua.Reader ( readCustom ) where import Control.Exception -import Control.Monad (when) +import Control.Monad ((<=<), when) import Control.Monad.IO.Class (MonadIO) import Data.Maybe (fromMaybe) import HsLua as Lua hiding (Operation (Div)) -import Text.Pandoc.Definition +import HsLua.Core.Run (newGCManagedState, withGCManagedState) import Text.Pandoc.Class (PandocMonad, findFileWithDataFallback, report) import Text.Pandoc.Logging import Text.Pandoc.Lua.Global (Global (..), setGlobals) -import Text.Pandoc.Lua.Init (runLua) +import Text.Pandoc.Lua.Init (runLuaWith) import Text.Pandoc.Lua.PandocLua import Text.Pandoc.Lua.Marshal.Pandoc (peekPandoc) -import Text.Pandoc.Options +import Text.Pandoc.Readers (Reader (..)) import Text.Pandoc.Sources (ToSources(..), sourcesToText) import qualified Data.Text as T -- | Convert custom markup to Pandoc. -readCustom :: (PandocMonad m, MonadIO m, ToSources s) - => FilePath -> ReaderOptions -> s -> m Pandoc -readCustom luaFile opts srcs = do - let globals = [ PANDOC_SCRIPT_FILE luaFile ] +readCustom :: (PandocMonad m, MonadIO m) + => FilePath -> m (Reader m) +readCustom luaFile = do + luaState <- liftIO newGCManagedState luaFile' <- fromMaybe luaFile <$> findFileWithDataFallback "readers" luaFile - res <- runLua $ do + either throw pure <=< runLuaWith luaState $ do + let globals = [ PANDOC_SCRIPT_FILE luaFile ] setGlobals globals stat <- dofileTrace luaFile' -- check for error in lua script (later we'll change the return type -- to handle this more gracefully): when (stat /= Lua.OK) Lua.throwErrorAsException - parseCustom - case res of - Left msg -> throw msg - Right doc -> return doc + pure (reader luaState) + where - parseCustom = do + reader st = TextReader $ \opts srcs -> liftIO . withGCManagedState st $ do let input = toSources srcs getglobal "Reader" push input diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 49fe34172..b4a9454e5 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -64,7 +64,6 @@ import Text.Pandoc.Scripting (ScriptingEngine (..)) import Text.Pandoc.SelfContained (makeSelfContained) import Text.Pandoc.Shared (eastAsianLineBreakFilter, headerShift, isURI, filterIpynbOutput, defaultUserDataDir, tshow) -import Text.Pandoc.Sources (toSources) import Text.Pandoc.Writers.Shared (lookupMetaString) import Text.Pandoc.Readers.Markdown (yamlToMeta) import qualified Text.Pandoc.UTF8 as UTF8 @@ -166,13 +165,7 @@ convertWithOpts' scriptingEngine istty datadir opts = do (reader, readerExts) <- if ".lua" `T.isSuffixOf` readerName - then return ( TextReader $ \ropts s -> - engineReadCustom scriptingEngine - (T.unpack readerName) - ropts - (toSources s) - , mempty - ) + then (,mempty) <$> engineReadCustom scriptingEngine (T.unpack readerName) else if optSandbox opts then case runPure (getReader flvrd) of Left e -> throwError e diff --git a/src/Text/Pandoc/Scripting.hs b/src/Text/Pandoc/Scripting.hs index 17bc2f972..9df00a5cf 100644 --- a/src/Text/Pandoc/Scripting.hs +++ b/src/Text/Pandoc/Scripting.hs @@ -22,7 +22,7 @@ import Text.Pandoc.Definition (Pandoc) import Text.Pandoc.Class.PandocMonad (PandocMonad) import Text.Pandoc.Error (PandocError (PandocNoScriptingEngine)) import Text.Pandoc.Filter.Environment (Environment) -import Text.Pandoc.Options (ReaderOptions) +import Text.Pandoc.Readers (Reader) import Text.Pandoc.Sources (Sources) import Text.Pandoc.Writers (Writer) @@ -36,7 +36,7 @@ data ScriptingEngine = ScriptingEngine -- ^ Use the scripting engine to run a filter. , engineReadCustom :: forall m. (PandocMonad m, MonadIO m) - => FilePath -> ReaderOptions -> Sources -> m Pandoc + => FilePath -> m (Reader m) -- ^ Function to parse input into a 'Pandoc' document. , engineWriteCustom :: forall m. (PandocMonad m, MonadIO m) @@ -49,7 +49,7 @@ noEngine = ScriptingEngine { engineName = "none" , engineApplyFilter = \_env _args _fp _doc -> throwError PandocNoScriptingEngine - , engineReadCustom = \_fp _ropts _sources -> + , engineReadCustom = \_fp -> throwError PandocNoScriptingEngine , engineWriteCustom = \_fp -> throwError PandocNoScriptingEngine |
