@@ -26,6 +26,7 @@ import Prelude
26import qualified Control.Exception as E
26import qualified Control.Exception as E
27import Control.Monad
27import Control.Monad
28import Control.Monad.Trans
28import Control.Monad.Trans
29+import Control.Monad.Except (throwError)
29import qualified Data.ByteString as BS
30import qualified Data.ByteString as BS
30import qualified Data.ByteString.Lazy as B
31import qualified Data.ByteString.Lazy as B
31import Data.Char (toLower)
32import Data.Char (toLower)
@@ -101,40 +102,6 @@ convertWithOpts opts = do
101 selectUserDataDir ds
102 selectUserDataDir ds
102 Just _ -> return $ optDataDir opts
103 Just _ -> return $ optDataDir opts
104− -- assign reader and writer based on options and filenames
105− let readerName = case optReader opts of
107− Nothing -> fromMaybe fallback $
108− formatFromFilePaths sources
109− where fallback = if any isURI sources
113− let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
115− -- TODO: we have to get the input and the output into the state for
116− -- the sake of the text2tags reader.
117− (reader, readerExts) <-
118− case getReader readerName of
119− Right (r, es) -> return (r :: Reader PandocIO, es)
120− Left e -> E.throwIO $ PandocAppError e'
121− where e' = case readerName of
123− "\nPandoc can convert to PDF, but not from PDF."
125− "\nPandoc can convert from DOCX, but not from DOC.\nTry using Word to save your DOC file as DOCX, and convert that with pandoc."
128− let convertTabs = tabFilter (if optPreserveTabs opts ||
129− readerName == "t2t" ||
132− else optTabStop opts)
134− readSources :: [FilePath] -> PandocIO Text
135− readSources srcs = convertTabs . T.intercalate (T.pack "\n") <$>
136− mapM readSource srcs
138 let runIO' :: PandocIO a -> IO a
105 let runIO' :: PandocIO a -> IO a
139 runIO' f = do
106 runIO' f = do
140 (res, reports) <- runIOorExplode $ do
107 (res, reports) <- runIOorExplode $ do
@@ -161,6 +128,43 @@ convertWithOpts opts = do
161 setInputFiles (optInputFiles opts)
128 setInputFiles (optInputFiles opts)
162 setOutputFile (optOutputFile opts)
129 setOutputFile (optOutputFile opts)
131+ -- assign reader and writer based on options and filenames
132+ readerName <- case optReader opts of
134+ Nothing -> case formatFromFilePaths sources of
135+ Just f' -> return f'
136+ Nothing | sources == ["-"] -> return "markdown"
137+ | any isURI sources -> return "html"
139+ report $ UnknownExtensions
140+ (map takeExtension sources) "markdown"
143+ let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
145+ (reader, readerExts) <-
146+ case getReader readerName of
147+ Right (r, es) -> return (r :: Reader PandocIO, es)
148+ Left e -> throwError $ PandocAppError e'
149+ where e' = case readerName of
151+ "\nPandoc can convert to PDF, but not from PDF."
153+ "\nPandoc can convert from DOCX, but not from DOC.\nTry using Word to save your DOC file as DOCX, and convert that with pandoc."
156+ let convertTabs = tabFilter (if optPreserveTabs opts ||
157+ readerName == "t2t" ||
160+ else optTabStop opts)
163+ let readSources :: [FilePath] -> PandocIO Text
164+ readSources srcs = convertTabs . T.intercalate (T.pack "\n") <$>
165+ mapM readSource srcs
164 outputSettings <- optToOutputSettings opts
168 outputSettings <- optToOutputSettings opts
165 let format = outputFormat outputSettings
169 let format = outputFormat outputSettings
166 let writer = outputWriter outputSettings
170 let writer = outputWriter outputSettings
@@ -180,7 +184,7 @@ convertWithOpts opts = do
180 istty <- liftIO $ queryTerminal stdOutput
184 istty <- liftIO $ queryTerminal stdOutput
182 when (not (isTextFormat format) && istty && isNothing ( optOutputFile opts)) $
186 when (not (isTextFormat format) && istty && isNothing ( optOutputFile opts)) $
183− liftIO $ E.throwIO $ PandocAppError $
187+ throwError $ PandocAppError $
184 "Cannot write " ++ format ++ " output to terminal.\n" ++
188 "Cannot write " ++ format ++ " output to terminal.\n" ++
185 "Specify an output file using the -o option, or " ++
189 "Specify an output file using the -o option, or " ++
186 "use '-o -' to force output to stdout."
190 "use '-o -' to force output to stdout."