git.iko.soy/typeable/pandoc
on master help sign in
Logging: Added UnknownExtensions constructor to LogMessage.

[API change] Issue this warning when we're falling back to markdown or html because we don't recognize the extension of the input or output files.

commit680d7b515f2467a74c508c3c26281eead9af6e7a authorJohn MacFarlane <jgm@berkeley.edu> dateSun 14 Jul 2019, 11:35 (7 years ago) parented0548d
3 files changed+58−39
src/Text/Pandoc/App.hs+39−35 src/Text/Pandoc/App/OutputSettings.hs+9−3 src/Text/Pandoc/Logging.hs+10−1
src/Text/Pandoc/App.hs+39−35
@@ -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."
src/Text/Pandoc/App/OutputSettings.hs+9−3
@@ -70,9 +70,15 @@ optToOutputSettings opts = do
7070 if pdfOutput
7171 then liftIO $ pdfWriterAndProg (optWriter opts) (optPdfEngine opts)
7272 else case optWriter opts of
73− Nothing ->
74− return (fromMaybe "html" $ formatFromFilePaths [outputFile],
75− Nothing)
73+ Nothing
74+ | outputFile == "-" -> return ("html", Nothing)
75+ | otherwise ->
76+ case formatFromFilePaths [outputFile] of
77+ Nothing -> do
78+ report $ UnknownExtensions
79+ [takeExtension outputFile] "html"
80+ return ("html", Nothing)
81+ Just f -> return (f, Nothing)
7682 Just f -> return (f, Nothing)
7783
7884 let format = if ".lua" `isSuffixOf` writerName
src/Text/Pandoc/Logging.hs+10−1
@@ -30,7 +30,7 @@ import Data.Aeson.Encode.Pretty (Config (..), defConfig, encodePretty',
3030 keyOrder)
3131import qualified Data.ByteString.Lazy as BL
3232import Data.Data (Data, toConstr)
33−import Data.List (isSuffixOf)
33+import Data.List (isSuffixOf, intercalate)
3434import qualified Data.Text as Text
3535import Data.Typeable (Typeable)
3636import GHC.Generics (Generic)
@@ -88,6 +88,7 @@ data LogMessage =
8888 | CouldNotLoadTranslations String String
8989 | UnexpectedXmlElement String String
9090 | UnknownOrgExportOption String
91+ | UnknownExtensions [String] String
9192 deriving (Show, Eq, Data, Ord, Typeable, Generic)
9293
9394instance ToJSON LogMessage where
@@ -207,6 +208,9 @@ instance ToJSON LogMessage where
207208 "parent" .= Text.pack parent]
208209 UnknownOrgExportOption option ->
209210 ["option" .= Text.pack option]
211+ UnknownExtensions exts format ->
212+ ["extensions" .= map Text.pack exts
213+ ,"format" .= Text.pack format]
210214
211215
212216showPos :: SourcePos -> String
@@ -310,6 +314,10 @@ showLogMessage msg =
310314 "Unexpected XML element " ++ element ++ " in " ++ parent
311315 UnknownOrgExportOption option ->
312316 "Ignoring unknown Org export option: " ++ option
317+ UnknownExtensions exts format ->
318+ "Could not deduce format from file extension " ++
319+ intercalate " or " exts ++ "\n" ++
320+ "Defaulting to " ++ format
313321
314322messageVerbosity:: LogMessage -> Verbosity
315323messageVerbosity msg =
@@ -351,3 +359,4 @@ messageVerbosity msg =
351359 CouldNotLoadTranslations{} -> WARNING
352360 UnexpectedXmlElement {} -> WARNING
353361 UnknownOrgExportOption {} -> WARNING
362+ UnknownExtensions{} -> WARNING