git.iko.soy/typeable/pandoc
on master help sign in
Allow binary formats to be written to stdout unless tty output.

Only works on posix. On Windows, pandoc works as before and requires an output file parameter for binary formats. Closes #2677.

commit738806112bc0ee1711c6f170361d382c7d4265e8 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 23 Oct 2016, 22:16 (10 years ago) parent5ec7331
2 files changed+18−5
pandoc.cabal+3 pandoc.hs+15−5
pandoc.cabal+3
@@ -442,6 +442,9 @@ Executable pandoc
442442 Ghc-Prof-Options: -fprof-auto-exported -rtsopts -with-rtsopts=-K16m
443443 if os(windows)
444444 Cpp-options: -D_WINDOWS
445+ else
446+ Build-Depends: unix >= 2.4 && < 2.8
447+
445448 Default-Language: Haskell98
446449 Other-Extensions: PatternGuards, OverloadedStrings,
447450 ScopedTypeVariables, GeneralizedNewtypeDeriving,
pandoc.hs+15−5
@@ -74,6 +74,10 @@ import Text.Pandoc.Readers.Txt2Tags (getT2TMeta)
7474import Paths_pandoc (getDataDir)
7575import Text.Printf (printf)
7676import Text.Pandoc.Error
77+#ifndef _WINDOWS
78+import System.Posix.Terminal (queryTerminal)
79+import System.Posix.IO (stdOutput)
80+#endif
7781
7882type Transform = Pandoc -> Pandoc
7983
@@ -1357,7 +1361,12 @@ convertWithOpts opts args = do
13571361 , readerFileScope = fileScope
13581362 }
13591363
1360− when (not (isTextFormat format) && outputFile == "-") $
1364+#ifdef _WINDOWS
1365+ let istty = True
1366+#else
1367+ istty <- queryTerminal stdOutput
1368+#endif
1369+ when (istty && not (isTextFormat format) && outputFile == "-") $
13611370 err 5 $ "Cannot write " ++ format ++ " output to stdout.\n" ++
13621371 "Specify an output file using the -o option."
13631372
@@ -1457,8 +1466,9 @@ convertWithOpts opts args = do
14571466 applyTransforms transforms >=>
14581467 applyFilters datadir filters' [format]) doc
14591468
1460− let writeBinary :: B.ByteString -> IO ()
1461− writeBinary = B.writeFile (UTF8.encodePath outputFile)
1469+ let writeFnBinary :: FilePath -> B.ByteString -> IO ()
1470+ writeFnBinary "-" = B.putStr
1471+ writeFnBinary f = B.writeFile (UTF8.encodePath f)
14621472
14631473 let writerFn :: FilePath -> String -> IO ()
14641474 writerFn "-" = UTF8.putStr
@@ -1466,7 +1476,7 @@ convertWithOpts opts args = do
14661476
14671477 case writer of
14681478 IOStringWriter f -> f writerOptions doc' >>= writerFn outputFile
1469− IOByteStringWriter f -> f writerOptions doc' >>= writeBinary
1479+ IOByteStringWriter f -> f writerOptions doc' >>= writeFnBinary outputFile
14701480 PureStringWriter f
14711481 | pdfOutput -> do
14721482 -- make sure writer is latex or beamer or context or html5
@@ -1486,7 +1496,7 @@ convertWithOpts opts args = do
14861496
14871497 res <- makePDF pdfprog f writerOptions doc'
14881498 case res of
1489− Right pdf -> writeBinary pdf
1499+ Right pdf -> writeFnBinary outputFile pdf
14901500 Left err' -> do
14911501 B.hPutStr stderr err'
14921502 B.hPut stderr $ B.pack [10]