@@ -26,6 +26,7 @@ import Prelude
2626import qualified Control.Exception as E
2727import Control.Monad
2828import Control.Monad.Trans
29+import Control.Monad.Except (throwError)
2930import qualified Data.ByteString as BS
3031import qualified Data.ByteString.Lazy as B
3132import Data.Char (toLower)
@@ -101,40 +102,6 @@ convertWithOpts opts = do
101102 selectUserDataDir ds
102103 Just _ -> return $ optDataDir opts
103104
104− -- assign reader and writer based on options and filenames
105− let readerName = case optReader opts of
106− Just f -> f
107− Nothing -> fromMaybe fallback $
108− formatFromFilePaths sources
109− where fallback = if any isURI sources
110− then "html"
111− else "markdown"
112−
113− let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
114−
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
122− "pdf" -> e ++
123− "\nPandoc can convert to PDF, but not from PDF."
124− "doc" -> e ++
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."
126− _ -> e
127−
128− let convertTabs = tabFilter (if optPreserveTabs opts ||
129− readerName == "t2t" ||
130− readerName == "man"
131− then 0
132− else optTabStop opts)
133−
134− readSources :: [FilePath] -> PandocIO Text
135− readSources srcs = convertTabs . T.intercalate (T.pack "\n") <$>
136− mapM readSource srcs
137−
138105 let runIO' :: PandocIO a -> IO a
139106 runIO' f = do
140107 (res, reports) <- runIOorExplode $ do
@@ -161,6 +128,43 @@ convertWithOpts opts = do
161128 setInputFiles (optInputFiles opts)
162129 setOutputFile (optOutputFile opts)
163130
131+ -- assign reader and writer based on options and filenames
132+ readerName <- case optReader opts of
133+ Just f -> return f
134+ Nothing -> case formatFromFilePaths sources of
135+ Just f' -> return f'
136+ Nothing | sources == ["-"] -> return "markdown"
137+ | any isURI sources -> return "html"
138+ | otherwise -> do
139+ report $ UnknownExtensions
140+ (map takeExtension sources) "markdown"
141+ return "markdown"
142+
143+ let pdfOutput = map toLower (takeExtension outputFile) == ".pdf"
144+
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
150+ "pdf" -> e ++
151+ "\nPandoc can convert to PDF, but not from PDF."
152+ "doc" -> e ++
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."
154+ _ -> e
155+
156+ let convertTabs = tabFilter (if optPreserveTabs opts ||
157+ readerName == "t2t" ||
158+ readerName == "man"
159+ then 0
160+ else optTabStop opts)
161+
162+
163+ let readSources :: [FilePath] -> PandocIO Text
164+ readSources srcs = convertTabs . T.intercalate (T.pack "\n") <$>
165+ mapM readSource srcs
166+
167+
164168 outputSettings <- optToOutputSettings opts
165169 let format = outputFormat outputSettings
166170 let writer = outputWriter outputSettings
@@ -180,7 +184,7 @@ convertWithOpts opts = do
180184 istty <- liftIO $ queryTerminal stdOutput
181185#endif
182186 when (not (isTextFormat format) && istty && isNothing ( optOutputFile opts)) $
183− liftIO $ E.throwIO $ PandocAppError $
187+ throwError $ PandocAppError $
184188 "Cannot write " ++ format ++ " output to terminal.\n" ++
185189 "Specify an output file using the -o option, or " ++
186190 "use '-o -' to force output to stdout."