From e0bea476bf3932e32b2e15827dfded61fcd9e87a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 27 Jun 2023 11:15:31 -0700 Subject: Use toTextM instead of toText when possible. (That is, whenever we have the filename and are in a PandocMonad instance.) This will lead to more informative error messages for UTF8 encoding, indicating the file path and byte offset where the error occurs. Closes #8884. --- src/Text/Pandoc/App.hs | 7 ++++--- src/Text/Pandoc/App/OutputSettings.hs | 9 +++++---- src/Text/Pandoc/Citeproc.hs | 21 +++++++++++---------- src/Text/Pandoc/Templates.hs | 12 ++++++------ src/Text/Pandoc/Translations.hs | 6 +++--- 5 files changed, 29 insertions(+), 26 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/App.hs b/src/Text/Pandoc/App.hs index 76828e9d8..6d2208785 100644 --- a/src/Text/Pandoc/App.hs +++ b/src/Text/Pandoc/App.hs @@ -366,10 +366,11 @@ configureCommonState datadir opts = do -- only affect the Markdown reader. readAbbreviations :: PandocMonad m => Maybe FilePath -> m (Set.Set Text) readAbbreviations mbfilepath = - Set.fromList . filter (not . T.null) . T.lines . UTF8.toText <$> - case mbfilepath of + (case mbfilepath of Nothing -> readDataFile "abbreviations" - Just f -> readFileStrict f + Just f -> readFileStrict f) + >>= fmap (Set.fromList . filter (not . T.null) . T.lines) . + toTextM (fromMaybe mempty mbfilepath) createPngFallbacks :: (PandocMonad m, MonadIO m) => Int -> m () createPngFallbacks dpi = do diff --git a/src/Text/Pandoc/App/OutputSettings.hs b/src/Text/Pandoc/App/OutputSettings.hs index af5c0ddbc..7f5d844e4 100644 --- a/src/Text/Pandoc/App/OutputSettings.hs +++ b/src/Text/Pandoc/App/OutputSettings.hs @@ -47,7 +47,7 @@ import Text.Pandoc.Scripting (ScriptingEngine (engineLoadCustom), import qualified Text.Pandoc.UTF8 as UTF8 readUtf8File :: PandocMonad m => FilePath -> m T.Text -readUtf8File = fmap UTF8.toText . readFileStrict +readUtf8File fp = readFileStrict fp >>= toTextM fp -- | Settings specifying how document output should be produced. data OutputSettings m = OutputSettings @@ -174,7 +174,7 @@ optToOutputSettings scriptingEngine opts = do (ListVal $ v : map toVal vs) ctxMap Nothing -> M.insert k (toVal vs) ctxMap - let getTextContents fp = UTF8.toText . fst <$> fetchItem (T.pack fp) + let getTextContents fp = (fst <$> fetchItem (T.pack fp)) >>= toTextM fp let setFilesVariableM k fps ctx = do xs <- mapM getTextContents fps @@ -209,8 +209,9 @@ optToOutputSettings scriptingEngine opts = do >>= (\vars -> if format == "dzslides" then do - dztempl <- UTF8.toText <$> readDataFile - ("dzslides" "template.html") + dztempl <- + let fp = "dzslides" "template.html" + in readDataFile fp >>= toTextM fp let dzline = "