git.iko.soy/typeable/pandoc
on master help sign in
Added `--verbose` flag for debugging output in PDF production.

Closes #1840. Closes #1653.

commite3422dc43836166af713d8e487aea6868bafeb71 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 26 Dec 2014, 11:19 (12 years ago) parentc30c96b
4 files changed+42−10
README+4 pandoc.hs+10−1 src/Text/Pandoc/Options.hs+2 src/Text/Pandoc/PDF.hs+26−9
README+4
@@ -230,6 +230,10 @@ General options
230230 `epub.css`, `templates`, `slidy`, `slideous`, or `s5` directory
231231 placed in this directory will override pandoc's normal defaults.
232232
233+`--verbose`
234+: Give verbose debugging output. Currently this only has an effect
235+ with PDF output.
236+
233237`-v`, `--version`
234238: Print version.
235239
pandoc.hs+10−1
@@ -186,6 +186,7 @@ data Opt = Opt
186186 , optTOCDepth :: Int -- ^ Number of levels to include in TOC
187187 , optDumpArgs :: Bool -- ^ Output command-line arguments
188188 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
189+ , optVerbose :: Bool -- ^ Verbose diagnostic output
189190 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
190191 , optWrapText :: Bool -- ^ Wrap text
191192 , optColumns :: Int -- ^ Line length in characters
@@ -246,6 +247,7 @@ defaultOpts = Opt
246247 , optTOCDepth = 3
247248 , optDumpArgs = False
248249 , optIgnoreArgs = False
250+ , optVerbose = False
249251 , optReferenceLinks = False
250252 , optWrapText = True
251253 , optColumns = 72
@@ -858,6 +860,11 @@ options =
858860 (\opt -> return opt { optIgnoreArgs = True }))
859861 "" -- "Ignore command-line arguments."
860862
863+ , Option "" ["verbose"]
864+ (NoArg
865+ (\opt -> return opt { optVerbose = True }))
866+ "" -- "Verbose diagnostic output."
867+
861868 , Option "v" ["version"]
862869 (NoArg
863870 (\_ -> do
@@ -1061,6 +1068,7 @@ main = do
10611068 , optTOCDepth = epubTOCDepth
10621069 , optDumpArgs = dumpArgs
10631070 , optIgnoreArgs = ignoreArgs
1071+ , optVerbose = verbose
10641072 , optReferenceLinks = referenceLinks
10651073 , optWrapText = wrap
10661074 , optColumns = columns
@@ -1302,7 +1310,8 @@ main = do
13021310 writerTOCDepth = epubTOCDepth,
13031311 writerReferenceODT = referenceODT,
13041312 writerReferenceDocx = referenceDocx,
1305− writerMediaBag = media
1313+ writerMediaBag = media,
1314+ writerVerbose = verbose
13061315 }
13071316
13081317
src/Text/Pandoc/Options.hs+2
@@ -323,6 +323,7 @@ data WriterOptions = WriterOptions
323323 , writerReferenceODT :: Maybe FilePath -- ^ Path to reference ODT if specified
324324 , writerReferenceDocx :: Maybe FilePath -- ^ Path to reference DOCX if specified
325325 , writerMediaBag :: MediaBag -- ^ Media collected by docx or epub reader
326+ , writerVerbose :: Bool -- ^ Verbose debugging output
326327 } deriving Show
327328
328329instance Default WriterOptions where
@@ -366,6 +367,7 @@ instance Default WriterOptions where
366367 , writerReferenceODT = Nothing
367368 , writerReferenceDocx = Nothing
368369 , writerMediaBag = mempty
370+ , writerVerbose = False
369371 }
370372
371373-- | Returns True if the given extension is enabled.
src/Text/Pandoc/PDF.hs+26−9
@@ -36,10 +36,11 @@ import qualified Data.ByteString.Lazy.Char8 as BC
3636import qualified Data.ByteString as BS
3737import System.Exit (ExitCode (..))
3838import System.FilePath
39+import System.IO (stderr, stdout)
3940import System.Directory
4041import Data.Digest.Pure.SHA (showDigest, sha1)
4142import System.Environment
42−import Control.Monad (unless, (<=<))
43+import Control.Monad (unless, when, (<=<))
4344import qualified Control.Exception as E
4445import Control.Applicative ((<$))
4546import Data.List (isInfixOf)
@@ -70,7 +71,7 @@ makePDF :: String -- ^ pdf creator (pdflatex, lualatex, xelatex)
7071makePDF program writer opts doc = withTempDir "tex2pdf." $ \tmpdir -> do
7172 doc' <- handleImages opts tmpdir doc
7273 let source = writer opts doc'
73− tex2pdf' tmpdir program source
74+ tex2pdf' (writerVerbose opts) tmpdir program source
7475
7576handleImages :: WriterOptions
7677 -> FilePath -- ^ temp dir to store images
@@ -130,15 +131,16 @@ convertImage tmpdir fname =
130131 mime = getMimeType fname
131132 doNothing = return (Right fname)
132133
133−tex2pdf' :: FilePath -- ^ temp directory for output
134+tex2pdf' :: Bool -- ^ Verbose output
135+ -> FilePath -- ^ temp directory for output
134136 -> String -- ^ tex program
135137 -> String -- ^ tex source
136138 -> IO (Either ByteString ByteString)
137−tex2pdf' tmpDir program source = do
139+tex2pdf' verbose tmpDir program source = do
138140 let numruns = if "\\tableofcontents" `isInfixOf` source
139141 then 3 -- to get page numbers
140142 else 2 -- 1 run won't give you PDF bookmarks
141− (exit, log', mbPdf) <- runTeXProgram program numruns tmpDir source
143+ (exit, log', mbPdf) <- runTeXProgram verbose program 1 numruns tmpDir source
142144 case (exit, mbPdf) of
143145 (ExitFailure _, _) -> do
144146 let logmsg = extractMsg log'
@@ -170,9 +172,9 @@ extractMsg log' = do
170172-- Run a TeX program on an input bytestring and return (exit code,
171173-- contents of stdout, contents of produced PDF if any). Rerun
172174-- a fixed number of times to resolve references.
173−runTeXProgram :: String -> Int -> FilePath -> String
175+runTeXProgram :: Bool -> String -> Int -> Int -> FilePath -> String
174176 -> IO (ExitCode, ByteString, Maybe ByteString)
175−runTeXProgram program runsLeft tmpDir source = do
177+runTeXProgram verbose program runNumber numRuns tmpDir source = do
176178 let file = tmpDir </> "input.tex"
177179 exists <- doesFileExist file
178180 unless exists $ UTF8.writeFile file source
@@ -192,9 +194,24 @@ runTeXProgram program runsLeft tmpDir source = do
192194 $ lookup "TEXINPUTS" env'
193195 let env'' = ("TEXINPUTS", texinputs) :
194196 [(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
197+ when (verbose && runNumber == 1) $ do
198+ putStrLn $ "[makePDF] Command line:"
199+ putStrLn $ program ++ " " ++ unwords (map show programArgs)
200+ putStr "\n"
201+ putStrLn $ "[makePDF] Environment:"
202+ mapM_ print env''
203+ putStr "\n"
204+ putStrLn $ "[makePDF] Contents of " ++ file' ++ ":"
205+ B.readFile file' >>= B.putStr
206+ putStr "\n"
195207 (exit, out, err) <- pipeProcess (Just env'') program programArgs BL.empty
196− if runsLeft > 1
197− then runTeXProgram program (runsLeft - 1) tmpDir source
208+ when verbose $ do
209+ putStrLn $ "[makePDF] Run #" ++ show runNumber
210+ B.hPutStr stdout out
211+ B.hPutStr stderr err
212+ putStr "\n"
213+ if runNumber <= numRuns
214+ then runTeXProgram verbose program (runNumber + 1) numRuns tmpDir source
198215 else do
199216 let pdfFile = replaceDirectory (replaceExtension file ".pdf") tmpDir
200217 pdfExists <- doesFileExist pdfFile