git.iko.soy/typeable/pandoc
on master help sign in
In producing PDFs, warn if the font is missing some characters.

* Added `MissingCharacter` to `LogMessage` in Text.Pandoc.Logging. * Parse the (xe)latex log for missing character warnings and issue the warning. Closes #3742.

commitabd2e94f5a8c1238eebeef9b6edb91b8031507e7 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 18 Jun 2017, 11:17 (9 years ago) parent6166655
2 files changed+40−13
src/Text/Pandoc/Logging.hs+6 src/Text/Pandoc/PDF.hs+34−13
src/Text/Pandoc/Logging.hs+6
@@ -93,6 +93,7 @@ data LogMessage =
9393 | NoTitleElement String
9494 | NoLangSpecified
9595 | CouldNotHighlight String
96+ | MissingCharacter String
9697 deriving (Show, Eq, Data, Ord, Typeable, Generic)
9798
9899instance ToJSON LogMessage where
@@ -186,6 +187,8 @@ instance ToJSON LogMessage where
186187 NoLangSpecified -> []
187188 CouldNotHighlight msg ->
188189 ["message" .= Text.pack msg]
190+ MissingCharacter msg ->
191+ ["message" .= Text.pack msg]
189192
190193showPos :: SourcePos -> String
191194showPos pos = sn ++ "line " ++
@@ -262,6 +265,8 @@ showLogMessage msg =
262265 "It is recommended that lang be specified for this format."
263266 CouldNotHighlight m ->
264267 "Could not highlight code block:\n" ++ m
268+ MissingCharacter m ->
269+ "Missing character: " ++ m
265270
266271messageVerbosity:: LogMessage -> Verbosity
267272messageVerbosity msg =
@@ -291,3 +296,4 @@ messageVerbosity msg =
291296 NoTitleElement{} -> WARNING
292297 NoLangSpecified -> INFO
293298 CouldNotHighlight{} -> WARNING
299+ MissingCharacter{} -> WARNING
src/Text/Pandoc/PDF.hs+34−13
@@ -40,7 +40,6 @@ import qualified Data.Text as T
4040import Data.Text (Text)
4141import qualified Data.ByteString as BS
4242import Data.ByteString.Lazy (ByteString)
43−import qualified Data.ByteString.Lazy as B
4443import qualified Data.ByteString.Lazy as BL
4544import qualified Data.ByteString.Lazy.Char8 as BC
4645import Data.Maybe (fromMaybe)
@@ -197,7 +196,22 @@ tex2pdf' verbosity args tmpDir program source = do
197196 _ -> ""
198197 return $ Left $ logmsg <> extramsg
199198 (ExitSuccess, Nothing) -> return $ Left ""
200− (ExitSuccess, Just pdf) -> return $ Right pdf
199+ (ExitSuccess, Just pdf) -> do
200+ missingCharacterWarnings verbosity log'
201+ return $ Right pdf
202+
203+missingCharacterWarnings :: Verbosity -> ByteString -> IO ()
204+missingCharacterWarnings verbosity log' = do
205+ let ls = BC.lines log'
206+ let isMissingCharacterWarning = BC.isPrefixOf "Missing character: "
207+ let warnings = [ UTF8.toStringLazy (BC.drop 19 l)
208+ | l <- ls
209+ , isMissingCharacterWarning l
210+ ]
211+ runIO $ do
212+ setVerbosity verbosity
213+ mapM_ (report . MissingCharacter) warnings
214+ return ()
201215
202216-- parsing output
203217
@@ -255,12 +269,12 @@ runTeXProgram verbosity program args runNumber numRuns tmpDir source = do
255269 mapM_ print env''
256270 putStr "\n"
257271 putStrLn $ "[makePDF] Contents of " ++ file' ++ ":"
258− B.readFile file' >>= B.putStr
272+ BL.readFile file' >>= BL.putStr
259273 putStr "\n"
260274 (exit, out) <- pipeProcess (Just env'') program programArgs BL.empty
261275 when (verbosity >= INFO) $ do
262276 putStrLn $ "[makePDF] Run #" ++ show runNumber
263− B.hPutStr stdout out
277+ BL.hPutStr stdout out
264278 putStr "\n"
265279 if runNumber <= numRuns
266280 then runTeXProgram verbosity program args (runNumber + 1) numRuns tmpDir source
@@ -271,9 +285,16 @@ runTeXProgram verbosity program args runNumber numRuns tmpDir source = do
271285 -- We read PDF as a strict bytestring to make sure that the
272286 -- temp directory is removed on Windows.
273287 -- See https://github.com/jgm/pandoc/issues/1192.
274− then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
288+ then (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
275289 else return Nothing
276− return (exit, out, pdf)
290+ -- Note that some things like Missing character warnings
291+ -- appear in the log but not on stderr, so we prefer the log:
292+ let logFile = replaceExtension file ".log"
293+ logExists <- doesFileExist logFile
294+ log' <- if logExists
295+ then BL.readFile logFile
296+ else return out
297+ return (exit, log', pdf)
277298
278299ms2pdf :: Verbosity
279300 -> [String]
@@ -294,7 +315,7 @@ ms2pdf verbosity args source = do
294315 (exit, out) <- pipeProcess (Just env') "pdfroff" args
295316 (BL.fromStrict $ UTF8.fromText source)
296317 when (verbosity >= INFO) $ do
297− B.hPutStr stdout out
318+ BL.hPutStr stdout out
298319 putStr "\n"
299320 return $ case exit of
300321 ExitFailure _ -> Left out
@@ -318,12 +339,12 @@ html2pdf verbosity args source = do
318339 mapM_ print env'
319340 putStr "\n"
320341 putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
321− B.readFile file >>= B.putStr
342+ BL.readFile file >>= BL.putStr
322343 putStr "\n"
323344 (exit, out) <- pipeProcess (Just env') "wkhtmltopdf" programArgs BL.empty
324345 removeFile file
325346 when (verbosity >= INFO) $ do
326− B.hPutStr stdout out
347+ BL.hPutStr stdout out
327348 putStr "\n"
328349 pdfExists <- doesFileExist pdfFile
329350 mbPdf <- if pdfExists
@@ -331,7 +352,7 @@ html2pdf verbosity args source = do
331352 -- temp directory is removed on Windows.
332353 -- See https://github.com/jgm/pandoc/issues/1192.
333354 then do
334− res <- (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
355+ res <- (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
335356 removeFile pdfFile
336357 return res
337358 else return Nothing
@@ -365,11 +386,11 @@ context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
365386 mapM_ print env'
366387 putStr "\n"
367388 putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
368− B.readFile file >>= B.putStr
389+ BL.readFile file >>= BL.putStr
369390 putStr "\n"
370391 (exit, out) <- pipeProcess (Just env') "context" programArgs BL.empty
371392 when (verbosity >= INFO) $ do
372− B.hPutStr stdout out
393+ BL.hPutStr stdout out
373394 putStr "\n"
374395 let pdfFile = replaceExtension file ".pdf"
375396 pdfExists <- doesFileExist pdfFile
@@ -377,7 +398,7 @@ context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
377398 -- We read PDF as a strict bytestring to make sure that the
378399 -- temp directory is removed on Windows.
379400 -- See https://github.com/jgm/pandoc/issues/1192.
380− then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
401+ then (Just . BL.fromChunks . (:[])) `fmap` BS.readFile pdfFile
381402 else return Nothing
382403 case (exit, mbPdf) of
383404 (ExitFailure _, _) -> do