summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Error.hs
diff options
context:
space:
mode:
authordespresc <christian.j.j.despres@gmail.com>2019-11-04 16:12:37 -0500
committerJohn MacFarlane <jgm@berkeley.edu>2019-11-12 16:03:45 -0800
commit90e436d49604e3fd1ef9432fb23f6d7f6245c7fd (patch)
tree4e7f0692f989643189f1fc6786050d95e239a0ea /src/Text/Pandoc/Error.hs
parentd3966372f5049eea56213b069fc4d70d8af9144c (diff)
Switch to new pandoc-types and use Text instead of String [API change].
PR #5884. + Use pandoc-types 1.20 and texmath 0.12. + Text is now used instead of String, with a few exceptions. + In the MediaBag module, some of the types using Strings were switched to use FilePath instead (not Text). + In the Parsing module, new parsers `manyChar`, `many1Char`, `manyTillChar`, `many1TillChar`, `many1Till`, `manyUntil`, `mantyUntilChar` have been added: these are like their unsuffixed counterparts but pack some or all of their output. + `glob` in Text.Pandoc.Class still takes String since it seems to be intended as an interface to Glob, which uses strings. It seems to be used only once in the package, in the EPUB writer, so that is not hard to change.
Diffstat (limited to 'src/Text/Pandoc/Error.hs')
-rw-r--r--src/Text/Pandoc/Error.hs116
1 files changed, 61 insertions, 55 deletions
diff --git a/src/Text/Pandoc/Error.hs b/src/Text/Pandoc/Error.hs
index 113ab9d6e..38db4fda9 100644
--- a/src/Text/Pandoc/Error.hs
+++ b/src/Text/Pandoc/Error.hs
@@ -1,6 +1,7 @@
-{-# LANGUAGE NoImplicitPrelude #-}
+{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
+{-# LANGUAGE OverloadedStrings #-}
{- |
Module : Text.Pandoc.Error
Copyright : Copyright (C) 2006-2019 John MacFarlane
@@ -22,6 +23,8 @@ import Prelude
import Control.Exception (Exception)
import Data.Typeable (Typeable)
import Data.Word (Word8)
+import Data.Text (Text)
+import qualified Data.Text as T
import GHC.Generics (Generic)
import Network.HTTP.Client (HttpException)
import System.Exit (ExitCode (..), exitWith)
@@ -31,32 +34,32 @@ import Text.Printf (printf)
import Text.Parsec.Error
import Text.Parsec.Pos hiding (Line)
-type Input = String
+type Input = Text
-data PandocError = PandocIOError String IOError
- | PandocHttpError String HttpException
- | PandocShouldNeverHappenError String
- | PandocSomeError String
- | PandocParseError String
+data PandocError = PandocIOError Text IOError
+ | PandocHttpError Text HttpException
+ | PandocShouldNeverHappenError Text
+ | PandocSomeError Text
+ | PandocParseError Text
| PandocParsecError Input ParseError
- | PandocMakePDFError String
- | PandocOptionError String
- | PandocSyntaxMapError String
+ | PandocMakePDFError Text
+ | PandocOptionError Text
+ | PandocSyntaxMapError Text
| PandocFailOnWarningError
- | PandocPDFProgramNotFoundError String
- | PandocPDFError String
- | PandocFilterError String String
- | PandocCouldNotFindDataFileError String
- | PandocResourceNotFound String
- | PandocTemplateError String
- | PandocAppError String
- | PandocEpubSubdirectoryError String
- | PandocMacroLoop String
- | PandocUTF8DecodingError String Int Word8
- | PandocIpynbDecodingError String
- | PandocUnknownReaderError String
- | PandocUnknownWriterError String
- | PandocUnsupportedExtensionError String String
+ | PandocPDFProgramNotFoundError Text
+ | PandocPDFError Text
+ | PandocFilterError Text Text
+ | PandocCouldNotFindDataFileError Text
+ | PandocResourceNotFound Text
+ | PandocTemplateError Text
+ | PandocAppError Text
+ | PandocEpubSubdirectoryError Text
+ | PandocMacroLoop Text
+ | PandocUTF8DecodingError Text Int Word8
+ | PandocIpynbDecodingError Text
+ | PandocUnknownReaderError Text
+ | PandocUnknownWriterError Text
+ | PandocUnsupportedExtensionError Text Text
deriving (Show, Typeable, Generic)
instance Exception PandocError
@@ -68,23 +71,23 @@ handleError (Left e) =
case e of
PandocIOError _ err' -> ioError err'
PandocHttpError u err' -> err 61 $
- "Could not fetch " ++ u ++ "\n" ++ show err'
+ "Could not fetch " <> u <> "\n" <> tshow err'
PandocShouldNeverHappenError s -> err 62 $
- "Something we thought was impossible happened!\n" ++
- "Please report this to pandoc's developers: " ++ s
+ "Something we thought was impossible happened!\n" <>
+ "Please report this to pandoc's developers: " <> s
PandocSomeError s -> err 63 s
PandocParseError s -> err 64 s
PandocParsecError input err' ->
let errPos = errorPos err'
errLine = sourceLine errPos
errColumn = sourceColumn errPos
- ls = lines input ++ [""]
+ ls = T.lines input <> [""]
errorInFile = if length ls > errLine - 1
- then concat ["\n", ls !! (errLine - 1)
- ,"\n", replicate (errColumn - 1) ' '
- ,"^"]
+ then T.concat ["\n", ls !! (errLine - 1)
+ ,"\n", T.replicate (errColumn - 1) " "
+ ,"^"]
else ""
- in err 65 $ "\nError at " ++ show err' ++
+ in err 65 $ "\nError at " <> tshow err' <>
-- if error comes from a chunk or included file,
-- then we won't get the right text this way:
if sourceName errPos == "source"
@@ -95,49 +98,52 @@ handleError (Left e) =
PandocSyntaxMapError s -> err 67 s
PandocFailOnWarningError -> err 3 "Failing because there were warnings."
PandocPDFProgramNotFoundError pdfprog -> err 47 $
- pdfprog ++ " not found. Please select a different --pdf-engine or install " ++ pdfprog
- PandocPDFError logmsg -> err 43 $ "Error producing PDF.\n" ++ logmsg
- PandocFilterError filtername msg -> err 83 $ "Error running filter " ++
- filtername ++ ":\n" ++ msg
+ pdfprog <> " not found. Please select a different --pdf-engine or install " <> pdfprog
+ PandocPDFError logmsg -> err 43 $ "Error producing PDF.\n" <> logmsg
+ PandocFilterError filtername msg -> err 83 $ "Error running filter " <>
+ filtername <> ":\n" <> msg
PandocCouldNotFindDataFileError fn -> err 97 $
- "Could not find data file " ++ fn
+ "Could not find data file " <> fn
PandocResourceNotFound fn -> err 99 $
- "File " ++ fn ++ " not found in resource path"
- PandocTemplateError s -> err 5 $ "Error compiling template " ++ s
+ "File " <> fn <> " not found in resource path"
+ PandocTemplateError s -> err 5 $ "Error compiling template " <> s
PandocAppError s -> err 4 s
PandocEpubSubdirectoryError s -> err 31 $
- "EPUB subdirectory name '" ++ s ++ "' contains illegal characters"
+ "EPUB subdirectory name '" <> s <> "' contains illegal characters"
PandocMacroLoop s -> err 91 $
- "Loop encountered in expanding macro " ++ s
+ "Loop encountered in expanding macro " <> s
PandocUTF8DecodingError f offset w -> err 92 $
- "UTF-8 decoding error in " ++ f ++ " at byte offset " ++ show offset ++
- " (" ++ printf "%2x" w ++ ").\n" ++
+ "UTF-8 decoding error in " <> f <> " at byte offset " <> tshow offset <>
+ " (" <> T.pack (printf "%2x" w) <> ").\n" <>
"The input must be a UTF-8 encoded text."
PandocIpynbDecodingError w -> err 93 $
- "ipynb decoding error: " ++ w
+ "ipynb decoding error: " <> w
PandocUnknownReaderError r -> err 21 $
- "Unknown input format " ++ r ++
+ "Unknown input format " <> r <>
case r of
- "doc" -> "\nPandoc can convert from DOCX, but not from DOC." ++
- "\nTry using Word to save your DOC file as DOCX," ++
+ "doc" -> "\nPandoc can convert from DOCX, but not from DOC." <>
+ "\nTry using Word to save your DOC file as DOCX," <>
" and convert that with pandoc."
"pdf" -> "\nPandoc can convert to PDF, but not from PDF."
_ -> ""
PandocUnknownWriterError w -> err 22 $
- "Unknown output format " ++ w ++
+ "Unknown output format " <> w <>
case w of
- "pdf" -> "To create a pdf using pandoc, use" ++
- " -t latex|beamer|context|ms|html5" ++
- "\nand specify an output file with " ++
+ "pdf" -> "To create a pdf using pandoc, use" <>
+ " -t latex|beamer|context|ms|html5" <>
+ "\nand specify an output file with " <>
".pdf extension (-o filename.pdf)."
"doc" -> "\nPandoc can convert to DOCX, but not from DOC."
_ -> ""
PandocUnsupportedExtensionError ext f -> err 23 $
- "The extension " ++ ext ++ " is not supported " ++
- "for " ++ f
+ "The extension " <> ext <> " is not supported " <>
+ "for " <> f
-err :: Int -> String -> IO a
+err :: Int -> Text -> IO a
err exitCode msg = do
- UTF8.hPutStrLn stderr msg
+ UTF8.hPutStrLn stderr (T.unpack msg)
exitWith $ ExitFailure exitCode
return undefined
+
+tshow :: Show a => a -> Text
+tshow = T.pack . show