git.iko.soy/typeable/pandoc
on master help sign in
Refactored error reporting in pandoc.hs.
commit4d98815edd3438be89ebe7276c55e92363d2181e authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 20 Jan 2012, 22:12 (15 years ago) parentd7b67f4
1 file changed+49−40
src/pandoc.hs+49−40
src/pandoc.hs+49−40
@@ -42,7 +42,7 @@ import System.FilePath
4242import System.Console.GetOpt
4343import Data.Char ( toLower )
4444import Data.List ( intercalate, isSuffixOf, isPrefixOf )
45−import System.Directory ( getAppUserDataDirectory, doesFileExist )
45+import System.Directory ( getAppUserDataDirectory, doesFileExist, findExecutable )
4646import System.IO ( stdout, stderr )
4747import System.IO.Error ( isDoesNotExistError )
4848import Control.Exception.Extensible ( throwIO )
@@ -217,10 +217,8 @@ options =
217217 (\arg opt ->
218218 case reads arg of
219219 [(t,"")] | t > 0 -> return opt { optTabStop = t }
220− _ -> do
221− UTF8.hPutStrLn stderr $
222− "tab-stop must be a number greater than 0"
223− exitWith $ ExitFailure 31)
220+ _ -> err 31
221+ "tab-stop must be a number greater than 0")
224222 "NUMBER")
225223 "" -- "Tab stop (default 4)"
226224
@@ -259,7 +257,7 @@ options =
259257 , Option "5" ["html5"]
260258 (NoArg
261259 (\opt -> do
262− UTF8.hPutStrLn stderr $ "pandoc: --html5 is deprecated. "
260+ warn $ "pandoc: --html5 is deprecated. "
263261 ++ "Use the html5 output format instead."
264262 return opt { optHtml5 = True }))
265263 "" -- "Produce HTML5 in HTML output"
@@ -279,9 +277,8 @@ options =
279277 "kate" -> return kate
280278 "monochrome" -> return monochrome
281279 "haddock" -> return haddock
282− _ -> UTF8.hPutStrLn stderr
283− ("Unknown style: " ++ arg) >>
284− exitWith (ExitFailure 39)
280+ _ -> err 39 $
281+ "Unknown style :" ++ arg
285282 return opt{ optHighlightStyle = newStyle })
286283 "STYLE")
287284 "" -- "Style for highlighted code"
@@ -364,7 +361,7 @@ options =
364361 , Option "" ["xetex"]
365362 (NoArg
366363 (\opt -> do
367− UTF8.hPutStrLn stderr $ "pandoc: --xetex is deprecated. "
364+ warn $ "pandoc: --xetex is deprecated. "
368365 ++ "It is no longer needed for use with XeTeX."
369366 return opt { optXeTeX = True }))
370367 "" -- "Format latex for processing by XeTeX"
@@ -399,10 +396,8 @@ options =
399396 (\arg opt ->
400397 case reads arg of
401398 [(t,"")] | t > 0 -> return opt { optColumns = t }
402− _ -> do
403− UTF8.hPutStrLn stderr $
404− "columns must be a number greater than 0"
405− exitWith $ ExitFailure 33)
399+ _ -> err 33 $
400+ "columns must be a number greater than 0")
406401 "NUMBER")
407402 "" -- "Length of line in characters"
408403
@@ -413,8 +408,8 @@ options =
413408 "references" -> return ReferenceObfuscation
414409 "javascript" -> return JavascriptObfuscation
415410 "none" -> return NoObfuscation
416− _ -> UTF8.hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
417− exitWith (ExitFailure 6)
411+ _ -> err 6
412+ ("Error: Unknown obfuscation method: " ++ arg)
418413 return opt { optEmailObfuscation = method })
419414 "none|javascript|references")
420415 "" -- "Method for obfuscating email in HTML"
@@ -446,10 +441,8 @@ options =
446441 let shift = t - 1
447442 return opt{ optTransforms =
448443 headerShift shift : oldTransforms }
449− _ -> do
450− UTF8.hPutStrLn stderr $
451− "base-header-level must be a number > 0"
452− exitWith $ ExitFailure 19)
444+ _ -> err 19
445+ "base-header-level must be a number > 0")
453446 "NUMBER")
454447 "" -- "Headers base level"
455448
@@ -468,9 +461,8 @@ options =
468461 (k,_:v) -> do
469462 let newvars = optVariables opt ++ [(k,v)]
470463 return opt{ optVariables = newvars }
471− _ -> do
472− UTF8.hPutStrLn stderr $ "Could not parse `" ++ arg ++ "' as a key/value pair (k=v or k:v)"
473− exitWith $ ExitFailure 17)
464+ _ -> err 17 $
465+ "Could not parse `" ++ arg ++ "' as a key/value pair (k=v or k:v)")
474466 "KEY:VALUE")
475467 "" -- "Use custom template"
476468
@@ -713,6 +705,19 @@ defaultWriterName x =
713705 ['.',y] | y `elem` ['1'..'9'] -> "man"
714706 _ -> "html"
715707
708+
709+err :: Int -> String -> IO a
710+err exitCode msg = do
711+ name <- getProgName
712+ UTF8.hPutStrLn stderr $ name ++ ": " ++ msg
713+ exitWith $ ExitFailure exitCode
714+ return undefined
715+
716+warn :: String -> IO ()
717+warn msg = do
718+ name <- getProgName
719+ UTF8.hPutStrLn stderr $ name ++ ": " ++ msg
720+
716721main :: IO ()
717722main = do
718723
@@ -725,10 +730,8 @@ main = do
725730 else getOpt Permute options rawArgs
726731
727732 unless (null errors) $
728− do name <- getProgName
729− mapM_ (\e -> UTF8.hPutStr stderr (name ++ ": ") >> UTF8.hPutStr stderr e) errors
730− UTF8.hPutStrLn stderr $ "Try " ++ name ++ " --help for more information."
731− exitWith $ ExitFailure 2
733+ err 2 $ concat $ errors ++
734+ ["Try " ++ prg ++ " --help for more information."]
732735
733736 let defaultOpts' = if compatMode
734737 then defaultOpts { optReader = "markdown"
@@ -809,7 +812,7 @@ main = do
809812
810813 reader <- case (lookup readerName' readers) of
811814 Just r -> return r
812− Nothing -> error ("Unknown reader: " ++ readerName')
815+ Nothing -> err 7 ("Unknown reader: " ++ readerName')
813816
814817 let standalone' = standalone || (`elem` nonTextFormats) writerName'
815818
@@ -861,10 +864,9 @@ main = do
861864 -- that we can do lookups with regular string equality
862865 let unescapeRefId ref = ref{ refId = deEntity (refId ref) }
863866
864− refs <- mapM (\f -> catch (CSL.readBiblioFile f) $ \e -> do
865− UTF8.hPutStrLn stderr $ "Error reading bibliography `" ++ f ++ "'"
866− UTF8.hPutStrLn stderr $ show e
867− exitWith (ExitFailure 23)) reffiles >>=
867+ refs <- mapM (\f -> catch (CSL.readBiblioFile f) $ \e ->
868+ err 23 $ "Error reading bibliography `" ++ f ++ "'" ++ "\n" ++ show e)
869+ reffiles >>=
868870 return . map unescapeRefId . concat
869871
870872 let sourceDir = if null sources
@@ -923,9 +925,8 @@ main = do
923925 writerHighlightStyle = highlightStyle }
924926
925927 when (writerName' `elem` nonTextFormats&& outputFile == "-") $
926− do UTF8.hPutStrLn stderr ("Error: Cannot write " ++ writerName' ++ " output to stdout.\n" ++
927− "Specify an output file using the -o option.")
928− exitWith $ ExitFailure 5
928+ err 5 $ "Error: Cannot write " ++ writerName' ++ " output to stdout.\n" ++
929+ "Specify an output file using the -o option."
929930
930931 let readSources [] = mapM readSource ["-"]
931932 readSources srcs = mapM readSource srcs
@@ -964,6 +965,8 @@ main = do
964965 processBiblio cslfile' cslabbrevs refs doc1
965966 else return doc1
966967
968+ let latexProgram = "pdflatex"
969+
967970 case lookup writerName' writers of
968971 Nothing | writerName' == "epub" ->
969972 writeEPUB epubStylesheet writerOptions doc2 >>= writeBinary
@@ -972,11 +975,17 @@ main = do
972975 | writerName' == "docx" ->
973976 writeDocx referenceDocx writerOptions doc2 >>= writeBinary
974977 | writerName' == "pdf" ->
975− do res <- tex2pdf "pdflatex" $ writeLaTeX writerOptions doc2
976− case res of
977− Right pdf -> writeBinary pdf
978− Left err' -> B.hPutStr stderr err'
979− | otherwise -> error $ "Unknown writer: " ++ writerName'
978+ do -- first check to make sure we have latex
979+ mbLatex <- findExecutable latexProgram
980+ case mbLatex of
981+ Nothing -> err 41 $
982+ latexProgram ++ " not found" -- TODO improve
983+ Just pgm -> do
984+ res <- tex2pdf latexProgram $ writeLaTeX writerOptions doc2
985+ case res of
986+ Right pdf -> writeBinary pdf
987+ Left err' -> err 43 $ toString err'
988+ | otherwise -> err 9 ("Unknown writer: " ++ writerName')
980989 where writeBinary = B.writeFile (encodeString outputFile)
981990 Just r -> writerFn outputFile =<< postProcess result
982991 where writerFn "-" = UTF8.putStr