summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Lua/Module
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2022-03-30 23:10:30 +0200
committerGitHub <noreply@github.com>2022-03-30 14:10:30 -0700
commitad726953b9cb7d2bf164ec091227872e97cf8f65 (patch)
tree9cacc25f86bfd87e778cc0651a29b3ac7feb6e9f /src/Text/Pandoc/Lua/Module
parent63de34481ad17882e92e6d782be57870bfb4c8f9 (diff)
Lua: allow to pass Sources to pandoc.read (#8002)
Sources, the data type passed to the `Reader` function in custom readers, are now accepted as input to `pandoc.read`.
Diffstat (limited to 'src/Text/Pandoc/Lua/Module')
-rw-r--r--src/Text/Pandoc/Lua/Module/Pandoc.hs20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/Text/Pandoc/Lua/Module/Pandoc.hs b/src/Text/Pandoc/Lua/Module/Pandoc.hs
index 7d8a98bb1..3dacc48de 100644
--- a/src/Text/Pandoc/Lua/Module/Pandoc.hs
+++ b/src/Text/Pandoc/Lua/Module/Pandoc.hs
@@ -19,6 +19,7 @@ module Text.Pandoc.Lua.Module.Pandoc
) where
import Prelude hiding (read)
+import Control.Applicative ((<|>))
import Control.Monad (forM_, when)
import Control.Monad.Catch (catch, throwM)
import Data.Data (Data, dataTypeConstrs, dataTypeOf, showConstr)
@@ -34,6 +35,7 @@ import Text.Pandoc.Lua.Marshal.AST
import Text.Pandoc.Lua.Marshal.Filter (peekFilter)
import Text.Pandoc.Lua.Marshal.ReaderOptions ( peekReaderOptions
, pushReaderOptions)
+import Text.Pandoc.Lua.Marshal.Sources (peekSources)
import Text.Pandoc.Lua.Marshal.WriterOptions ( peekWriterOptions
, pushWriterOptions)
import Text.Pandoc.Lua.Module.Utils (sha1)
@@ -42,6 +44,7 @@ import Text.Pandoc.Options ( ReaderOptions (readerExtensions)
, WriterOptions (writerExtensions) )
import Text.Pandoc.Process (pipeProcess)
import Text.Pandoc.Readers (Reader (..), getReader)
+import Text.Pandoc.Sources (toSources)
import Text.Pandoc.Writers (Writer (..), getWriter)
import qualified HsLua as Lua
@@ -180,10 +183,17 @@ functions =
let formatSpec = fromMaybe "markdown" mformatspec
readerOpts = fromMaybe def mreaderOptions
readAction = getReader formatSpec >>= \case
- (TextReader r, es) ->
- r readerOpts{readerExtensions = es} (UTF8.toText content)
+ (TextReader r, es) ->
+ r readerOpts{readerExtensions = es}
+ (case content of
+ Left bs -> toSources $ UTF8.toText bs
+ Right sources -> sources)
(ByteStringReader r, es) ->
- r readerOpts{readerExtensions = es} (BSL.fromStrict content)
+ case content of
+ Left bs -> r readerOpts{readerExtensions = es}
+ (BSL.fromStrict bs)
+ Right _ -> liftPandocLua $ Lua.failLua
+ "Cannot use bytestring reader with Sources"
try (unPandocLua readAction) >>= \case
Right pd ->
-- success, got a Pandoc document
@@ -195,7 +205,9 @@ functions =
"Extension " <> e <> " not supported for " <> f
Left e ->
throwM e)
- <#> parameter peekByteString "string" "content" "text to parse"
+ <#> parameter (\idx -> (Left <$> peekByteString idx)
+ <|> (Right <$> peekSources idx))
+ "string|Sources" "content" "text to parse"
<#> opt (textParam "formatspec" "format and extensions")
<#> opt (parameter peekReaderOptions "ReaderOptions" "reader_options"
"reader options")