diff options
| author | Albert Krewinkel <albert@zeitkraut.de> | 2022-10-12 11:48:21 +0200 |
|---|---|---|
| committer | Albert Krewinkel <albert@zeitkraut.de> | 2022-10-12 11:58:18 +0200 |
| commit | 253d2e768a43c8ab3ad8e1c46b2bc4a02acec946 (patch) | |
| tree | 591d2f32798cdcd5d6b841b162bbd6aa654ce4bb /pandoc-lua-engine/src | |
| parent | 543cb5d45d3ebc4c5a504be42efd6febb3a9ceaa (diff) | |
Lua: support extensions in custom readers.
Like custom readers, like writers, can define the set of supported
extensions by setting a global. E.g.:
``` lua
reader_extensions = {
smart = true,
citations = false,
}
```
Diffstat (limited to 'pandoc-lua-engine/src')
| -rw-r--r-- | pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs index 6aeda526f..2c2dd369b 100644 --- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs +++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Reader.hs @@ -1,6 +1,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TupleSections #-} {- | Module : Text.Pandoc.Lua.Reader Copyright : Copyright (C) 2021-2022 John MacFarlane @@ -14,17 +15,19 @@ 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 HsLua.Core.Run (GCManagedState, newGCManagedState, withGCManagedState) import Text.Pandoc.Class (PandocMonad, findFileWithDataFallback, report) import Text.Pandoc.Error (PandocError) +import Text.Pandoc.Format (ExtensionsConfig (..)) import Text.Pandoc.Logging import Text.Pandoc.Lua.Global (Global (..), setGlobals) import Text.Pandoc.Lua.Init (runLuaWith) import Text.Pandoc.Lua.PandocLua +import Text.Pandoc.Lua.Marshal.Format (peekExtensionsConfig) import Text.Pandoc.Lua.Marshal.Pandoc (peekPandoc) import Text.Pandoc.Readers (Reader (..)) import Text.Pandoc.Sources (ToSources(..), sourcesToText) @@ -32,7 +35,7 @@ import qualified Data.Text as T -- | Convert custom markup to Pandoc. readCustom :: (PandocMonad m, MonadIO m) - => FilePath -> m (Reader m) + => FilePath -> m (Reader m, ExtensionsConfig) readCustom luaFile = do luaState <- liftIO newGCManagedState luaFile' <- fromMaybe luaFile <$> findFileWithDataFallback "readers" luaFile @@ -44,7 +47,12 @@ readCustom luaFile = do -- to handle this more gracefully): when (stat /= Lua.OK) Lua.throwErrorAsException - getCustomReader luaState + + extsConf <- getglobal "reader_extensions" >>= \case + TypeNil -> pure $ ExtensionsConfig mempty mempty + _ -> forcePeek $ peekExtensionsConfig top `lastly` pop 1 + + (,extsConf) <$!> getCustomReader luaState where readerField = "PANDOC Reader function" |
