git.iko.soy/typeable/pandoc
on master help sign in
Changes to verbosity in writer and reader options.

API changes: Text.Pandoc.Options: * Added Verbosity. * Added writerVerbosity. * Added readerVerbosity. * Removed writerVerbose. * Removed readerTrace. pandoc CLI: The `--trace` option sets verbosity to DEBUG; the `--quiet` option sets it to ERROR, and the `--verbose` option sets it to INFO. The default is WARNING.

commit8280d6a48958ef305e3dd29e2bb189fb1ea96b14 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 22 Jan 2017, 12:19 (10 years ago) parentd1efc83
10 files changed+52−52
pandoc.hs+11−17 src/Text/Pandoc/Options.hs+9−4 src/Text/Pandoc/PDF.hs+22−21 src/Text/Pandoc/Readers/EPUB.hs+2−2 src/Text/Pandoc/Readers/HTML.hs+3−3 src/Text/Pandoc/Readers/Haddock.hs+1−1 src/Text/Pandoc/Readers/Markdown.hs+1−1 src/Text/Pandoc/Readers/MediaWiki.hs+1−1 src/Text/Pandoc/Readers/TWiki.hs+1−1 src/Text/Pandoc/Readers/Textile.hs+1−1
pandoc.hs+11−17
@@ -125,8 +125,7 @@ convertWithOpts opts args = do
125125 , optTOCDepth = epubTOCDepth
126126 , optDumpArgs = dumpArgs
127127 , optIgnoreArgs = ignoreArgs
128− , optVerbose = verbose
129− , optQuiet = quiet
128+ , optVerbosity = verbosity
130129 , optFailIfWarnings = failIfWarnings
131130 , optReferenceLinks = referenceLinks
132131 , optReferenceLocation = referenceLocation
@@ -147,7 +146,6 @@ convertWithOpts opts args = do
147146 , optAscii = ascii
148147 , optDefaultImageExtension = defaultImageExtension
149148 , optExtractMedia = mbExtractMedia
150− , optTrace = trace
151149 , optTrackChanges = trackChanges
152150 , optFileScope = fileScope
153151 , optKaTeXStylesheet = katexStylesheet
@@ -294,7 +292,7 @@ convertWithOpts opts args = do
294292 , readerIndentedCodeClasses = codeBlockClasses
295293 , readerApplyMacros = not laTeXOutput
296294 , readerDefaultImageExtension = defaultImageExtension
297− , readerTrace = trace
295+ , readerVerbosity = verbosity
298296 , readerTrackChanges = trackChanges
299297 }
300298
@@ -331,7 +329,7 @@ convertWithOpts opts args = do
331329 writerEpubChapterLevel = epubChapterLevel,
332330 writerTOCDepth = epubTOCDepth,
333331 writerReferenceDoc = referenceDoc,
334− writerVerbose = verbose,
332+ writerVerbosity = verbosity,
335333 writerLaTeXArgs = latexEngineArgs
336334 }
337335
@@ -361,8 +359,8 @@ convertWithOpts opts args = do
361359 ws <- getWarnings
362360 return (x, ws)
363361 when (not (null warnings)) $ do
364− unless quiet $
365− mapM_ warn warnings
362+ -- TODO make these streaming
363+ when (verbosity >= WARNING) $ mapM_ warn warnings
366364 when failIfWarnings $
367365 err 3 "Failing because there were warnings."
368366 return res
@@ -541,8 +539,7 @@ data Opt = Opt
541539 , optTOCDepth :: Int -- ^ Number of levels to include in TOC
542540 , optDumpArgs :: Bool -- ^ Output command-line arguments
543541 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
544− , optVerbose :: Bool -- ^ Verbose diagnostic output
545− , optQuiet :: Bool -- ^ Suppress warnings
542+ , optVerbosity :: Verbosity -- ^ Verbosity of diagnostic output
546543 , optFailIfWarnings :: Bool -- ^ Fail on warnings
547544 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
548545 , optReferenceLocation :: ReferenceLocation -- ^ location for footnotes and link references in markdown output
@@ -563,7 +560,6 @@ data Opt = Opt
563560 , optAscii :: Bool -- ^ Use ascii characters only in html
564561 , optDefaultImageExtension :: String -- ^ Default image extension
565562 , optExtractMedia :: Maybe FilePath -- ^ Path to extract embedded media
566− , optTrace :: Bool -- ^ Print debug information
567563 , optTrackChanges :: TrackChanges -- ^ Accept or reject MS Word track-changes.
568564 , optFileScope :: Bool -- ^ Parse input files before combining
569565 , optKaTeXStylesheet :: Maybe String -- ^ Path to stylesheet for KaTeX
@@ -603,8 +599,7 @@ defaultOpts = Opt
603599 , optTOCDepth = 3
604600 , optDumpArgs = False
605601 , optIgnoreArgs = False
606− , optVerbose = False
607− , optQuiet = False
602+ , optVerbosity = WARNING
608603 , optFailIfWarnings = False
609604 , optReferenceLinks = False
610605 , optReferenceLocation = EndOfDocument
@@ -625,9 +620,8 @@ defaultOpts = Opt
625620 , optAscii = False
626621 , optDefaultImageExtension = ""
627622 , optExtractMedia = Nothing
628− , optTrace = False
629623 , optTrackChanges = AcceptChanges
630− , optFileScope = False
624+ , optFileScope = False
631625 , optKaTeXStylesheet = Nothing
632626 , optKaTeXJS = Nothing
633627 }
@@ -1186,7 +1180,7 @@ options =
11861180
11871181 , Option "" ["trace"]
11881182 (NoArg
1189− (\opt -> return opt { optTrace = True }))
1183+ (\opt -> return opt { optVerbosity = DEBUG }))
11901184 "" -- "Turn on diagnostic tracing in readers."
11911185
11921186 , Option "" ["dump-args"]
@@ -1201,12 +1195,12 @@ options =
12011195
12021196 , Option "" ["verbose"]
12031197 (NoArg
1204− (\opt -> return opt { optVerbose = True }))
1198+ (\opt -> return opt { optVerbosity = INFO }))
12051199 "" -- "Verbose diagnostic output."
12061200
12071201 , Option "" ["quiet"]
12081202 (NoArg
1209− (\opt -> return opt { optQuiet = True }))
1203+ (\opt -> return opt { optVerbosity = ERROR }))
12101204 "" -- "Suppress warnings."
12111205
12121206 , Option "" ["fail-if-warnings"]
src/Text/Pandoc/Options.hs+9−4
@@ -37,6 +37,7 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
3737 , HTMLSlideVariant (..)
3838 , EPUBVersion (..)
3939 , WrapOption (..)
40+ , Verbosity (..)
4041 , TopLevelDivision (..)
4142 , WriterOptions (..)
4243 , TrackChanges (..)
@@ -61,7 +62,7 @@ data ReaderOptions = ReaderOptions{
6162 , readerIndentedCodeClasses :: [String] -- ^ Default classes for
6263 -- indented code blocks
6364 , readerDefaultImageExtension :: String -- ^ Default extension for images
64− , readerTrace :: Bool -- ^ Print debugging info
65+ , readerVerbosity :: Verbosity -- ^ Verbosity level
6566 , readerTrackChanges :: TrackChanges
6667} deriving (Show, Read, Data, Typeable, Generic)
6768
@@ -75,7 +76,7 @@ instance Default ReaderOptions
7576 , readerApplyMacros = True
7677 , readerIndentedCodeClasses = []
7778 , readerDefaultImageExtension = ""
78− , readerTrace = False
79+ , readerVerbosity = ERROR
7980 , readerTrackChanges = AcceptChanges
8081 }
8182
@@ -141,6 +142,10 @@ data ReferenceLocation = EndOfBlock -- ^ End of block
141142 | EndOfDocument -- ^ at end of document
142143 deriving (Show, Read, Eq, Data, Typeable, Generic)
143144
145+-- | Verbosity level.
146+data Verbosity = ERROR | WARNING | INFO | DEBUG
147+ deriving (Show, Read, Eq, Data, Enum, Ord, Bounded, Typeable, Generic)
148+
144149-- | Options for writers
145150data WriterOptions = WriterOptions
146151 { writerTemplate :: Maybe String -- ^ Template to use
@@ -181,7 +186,7 @@ data WriterOptions = WriterOptions
181186 , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
182187 , writerTOCDepth :: Int -- ^ Number of levels to include in TOC
183188 , writerReferenceDoc :: Maybe FilePath -- ^ Path to reference document if specified
184− , writerVerbose :: Bool -- ^ Verbose debugging output
189+ , writerVerbosity :: Verbosity -- ^ Verbose debugging output
185190 , writerLaTeXArgs :: [String] -- ^ Flags to pass to latex-engine
186191 , writerReferenceLocation :: ReferenceLocation -- ^ Location of footnotes and references for writing markdown
187192 } deriving (Show, Data, Typeable, Generic)
@@ -223,7 +228,7 @@ instance Default WriterOptions where
223228 , writerEpubChapterLevel = 1
224229 , writerTOCDepth = 3
225230 , writerReferenceDoc = Nothing
226− , writerVerbose = False
231+ , writerVerbosity = WARNING
227232 , writerLaTeXArgs = []
228233 , writerReferenceLocation = EndOfDocument
229234 }
src/Text/Pandoc/PDF.hs+22−21
@@ -52,7 +52,8 @@ import Text.Pandoc.MediaBag
5252import Text.Pandoc.Walk (walkM)
5353import Text.Pandoc.Shared (warn, withTempDir, inDirectory, stringify)
5454import Text.Pandoc.Writers.Shared (getField, metaToJSON)
55−import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..))
55+import Text.Pandoc.Options (WriterOptions(..), HTMLMathMethod(..),
56+ Verbosity(..))
5657import Text.Pandoc.MIME (extensionFromMimeType, getMimeType)
5758import Text.Pandoc.Process (pipeProcess)
5859import Control.Monad.Trans (MonadIO(..))
@@ -98,16 +99,16 @@ makePDF "wkhtmltopdf" writer opts _mediabag doc@(Pandoc meta _) = liftIO $ do
9899 (getField "margin-left" meta'))
99100 ]
100101 source <- runIOorExplode $ writer opts doc
101− html2pdf (writerVerbose opts) args source
102+ html2pdf (writerVerbosity opts) args source
102103makePDF program writer opts mediabag doc =
103104 liftIO $ withTempDir "tex2pdf." $ \tmpdir -> do
104105 doc' <- handleImages opts mediabag tmpdir doc
105106 source <- runIOorExplode $ writer opts doc'
106107 let args = writerLaTeXArgs opts
107108 case takeBaseName program of
108− "context" -> context2pdf (writerVerbose opts) tmpdir source
109+ "context" -> context2pdf (writerVerbosity opts) tmpdir source
109110 prog | prog `elem` ["pdflatex", "lualatex", "xelatex"]
110− -> tex2pdf' (writerVerbose opts) args tmpdir program source
111+ -> tex2pdf' (writerVerbosity opts) args tmpdir program source
111112 _ -> return $ Left $ UTF8.fromStringLazy $ "Unknown program " ++ program
112113
113114handleImages :: WriterOptions
@@ -174,17 +175,17 @@ convertImage tmpdir fname =
174175 mime = getMimeType fname
175176 doNothing = return (Right fname)
176177
177−tex2pdf' :: Bool -- ^ Verbose output
178+tex2pdf' :: Verbosity -- ^ Verbosity level
178179 -> [String] -- ^ Arguments to the latex-engine
179180 -> FilePath -- ^ temp directory for output
180181 -> String -- ^ tex program
181182 -> String -- ^ tex source
182183 -> IO (Either ByteString ByteString)
183−tex2pdf' verbose args tmpDir program source = do
184+tex2pdf' verbosity args tmpDir program source = do
184185 let numruns = if "\\tableofcontents" `isInfixOf` source
185186 then 3 -- to get page numbers
186187 else 2 -- 1 run won't give you PDF bookmarks
187− (exit, log', mbPdf) <- runTeXProgram verbose program args 1 numruns tmpDir source
188+ (exit, log', mbPdf) <- runTeXProgram verbosity program args 1 numruns tmpDir source
188189 case (exit, mbPdf) of
189190 (ExitFailure _, _) -> do
190191 let logmsg = extractMsg log'
@@ -222,9 +223,9 @@ extractConTeXtMsg log' = do
222223-- Run a TeX program on an input bytestring and return (exit code,
223224-- contents of stdout, contents of produced PDF if any). Rerun
224225-- a fixed number of times to resolve references.
225−runTeXProgram :: Bool -> String -> [String] -> Int -> Int -> FilePath -> String
226− -> IO (ExitCode, ByteString, Maybe ByteString)
227−runTeXProgram verbose program args runNumber numRuns tmpDir source = do
226+runTeXProgram :: Verbosity -> String -> [String] -> Int -> Int -> FilePath
227+ -> String -> IO (ExitCode, ByteString, Maybe ByteString)
228+runTeXProgram verbosity program args runNumber numRuns tmpDir source = do
228229 let file = tmpDir </> "input.tex"
229230 exists <- doesFileExist file
230231 unless exists $ UTF8.writeFile file source
@@ -244,7 +245,7 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
244245 $ lookup "TEXINPUTS" env'
245246 let env'' = ("TEXINPUTS", texinputs) :
246247 [(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
247− when (verbose && runNumber == 1) $ do
248+ when (verbosity >= INFO && runNumber == 1) $ do
248249 putStrLn "[makePDF] temp dir:"
249250 putStrLn tmpDir'
250251 putStrLn "[makePDF] Command line:"
@@ -257,12 +258,12 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
257258 B.readFile file' >>= B.putStr
258259 putStr "\n"
259260 (exit, out) <- pipeProcess (Just env'') program programArgs BL.empty
260− when verbose $ do
261+ when (verbosity >= INFO) $ do
261262 putStrLn $ "[makePDF] Run #" ++ show runNumber
262263 B.hPutStr stdout out
263264 putStr "\n"
264265 if runNumber <= numRuns
265− then runTeXProgram verbose program args (runNumber + 1) numRuns tmpDir source
266+ then runTeXProgram verbosity program args (runNumber + 1) numRuns tmpDir source
266267 else do
267268 let pdfFile = replaceDirectory (replaceExtension file ".pdf") tmpDir
268269 pdfExists <- doesFileExist pdfFile
@@ -274,17 +275,17 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
274275 else return Nothing
275276 return (exit, out, pdf)
276277
277−html2pdf :: Bool -- ^ Verbose output
278+html2pdf :: Verbosity -- ^ Verbosity level
278279 -> [String] -- ^ Args to wkhtmltopdf
279280 -> String -- ^ HTML5 source
280281 -> IO (Either ByteString ByteString)
281−html2pdf verbose args source = do
282+html2pdf verbosity args source = do
282283 file <- withTempFile "." "html2pdf.html" $ \fp _ -> return fp
283284 pdfFile <- withTempFile "." "html2pdf.pdf" $ \fp _ -> return fp
284285 UTF8.writeFile file source
285286 let programArgs = args ++ [file, pdfFile]
286287 env' <- getEnvironment
287− when verbose $ do
288+ when (verbosity >= INFO) $ do
288289 putStrLn "[makePDF] Command line:"
289290 putStrLn $ "wkhtmltopdf" ++ " " ++ unwords (map show programArgs)
290291 putStr "\n"
@@ -296,7 +297,7 @@ html2pdf verbose args source = do
296297 putStr "\n"
297298 (exit, out) <- pipeProcess (Just env') "wkhtmltopdf" programArgs BL.empty
298299 removeFile file
299− when verbose $ do
300+ when (verbosity >= INFO) $ do
300301 B.hPutStr stdout out
301302 putStr "\n"
302303 pdfExists <- doesFileExist pdfFile
@@ -314,11 +315,11 @@ html2pdf verbose args source = do
314315 (ExitSuccess, Nothing) -> Left ""
315316 (ExitSuccess, Just pdf) -> Right pdf
316317
317−context2pdf :: Bool -- ^ Verbose output
318+context2pdf :: Verbosity -- ^ Verbosity level
318319 -> FilePath -- ^ temp directory for output
319320 -> String -- ^ ConTeXt source
320321 -> IO (Either ByteString ByteString)
321−context2pdf verbose tmpDir source = inDirectory tmpDir $ do
322+context2pdf verbosity tmpDir source = inDirectory tmpDir $ do
322323 let file = "input.tex"
323324 UTF8.writeFile file source
324325#ifdef _WINDOWS
@@ -334,7 +335,7 @@ context2pdf verbose tmpDir source = inDirectory tmpDir $ do
334335 $ lookup "TEXINPUTS" env'
335336 let env'' = ("TEXINPUTS", texinputs) :
336337 [(k,v) | (k,v) <- env', k /= "TEXINPUTS"]
337− when verbose $ do
338+ when (verbosity >= INFO) $ do
338339 putStrLn "[makePDF] temp dir:"
339340 putStrLn tmpDir'
340341 putStrLn "[makePDF] Command line:"
@@ -347,7 +348,7 @@ context2pdf verbose tmpDir source = inDirectory tmpDir $ do
347348 B.readFile file >>= B.putStr
348349 putStr "\n"
349350 (exit, out) <- pipeProcess (Just env'') "context" programArgs BL.empty
350− when verbose $ do
351+ when (verbosity >= INFO) $ do
351352 B.hPutStr stdout out
352353 putStr "\n"
353354 let pdfFile = replaceExtension file ".pdf"
src/Text/Pandoc/Readers/EPUB.hs+2−2
@@ -12,7 +12,7 @@ import Text.XML.Light
1212import Text.Pandoc.Definition hiding (Attr)
1313import Text.Pandoc.Readers.HTML (readHtml)
1414import Text.Pandoc.Walk (walk, query)
15−import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
15+import Text.Pandoc.Options ( ReaderOptions(..), readerVerbosity, Verbosity(..))
1616import Text.Pandoc.Shared (escapeURI, collapseFilePath, addMetaField)
1717import Network.URI (unEscapeString)
1818import Text.Pandoc.MediaBag (MediaBag, insertMedia)
@@ -71,7 +71,7 @@ archiveToEPUB os archive = do
7171 os' = os {readerParseRaw = True}
7272 parseSpineElem :: PandocMonad m => FilePath -> (FilePath, MimeType) -> m Pandoc
7373 parseSpineElem (normalise -> r) (normalise -> path, mime) = do
74− when (readerTrace os) (traceM path)
74+ when (readerVerbosity os == DEBUG) (traceM path)
7575 doc <- mimeToReader mime r path
7676 let docSpan = B.doc $ B.para $ B.spanWith (takeFileName path, [], []) mempty
7777 return $ docSpan <> doc
src/Text/Pandoc/Readers/HTML.hs+3−3
@@ -45,8 +45,8 @@ import qualified Text.Pandoc.Builder as B
4545import Text.Pandoc.Builder (Blocks, Inlines, trimInlines, HasMeta(..))
4646import Text.Pandoc.Shared ( extractSpaces, renderTags', addMetaField
4747 , escapeURI, safeRead )
48−import Text.Pandoc.Options (ReaderOptions(readerParseRaw, readerTrace)
49− , Extension (Ext_epub_html_exts,
48+import Text.Pandoc.Options (ReaderOptions(readerParseRaw, readerVerbosity),
49+ Verbosity(..), Extension (Ext_epub_html_exts,
5050 Ext_native_divs, Ext_native_spans))
5151import Text.Pandoc.Parsing hiding ((<|>))
5252import Text.Pandoc.Walk
@@ -160,7 +160,7 @@ pHead = pInTags "head" $ pTitle <|> pMetaTag <|> pBaseTag <|> (mempty <$ pAnyTag
160160
161161block :: PandocMonad m => TagParser m Blocks
162162block = do
163− tr <- getOption readerTrace
163+ tr <- (== DEBUG) <$> getOption readerVerbosity
164164 pos <- getPosition
165165 res <- choice
166166 [ eSection
src/Text/Pandoc/Readers/Haddock.hs+1−1
@@ -48,7 +48,7 @@ readHaddockEither opts =
4848#else
4949 Right . B.doc . docHToBlocks . trace' . parseParas
5050#endif
51− where trace' x = if readerTrace opts
51+ where trace' x = if readerVerbosity opts == DEBUG
5252 then trace (show x) x
5353 else x
5454
src/Text/Pandoc/Readers/Markdown.hs+1−1
@@ -490,7 +490,7 @@ parseBlocks = mconcat <$> manyTill block eof
490490
491491block :: PandocMonad m => MarkdownParser m (F Blocks)
492492block = do
493− tr <- getOption readerTrace
493+ tr <- (== DEBUG) <$> getOption readerVerbosity
494494 pos <- getPosition
495495 res <- choice [ mempty <$ blanklines
496496 , codeBlockFenced
src/Text/Pandoc/Readers/MediaWiki.hs+1−1
@@ -194,7 +194,7 @@ parseMediaWiki = do
194194
195195block :: PandocMonad m => MWParser m Blocks
196196block = do
197− tr <- getOption readerTrace
197+ tr <- (== DEBUG) <$> getOption readerVerbosity
198198 pos <- getPosition
199199 res <- mempty <$ skipMany1 blankline
200200 <|> table
src/Text/Pandoc/Readers/TWiki.hs+1−1
@@ -127,7 +127,7 @@ parseTWiki = do
127127
128128block :: TWParser B.Blocks
129129block = do
130− tr <- getOption readerTrace
130+ tr <- (== DEBUG) <$> getOption readerVerbosity
131131 pos <- getPosition
132132 res <- mempty <$ skipMany1 blankline
133133 <|> blockElements
src/Text/Pandoc/Readers/Textile.hs+1−1
@@ -147,7 +147,7 @@ block :: PandocMonad m => ParserT [Char] ParserState m Blocks
147147block = do
148148 res <- choice blockParsers <?> "block"
149149 pos <- getPosition
150− tr <- getOption readerTrace
150+ tr <- (== DEBUG) <$> getOption readerVerbosity
151151 when tr $
152152 trace (printf "line %d: %s" (sourceLine pos)
153153 (take 60 $ show $ B.toList res)) (return ())