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.
This commit is contained in:
John MacFarlane 2016-10-23 22:16:00 +02:00
parent 5ec7331349
commit 738806112b
2 changed files with 18 additions and 5 deletions

View file

@ -442,6 +442,9 @@ Executable pandoc
Ghc-Prof-Options: -fprof-auto-exported -rtsopts -with-rtsopts=-K16m
if os(windows)
Cpp-options: -D_WINDOWS
else
Build-Depends: unix >= 2.4 && < 2.8
Default-Language: Haskell98
Other-Extensions: PatternGuards, OverloadedStrings,
ScopedTypeVariables, GeneralizedNewtypeDeriving,

View file

@ -74,6 +74,10 @@ import Text.Pandoc.Readers.Txt2Tags (getT2TMeta)
import Paths_pandoc (getDataDir)
import Text.Printf (printf)
import Text.Pandoc.Error
#ifndef _WINDOWS
import System.Posix.Terminal (queryTerminal)
import System.Posix.IO (stdOutput)
#endif
type Transform = Pandoc -> Pandoc
@ -1357,7 +1361,12 @@ convertWithOpts opts args = do
, readerFileScope = fileScope
}
when (not (isTextFormat format) && outputFile == "-") $
#ifdef _WINDOWS
let istty = True
#else
istty <- queryTerminal stdOutput
#endif
when (istty && not (isTextFormat format) && outputFile == "-") $
err 5 $ "Cannot write " ++ format ++ " output to stdout.\n" ++
"Specify an output file using the -o option."
@ -1457,8 +1466,9 @@ convertWithOpts opts args = do
applyTransforms transforms >=>
applyFilters datadir filters' [format]) doc
let writeBinary :: B.ByteString -> IO ()
writeBinary = B.writeFile (UTF8.encodePath outputFile)
let writeFnBinary :: FilePath -> B.ByteString -> IO ()
writeFnBinary "-" = B.putStr
writeFnBinary f = B.writeFile (UTF8.encodePath f)
let writerFn :: FilePath -> String -> IO ()
writerFn "-" = UTF8.putStr
@ -1466,7 +1476,7 @@ convertWithOpts opts args = do
case writer of
IOStringWriter f -> f writerOptions doc' >>= writerFn outputFile
IOByteStringWriter f -> f writerOptions doc' >>= writeBinary
IOByteStringWriter f -> f writerOptions doc' >>= writeFnBinary outputFile
PureStringWriter f
| pdfOutput -> do
-- make sure writer is latex or beamer or context or html5
@ -1486,7 +1496,7 @@ convertWithOpts opts args = do
res <- makePDF pdfprog f writerOptions doc'
case res of
Right pdf -> writeBinary pdf
Right pdf -> writeFnBinary outputFile pdf
Left err' -> do
B.hPutStr stderr err'
B.hPut stderr $ B.pack [10]