aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2021-08-22 17:47:18 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2021-08-24 22:19:15 -0700
commitc39ddeb8f86e9dec5bd6096685812452e3f2c65e (patch)
tree7c9425531bcf7b60a9f1c8b9ef789ca3261ba91f
parent8ad22002cb5ac4c79acda84c7256c6a122e2dd93 (diff)
downloadpandoc-c39ddeb8f86e9dec5bd6096685812452e3f2c65e.tar.gz
Text.Pandoc.Class: add readStdinStrict method to PandocMonad.
[API change]
-rw-r--r--src/Text/Pandoc/Class/IO.hs6
-rw-r--r--src/Text/Pandoc/Class/PandocIO.hs1
-rw-r--r--src/Text/Pandoc/Class/PandocMonad.hs5
-rw-r--r--src/Text/Pandoc/Class/PandocPure.hs4
-rw-r--r--src/Text/Pandoc/Lua/PandocLua.hs1
5 files changed, 17 insertions, 0 deletions
diff --git a/src/Text/Pandoc/Class/IO.hs b/src/Text/Pandoc/Class/IO.hs
index f4cfc8682..305f07a01 100644
--- a/src/Text/Pandoc/Class/IO.hs
+++ b/src/Text/Pandoc/Class/IO.hs
@@ -30,6 +30,7 @@ module Text.Pandoc.Class.IO
, openURL
, readFileLazy
, readFileStrict
+ , readStdinStrict
, extractMedia
) where
@@ -158,6 +159,11 @@ readFileLazy s = liftIOError BL.readFile s
readFileStrict :: (PandocMonad m, MonadIO m) => FilePath -> m B.ByteString
readFileStrict s = liftIOError B.readFile s
+-- | Read the strict ByteString contents from stdin, raising
+-- an error on failure.
+readStdinStrict :: (PandocMonad m, MonadIO m) => m B.ByteString
+readStdinStrict = liftIOError (const B.getContents) "stdin"
+
-- | Return a list of paths that match a glob, relative to the working
-- directory. See 'System.FilePath.Glob' for the glob syntax.
glob :: (PandocMonad m, MonadIO m) => String -> m [FilePath]
diff --git a/src/Text/Pandoc/Class/PandocIO.hs b/src/Text/Pandoc/Class/PandocIO.hs
index 86038a659..61ee1f1c6 100644
--- a/src/Text/Pandoc/Class/PandocIO.hs
+++ b/src/Text/Pandoc/Class/PandocIO.hs
@@ -62,6 +62,7 @@ instance PandocMonad PandocIO where
openURL = IO.openURL
readFileLazy = IO.readFileLazy
readFileStrict = IO.readFileStrict
+ readStdinStrict = IO.readStdinStrict
glob = IO.glob
fileExists = IO.fileExists
diff --git a/src/Text/Pandoc/Class/PandocMonad.hs b/src/Text/Pandoc/Class/PandocMonad.hs
index 439aec071..d3cf1201c 100644
--- a/src/Text/Pandoc/Class/PandocMonad.hs
+++ b/src/Text/Pandoc/Class/PandocMonad.hs
@@ -117,6 +117,9 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
-- | Read the strict ByteString contents from a file path,
-- raising an error on failure.
readFileStrict :: FilePath -> m B.ByteString
+ -- | Read the contents of stdin as a strict ByteString, raising
+ -- an error on failure.
+ readStdinStrict :: m B.ByteString
-- | Return a list of paths that match a glob, relative to
-- the working directory. See 'System.FilePath.Glob' for
-- the glob syntax.
@@ -674,6 +677,7 @@ instance (MonadTrans t, PandocMonad m, Functor (t m),
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
+ readStdinStrict = lift readStdinStrict
glob = lift . glob
fileExists = lift . fileExists
getDataFileName = lift . getDataFileName
@@ -691,6 +695,7 @@ instance {-# OVERLAPS #-} PandocMonad m => PandocMonad (ParsecT s st m) where
openURL = lift . openURL
readFileLazy = lift . readFileLazy
readFileStrict = lift . readFileStrict
+ readStdinStrict = lift readStdinStrict
glob = lift . glob
fileExists = lift . fileExists
getDataFileName = lift . getDataFileName
diff --git a/src/Text/Pandoc/Class/PandocPure.hs b/src/Text/Pandoc/Class/PandocPure.hs
index 23c941839..290a6d97c 100644
--- a/src/Text/Pandoc/Class/PandocPure.hs
+++ b/src/Text/Pandoc/Class/PandocPure.hs
@@ -64,6 +64,7 @@ data PureState = PureState
, stReferencePptx :: Archive
, stReferenceODT :: Archive
, stFiles :: FileTree
+ , stStdin :: B.ByteString
, stUserDataFiles :: FileTree
, stCabalDataFiles :: FileTree
}
@@ -80,6 +81,7 @@ instance Default PureState where
, stReferencePptx = emptyArchive
, stReferenceODT = emptyArchive
, stFiles = mempty
+ , stStdin = mempty
, stUserDataFiles = mempty
, stCabalDataFiles = mempty
}
@@ -193,6 +195,8 @@ instance PandocMonad PandocPure where
Just bs -> return bs
Nothing -> throwError $ PandocResourceNotFound $ T.pack fp
+ readStdinStrict = getsPureState stStdin
+
glob s = do
FileTree ftmap <- getsPureState stFiles
return $ filter (match (compile s)) $ M.keys ftmap
diff --git a/src/Text/Pandoc/Lua/PandocLua.hs b/src/Text/Pandoc/Lua/PandocLua.hs
index 3f6641f95..b7f084957 100644
--- a/src/Text/Pandoc/Lua/PandocLua.hs
+++ b/src/Text/Pandoc/Lua/PandocLua.hs
@@ -126,6 +126,7 @@ instance PandocMonad PandocLua where
readFileLazy = IO.readFileLazy
readFileStrict = IO.readFileStrict
+ readStdinStrict = IO.readStdinStrict
glob = IO.glob
fileExists = IO.fileExists