summaryrefslogtreecommitdiff
path: root/pandoc.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2016-10-23 22:16:00 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2016-10-23 22:16:00 +0200
commit738806112bc0ee1711c6f170361d382c7d4265e8 (patch)
tree26fdc9916d3908c36504487bf94fe55f5bcd91fe /pandoc.hs
parent5ec7331349f14238734433448afcbcf7c804494b (diff)
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.
Diffstat (limited to 'pandoc.hs')
-rw-r--r--pandoc.hs20
1 files changed, 15 insertions, 5 deletions
diff --git a/pandoc.hs b/pandoc.hs
index 4f87947e7..541d17ef3 100644
--- a/pandoc.hs
+++ b/pandoc.hs
@@ -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]