diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2023-12-15 12:23:14 -0800 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2023-12-15 12:23:33 -0800 |
| commit | 9f43da7f8183bb0519eda88664486f3f307e4b07 (patch) | |
| tree | ccdf4db025c7cb62336896afa2facff619af3663 /src/Text | |
| parent | 04acb3f7f7c8c0a1113ebd391d30267b0f4d9b40 (diff) | |
Logging: add MakePDFWarning constructor to LogMessage.
[API change]
Use this to pass LaTeX warnings on to user as warnings.
Diffstat (limited to 'src/Text')
| -rw-r--r-- | src/Text/Pandoc/Logging.hs | 5 | ||||
| -rw-r--r-- | src/Text/Pandoc/PDF.hs | 20 |
2 files changed, 23 insertions, 2 deletions
diff --git a/src/Text/Pandoc/Logging.hs b/src/Text/Pandoc/Logging.hs index 3b5d63049..9245107ee 100644 --- a/src/Text/Pandoc/Logging.hs +++ b/src/Text/Pandoc/Logging.hs @@ -100,6 +100,7 @@ data LogMessage = | DuplicateAttribute Text Text | NotUTF8Encoded FilePath | MakePDFInfo Text Text + | MakePDFWarning Text deriving (Show, Eq, Data, Ord, Typeable, Generic) instance ToJSON LogMessage where @@ -257,6 +258,8 @@ instance ToJSON LogMessage where MakePDFInfo description contents -> ["description" .= description ,"contents" .= contents] + MakePDFWarning message -> + ["message" .= message] showPos :: SourcePos -> Text showPos pos = Text.pack $ sn ++ "line " ++ @@ -396,6 +399,7 @@ showLogMessage msg = if Text.null contents then mempty else "\n" <> contents + MakePDFWarning message -> "[makePDF] " <> message messageVerbosity :: LogMessage -> Verbosity messageVerbosity msg = @@ -450,3 +454,4 @@ messageVerbosity msg = DuplicateAttribute{} -> WARNING NotUTF8Encoded{} -> WARNING MakePDFInfo{} -> INFO + MakePDFWarning{} -> WARNING diff --git a/src/Text/Pandoc/PDF.hs b/src/Text/Pandoc/PDF.hs index 2cef4404e..060262449 100644 --- a/src/Text/Pandoc/PDF.hs +++ b/src/Text/Pandoc/PDF.hs @@ -19,6 +19,7 @@ module Text.Pandoc.PDF ( makePDF ) where import qualified Codec.Picture as JP import qualified Control.Exception as E import Control.Monad.Trans (MonadIO (..)) +import Control.Monad (foldM_) import qualified Data.ByteString as BS import Data.ByteString.Lazy (ByteString) import qualified Data.ByteString.Lazy as BL @@ -38,7 +39,7 @@ import System.IO (hClose) import System.IO.Temp (withSystemTempDirectory, withTempDirectory, withTempFile) import qualified System.IO.Error as IE -import Text.DocLayout (literal) +import Text.DocLayout (literal, render, hsep) import Text.Pandoc.Definition import Text.Pandoc.Error (PandocError (PandocPDFProgramNotFoundError)) import Text.Pandoc.MIME (getMimeType) @@ -286,13 +287,14 @@ tex2pdf program args tmpDir' source = do return $ Left $ logmsg <> extramsg (ExitSuccess, Nothing) -> return $ Left "" (ExitSuccess, Just pdf) -> do + latexWarnings log' missingCharacterWarnings log' return $ Right pdf missingCharacterWarnings :: PandocMonad m => ByteString -> m () missingCharacterWarnings log' = do let ls = BC.lines log' - let isMissingCharacterWarning = BC.isPrefixOf "Missing character: " + let isMissingCharacterWarning = BC.isPrefixOf "Missing character:" let toCodePoint c | isAscii c = T.singleton c | otherwise = T.pack $ c : " (U+" ++ printf "%04X" (ord c) ++ ")" @@ -303,6 +305,20 @@ missingCharacterWarnings log' = do ] mapM_ (report . MissingCharacter) warnings +latexWarnings :: PandocMonad m => ByteString -> m () +latexWarnings log' = foldM_ go Nothing (BC.lines log') + where + go Nothing ln + | BC.isPrefixOf "LaTeX Warning:" ln = + pure $ Just ln + | otherwise = pure Nothing + go (Just msg) ln + | ln == "" = do -- emit report and reset accumulator + report $ MakePDFWarning $ render (Just 60) $ + hsep $ map literal $ T.words $ UTF8.toText $ BC.toStrict msg + pure Nothing + | otherwise = pure $ Just (msg <> ln) + -- parsing output extractMsg :: ByteString -> ByteString |
