diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-08-07 09:23:59 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-08-07 09:23:59 -0700 |
commit | 87ce1ffbd97f6cb2785f9fbf05f68d3185f50675 (patch) | |
tree | 8cc8c82da8d1f38532eca48ffb0a2c5eeaf653c5 /src/Text | |
parent | ec55382f8d7102754307492ed9c6259d9cc09e4f (diff) | |
download | pandoc-87ce1ffbd97f6cb2785f9fbf05f68d3185f50675.tar.gz |
Avoid a pointless Text -> String conversion on output.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/App.hs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 737e43fd9..b6ab0d0ba 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -34,15 +34,16 @@ import Data.Maybe (fromMaybe, isJust, isNothing) import qualified Data.Set as Set import Data.Text (Text) import qualified Data.Text as T +import qualified Data.Text.IO as TIO import qualified Data.Text.Lazy as TL -import qualified Data.Text.Lazy.Encoding as TE import qualified Data.Text.Encoding.Error as TE -import qualified Data.Text.Encoding.Error as TSE +import qualified Data.Text.Lazy.Encoding as TLE import Network.URI (URI (..), parseURI) import System.Directory (doesDirectoryExist) import System.Exit (exitSuccess) import System.FilePath -import System.IO (nativeNewline, stdout) +import System.IO (nativeNewline, stdout, hSetNewlineMode, hSetEncoding, + NewlineMode(..), utf8) import qualified System.IO as IO (Newline (..)) import Text.Pandoc import Text.Pandoc.App.FormatHeuristics (formatFromFilePaths) @@ -295,7 +296,7 @@ convertWithOpts opts = do case res of Right pdf -> writeFnBinary outputFile pdf Left err' -> throwError $ PandocPDFError $ - TL.unpack (TE.decodeUtf8With TE.lenientDecode err') + TL.unpack (TLE.decodeUtf8With TE.lenientDecode err') Nothing -> do let addNl = if standalone @@ -365,7 +366,7 @@ readSource src = case parseURI src of else BS.readFile fp E.catch (return $! UTF8.toText bs) (\e -> case e of - TSE.DecodeError _ (Just w) -> do + TE.DecodeError _ (Just w) -> do case BS.elemIndex w bs of Just offset -> E.throwIO $ PandocUTF8DecodingError fp offset w @@ -384,6 +385,11 @@ writeFnBinary "-" = liftIO . BL.putStr writeFnBinary f = liftIO . BL.writeFile (UTF8.encodePath f) writerFn :: MonadIO m => IO.Newline -> FilePath -> Text -> m () --- TODO this implementation isn't maximally efficient: -writerFn eol "-" = liftIO . UTF8.putStrWith eol . T.unpack -writerFn eol f = liftIO . UTF8.writeFileWith eol f . T.unpack +writerFn eol "-" t = liftIO $ do + hSetNewlineMode stdout (NewlineMode eol eol) + hSetEncoding stdout utf8 + TIO.putStr t +writerFn eol f t = liftIO $ do + hSetNewlineMode stdout (NewlineMode eol eol) + hSetEncoding stdout utf8 + TIO.writeFile f t |