aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2019-09-22 16:51:58 -0700
committerJohn MacFarlane <[email protected]>2019-09-22 16:51:58 -0700
commitab286df364399a8975b72dc86de9c7d85eeaff54 (patch)
treeb60bfe2a30e5ec3dd9e3da1e74ec4c21ca3d6749
parent84a5d18a6b6b5aad2bac849e90ee41dae4618478 (diff)
Revert "Class: remove stStdin and readStdinStrict."sandboxed
This reverts commit 4b54454c8f49678d48e7fb33fd7f5471672ad3c7.
-rw-r--r--src/Text/Pandoc/App.hs6
-rw-r--r--src/Text/Pandoc/Class.hs23
-rw-r--r--src/Text/Pandoc/Error.hs4
3 files changed, 22 insertions, 11 deletions
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs
index 8f6eac40e..1792a39bb 100644
--- a/src/Text/Pandoc/App.hs
+++ b/src/Text/Pandoc/App.hs
@@ -275,7 +275,7 @@ convertWithOpts opts = do
r readerOpts . convertTabs
ByteStringReader r ->
mconcat <$> mapM
- (readFileLazy >=> r readerOpts) sources'
+ (readFile' >=> r readerOpts) sources'
when (readerName == "markdown_github" ||
@@ -376,6 +376,10 @@ readSources srcs = T.intercalate (T.pack "\n") <$> mapM readSource srcs
readURI :: PandocMonad m => FilePath -> m Text
readURI src = UTF8.toText . fst <$> openURL src
+readFile' :: PandocMonad m => FilePath -> m BL.ByteString
+readFile' "-" = BL.fromStrict <$> readStdinStrict
+readFile' f = readFileLazy f
+
writeFnBinary :: MonadIO m => FilePath -> BL.ByteString -> m ()
writeFnBinary "-" = liftIO . BL.putStr
writeFnBinary f = liftIO . BL.writeFile (UTF8.encodePath f)
diff --git a/src/Text/Pandoc/Class.hs b/src/Text/Pandoc/Class.hs
index 7d4db600c..eabaf14fa 100644
--- a/src/Text/Pandoc/Class.hs
+++ b/src/Text/Pandoc/Class.hs
@@ -171,11 +171,14 @@ class (Functor m, Applicative m, Monad m, MonadError PandocError m)
-- an error on failure.
openURL :: String -> m (B.ByteString, Maybe MimeType)
-- | Read the lazy ByteString contents from a file path,
- -- raising an error on failure. The path "-" is treated as stdin.
+ -- raising an error on failure.
readFileLazy :: FilePath -> m BL.ByteString
-- | Read the strict ByteString contents from a file path,
- -- raising an error on failure. The path "-" is treated as stdin.
+ -- raising an error on failure.
readFileStrict :: FilePath -> m B.ByteString
+ -- | Read from 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.
@@ -239,7 +242,9 @@ report msg = do
-- not UTF-8 encoded. If file path is "-", stdin is read instead.
readTextFile :: PandocMonad m => FilePath -> m Text
readTextFile fp = do
- bs <- readFileStrict fp
+ bs <- if fp == "-"
+ then readStdinStrict
+ else readFileStrict fp
case decodeUtf8' . filterCRs . dropBOM $ bs of
Right t -> return t
Left (TSE.DecodeError _ (Just w)) -> throwError $
@@ -527,10 +532,9 @@ instance PandocMonad PandocIO where
Right r -> return r
Left e -> throwError $ PandocHttpError u e
- readFileLazy "-" = liftIOError (\_ -> BL.getContents) "stdin"
- readFileLazy s = liftIOError BL.readFile s
- readFileStrict "-" = liftIOError (\_ -> B.getContents) "stdin"
- readFileStrict s = liftIOError B.readFile s
+ readFileLazy s = liftIOError BL.readFile s
+ readFileStrict s = liftIOError B.readFile s
+ readStdinStrict = liftIOError (\_ -> B.getContents) "stdin"
glob = liftIOError IO.glob
fileExists = liftIOError Directory.doesFileExist
#ifdef EMBED_DATA_FILES
@@ -910,6 +914,7 @@ data PureState = PureState { stStdGen :: StdGen
, stReferencePptx :: Archive
, stReferenceODT :: Archive
, stFiles :: FileTree
+ , stStdin :: B.ByteString
, stUserDataFiles :: FileTree
, stCabalDataFiles :: FileTree
}
@@ -925,6 +930,7 @@ instance Default PureState where
, stReferencePptx = emptyArchive
, stReferenceODT = emptyArchive
, stFiles = mempty
+ , stStdin = mempty
, stUserDataFiles = mempty
, stCabalDataFiles = mempty
}
@@ -1026,6 +1032,7 @@ instance PandocMonad PandocPure where
case infoFileContents <$> getFileInfo fp fps of
Just bs -> return bs
Nothing -> throwError $ PandocResourceNotFound fp
+ readStdinStrict = getsPureState stStdin
glob s = do
FileTree ftmap <- getsPureState stFiles
@@ -1066,6 +1073,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
@@ -1083,6 +1091,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/Error.hs b/src/Text/Pandoc/Error.hs
index e4ddce42a..ae66162b3 100644
--- a/src/Text/Pandoc/Error.hs
+++ b/src/Text/Pandoc/Error.hs
@@ -63,9 +63,7 @@ handleError :: Either PandocError a -> IO a
handleError (Right r) = return r
handleError (Left e) =
case e of
- PandocIOError fp err' -> do
- UTF8.hPutStrLn stderr $ "IO Error (" ++ show fp ++ ")"
- ioError err'
+ PandocIOError _ err' -> ioError err'
PandocHttpError u err' -> err 61 $
"Could not fetch " ++ u ++ "\n" ++ show err'
PandocShouldNeverHappenError s -> err 62 $