Change behavior with binary format output to stdout.
Previously, for binary formats, output to stdout was disabled unless we could detect that the output was being piped (and not sent to the terminal). Unfortunately, such detection is not possible on Windows, leaving windows users no way to pipe binary output. So we have changed the behavior in the following way: * If the -o option is not used, binary output is never sent to stdout by default; instead, an error is raised. * IF '-o -' is used, binary output is sent to stdout, regardless of whether it is being piped. This works on Windows too.
This commit is contained in:
parent
7a40f4865f
commit
61cf3affa9
2 changed files with 16 additions and 23 deletions
|
@ -110,8 +110,8 @@ If no *input-file* is specified, input is read from *stdin*.
|
||||||
Otherwise, the *input-files* are concatenated (with a blank
|
Otherwise, the *input-files* are concatenated (with a blank
|
||||||
line between each) and used as input. Output goes to *stdout* by
|
line between each) and used as input. Output goes to *stdout* by
|
||||||
default (though output to *stdout* is disabled for the `odt`, `docx`,
|
default (though output to *stdout* is disabled for the `odt`, `docx`,
|
||||||
`epub2`, and `epub3` output formats). For output to a file, use the
|
`epub2`, and `epub3` output formats, unless it is forced using
|
||||||
`-o` option:
|
`-o -`). For output to a file, use the `-o` option:
|
||||||
|
|
||||||
pandoc -o output.html input.txt
|
pandoc -o output.html input.txt
|
||||||
|
|
||||||
|
@ -328,8 +328,8 @@ General options
|
||||||
`-o` *FILE*, `--output=`*FILE*
|
`-o` *FILE*, `--output=`*FILE*
|
||||||
|
|
||||||
: Write output to *FILE* instead of *stdout*. If *FILE* is
|
: Write output to *FILE* instead of *stdout*. If *FILE* is
|
||||||
`-`, output will go to *stdout*. (Exception: if the output
|
`-`, output will go to *stdout*, even if a non-textual format
|
||||||
format is `odt`, `docx`, `epub`, or `epub3`, output to stdout is disabled.)
|
(`docx`, `odt`, `epub2`, `epub3`) is specified.
|
||||||
|
|
||||||
`--data-dir=`*DIRECTORY*
|
`--data-dir=`*DIRECTORY*
|
||||||
|
|
||||||
|
|
|
@ -91,10 +91,6 @@ import Text.Pandoc.Shared (headerShift, isURI, openURL,
|
||||||
import qualified Text.Pandoc.UTF8 as UTF8
|
import qualified Text.Pandoc.UTF8 as UTF8
|
||||||
import Text.Pandoc.XML (toEntities)
|
import Text.Pandoc.XML (toEntities)
|
||||||
import Text.Printf
|
import Text.Printf
|
||||||
#ifndef _WINDOWS
|
|
||||||
import System.Posix.IO (stdOutput)
|
|
||||||
import System.Posix.Terminal (queryTerminal)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
data LineEnding = LF | CRLF | Native deriving (Show, Generic)
|
data LineEnding = LF | CRLF | Native deriving (Show, Generic)
|
||||||
|
|
||||||
|
@ -124,7 +120,7 @@ parseOptions options' defaults = do
|
||||||
convertWithOpts :: Opt -> IO ()
|
convertWithOpts :: Opt -> IO ()
|
||||||
convertWithOpts opts = do
|
convertWithOpts opts = do
|
||||||
let args = optInputFiles opts
|
let args = optInputFiles opts
|
||||||
let outputFile = optOutputFile opts
|
let outputFile = fromMaybe "-" (optOutputFile opts)
|
||||||
let filters = optFilters opts
|
let filters = optFilters opts
|
||||||
let verbosity = optVerbosity opts
|
let verbosity = optVerbosity opts
|
||||||
|
|
||||||
|
@ -245,18 +241,14 @@ convertWithOpts opts = do
|
||||||
(\(syn,dep) -> (T.unpack syn ++ " requires " ++
|
(\(syn,dep) -> (T.unpack syn ++ " requires " ++
|
||||||
T.unpack dep ++ " through IncludeRules.")) xs)
|
T.unpack dep ++ " through IncludeRules.")) xs)
|
||||||
|
|
||||||
|
-- We don't want to send output to the terminal if the user
|
||||||
|
-- does 'pandoc -t docx input.txt'; though we allow them to
|
||||||
#ifdef _WINDOWS
|
-- force this with '-o -'.
|
||||||
let istty = True
|
when (not (isTextFormat format) && optOutputFile opts == Nothing) $
|
||||||
#else
|
|
||||||
istty <- queryTerminal stdOutput
|
|
||||||
#endif
|
|
||||||
when (istty && not (isTextFormat format) && outputFile == "-") $
|
|
||||||
E.throwIO $ PandocAppError $
|
E.throwIO $ PandocAppError $
|
||||||
"Cannot write " ++ format ++ " output to stdout.\n" ++
|
"Cannot write " ++ format ++ " output to stdout.\n" ++
|
||||||
"Specify an output file using the -o option."
|
"Specify an output file using the -o option, or " ++
|
||||||
|
"use '-o -' to force output to stdout."
|
||||||
|
|
||||||
let convertTabs = tabFilter (if optPreserveTabs opts || readerName == "t2t"
|
let convertTabs = tabFilter (if optPreserveTabs opts || readerName == "t2t"
|
||||||
then 0
|
then 0
|
||||||
|
@ -303,7 +295,8 @@ convertWithOpts opts = do
|
||||||
variables <-
|
variables <-
|
||||||
withList (addStringAsVariable "sourcefile")
|
withList (addStringAsVariable "sourcefile")
|
||||||
(reverse $ optInputFiles opts)
|
(reverse $ optInputFiles opts)
|
||||||
(("outputfile", optOutputFile opts) : optVariables opts)
|
(("outputfile", fromMaybe "-" (optOutputFile opts))
|
||||||
|
: optVariables opts)
|
||||||
-- we reverse this list because, unlike
|
-- we reverse this list because, unlike
|
||||||
-- the other option lists here, it is
|
-- the other option lists here, it is
|
||||||
-- not reversed when parsed from CLI arguments.
|
-- not reversed when parsed from CLI arguments.
|
||||||
|
@ -562,7 +555,7 @@ data Opt = Opt
|
||||||
, optTemplate :: Maybe FilePath -- ^ Custom template
|
, optTemplate :: Maybe FilePath -- ^ Custom template
|
||||||
, optVariables :: [(String,String)] -- ^ Template variables to set
|
, optVariables :: [(String,String)] -- ^ Template variables to set
|
||||||
, optMetadata :: [(String, String)] -- ^ Metadata fields to set
|
, optMetadata :: [(String, String)] -- ^ Metadata fields to set
|
||||||
, optOutputFile :: FilePath -- ^ Name of output file
|
, optOutputFile :: Maybe FilePath -- ^ Name of output file
|
||||||
, optInputFiles :: [FilePath] -- ^ Names of input files
|
, optInputFiles :: [FilePath] -- ^ Names of input files
|
||||||
, optNumberSections :: Bool -- ^ Number sections in LaTeX
|
, optNumberSections :: Bool -- ^ Number sections in LaTeX
|
||||||
, optNumberOffset :: [Int] -- ^ Starting number for sections
|
, optNumberOffset :: [Int] -- ^ Starting number for sections
|
||||||
|
@ -638,7 +631,7 @@ defaultOpts = Opt
|
||||||
, optTemplate = Nothing
|
, optTemplate = Nothing
|
||||||
, optVariables = []
|
, optVariables = []
|
||||||
, optMetadata = []
|
, optMetadata = []
|
||||||
, optOutputFile = "-" -- "-" means stdout
|
, optOutputFile = Nothing
|
||||||
, optInputFiles = []
|
, optInputFiles = []
|
||||||
, optNumberSections = False
|
, optNumberSections = False
|
||||||
, optNumberOffset = [0,0,0,0,0,0]
|
, optNumberOffset = [0,0,0,0,0,0]
|
||||||
|
@ -889,7 +882,7 @@ options =
|
||||||
|
|
||||||
, Option "o" ["output"]
|
, Option "o" ["output"]
|
||||||
(ReqArg
|
(ReqArg
|
||||||
(\arg opt -> return opt { optOutputFile = arg })
|
(\arg opt -> return opt { optOutputFile = Just arg })
|
||||||
"FILE")
|
"FILE")
|
||||||
"" -- "Name of output file"
|
"" -- "Name of output file"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue