git.iko.soy/typeable/pandoc
on master help sign in
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.

commit61cf3affa98f8331a2f2d55eedf56fc2a2a529e2 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 16 Aug 2017, 09:45 (9 years ago) parent7a40f48
2 files changed+16−23
MANUAL.txt+4−4 src/Text/Pandoc/App.hs+12−19
MANUAL.txt+4−4
@@ -110,8 +110,8 @@ If no *input-file* is specified, input is read from *stdin*.
110110Otherwise, the *input-files* are concatenated (with a blank
111111line between each) and used as input. Output goes to *stdout* by
112112default (though output to *stdout* is disabled for the `odt`, `docx`,
113−`epub2`, and `epub3` output formats). For output to a file, use the
114−`-o` option:
113+`epub2`, and `epub3` output formats, unless it is forced using
114+`-o -`). For output to a file, use the `-o` option:
115115
116116 pandoc -o output.html input.txt
117117
@@ -328,8 +328,8 @@ General options
328328`-o` *FILE*, `--output=`*FILE*
329329
330330: Write output to *FILE* instead of *stdout*. If *FILE* is
331− `-`, output will go to *stdout*. (Exception: if the output
332− format is `odt`, `docx`, `epub`, or `epub3`, output to stdout is disabled.)
331+ `-`, output will go to *stdout*, even if a non-textual format
332+ (`docx`, `odt`, `epub2`, `epub3`) is specified.
333333
334334`--data-dir=`*DIRECTORY*
335335
src/Text/Pandoc/App.hs+12−19
@@ -91,10 +91,6 @@ import Text.Pandoc.Shared (headerShift, isURI, openURL,
9191import qualified Text.Pandoc.UTF8 as UTF8
9292import Text.Pandoc.XML (toEntities)
9393import Text.Printf
94−#ifndef _WINDOWS
95−import System.Posix.IO (stdOutput)
96−import System.Posix.Terminal (queryTerminal)
97−#endif
9894
9995data LineEnding = LF | CRLF | Native deriving (Show, Generic)
10096
@@ -124,7 +120,7 @@ parseOptions options' defaults = do
124120convertWithOpts :: Opt -> IO ()
125121convertWithOpts opts = do
126122 let args = optInputFiles opts
127− let outputFile = optOutputFile opts
123+ let outputFile = fromMaybe "-" (optOutputFile opts)
128124 let filters = optFilters opts
129125 let verbosity = optVerbosity opts
130126
@@ -245,18 +241,14 @@ convertWithOpts opts = do
245241 (\(syn,dep) -> (T.unpack syn ++ " requires " ++
246242 T.unpack dep ++ " through IncludeRules.")) xs)
247243
248−
249−
250−#ifdef _WINDOWS
251− let istty = True
252−#else
253− istty <- queryTerminal stdOutput
254−#endif
255− when (istty && not (isTextFormat format) && outputFile == "-") $
244+ -- We don't want to send output to the terminal if the user
245+ -- does 'pandoc -t docx input.txt'; though we allow them to
246+ -- force this with '-o -'.
247+ when (not (isTextFormat format) && optOutputFile opts == Nothing) $
256248 E.throwIO $ PandocAppError $
257249 "Cannot write " ++ format ++ " output to stdout.\n" ++
258− "Specify an output file using the -o option."
259−
250+ "Specify an output file using the -o option, or " ++
251+ "use '-o -' to force output to stdout."
260252
261253 let convertTabs = tabFilter (if optPreserveTabs opts || readerName == "t2t"
262254 then 0
@@ -303,7 +295,8 @@ convertWithOpts opts = do
303295 variables <-
304296 withList (addStringAsVariable "sourcefile")
305297 (reverse $ optInputFiles opts)
306− (("outputfile", optOutputFile opts) : optVariables opts)
298+ (("outputfile", fromMaybe "-" (optOutputFile opts))
299+ : optVariables opts)
307300 -- we reverse this list because, unlike
308301 -- the other option lists here, it is
309302 -- not reversed when parsed from CLI arguments.
@@ -562,7 +555,7 @@ data Opt = Opt
562555 , optTemplate :: Maybe FilePath -- ^ Custom template
563556 , optVariables :: [(String,String)] -- ^ Template variables to set
564557 , optMetadata :: [(String, String)] -- ^ Metadata fields to set
565− , optOutputFile :: FilePath -- ^ Name of output file
558+ , optOutputFile :: Maybe FilePath -- ^ Name of output file
566559 , optInputFiles :: [FilePath] -- ^ Names of input files
567560 , optNumberSections :: Bool -- ^ Number sections in LaTeX
568561 , optNumberOffset :: [Int] -- ^ Starting number for sections
@@ -638,7 +631,7 @@ defaultOpts = Opt
638631 , optTemplate = Nothing
639632 , optVariables = []
640633 , optMetadata = []
641− , optOutputFile = "-" -- "-" means stdout
634+ , optOutputFile = Nothing
642635 , optInputFiles = []
643636 , optNumberSections = False
644637 , optNumberOffset = [0,0,0,0,0,0]
@@ -889,7 +882,7 @@ options =
889882
890883 , Option "o" ["output"]
891884 (ReqArg
892− (\arg opt -> return opt { optOutputFile = arg })
885+ (\arg opt -> return opt { optOutputFile = Just arg })
893886 "FILE")
894887 "" -- "Name of output file"
895888