Avoid a pointless Text -> String conversion on output.

This commit is contained in:
John MacFarlane 2019-08-07 09:23:59 -07:00
parent ec55382f8d
commit 87ce1ffbd9

View file

@ -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