git.iko.soy/typeable/pandoc
on master help sign in
Logging: Added UnknownExtensions constructor to LogMessage.
680d7b5John MacFarlane
src/Text/Pandoc/App.hs+39−35
@@ -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
103
104
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−
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)
163
130
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+
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
181#endif
185#endif
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."
src/Text/Pandoc/App/OutputSettings.hs+9−3
@@ -70,9 +70,15 @@ optToOutputSettings opts = do
70 if pdfOutput
70 if pdfOutput
71 then liftIO $ pdfWriterAndProg (optWriter opts) (optPdfEngine opts)
71 then liftIO $ pdfWriterAndProg (optWriter opts) (optPdfEngine opts)
72 else case optWriter opts of
72 else case optWriter opts of
73− Nothing ->
73+ Nothing
74− return (fromMaybe "html" $ formatFromFilePaths [outputFile],
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)
75− Nothing)
81+ Just f -> return (f, Nothing)
76 Just f -> return (f, Nothing)
82 Just f -> return (f, Nothing)
77
83
78 let format = if ".lua" `isSuffixOf` writerName
84 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',
30 keyOrder)
30 keyOrder)
31import qualified Data.ByteString.Lazy as BL
31import qualified Data.ByteString.Lazy as BL
32import Data.Data (Data, toConstr)
32import Data.Data (Data, toConstr)
33−import Data.List (isSuffixOf)
33+import Data.List (isSuffixOf, intercalate)
34import qualified Data.Text as Text
34import qualified Data.Text as Text
35import Data.Typeable (Typeable)
35import Data.Typeable (Typeable)
36import GHC.Generics (Generic)
36import GHC.Generics (Generic)
@@ -88,6 +88,7 @@ data LogMessage =
88 | CouldNotLoadTranslations String String
88 | CouldNotLoadTranslations String String
89 | UnexpectedXmlElement String String
89 | UnexpectedXmlElement String String
90 | UnknownOrgExportOption String
90 | UnknownOrgExportOption String
91+ | UnknownExtensions [String] String
91 deriving (Show, Eq, Data, Ord, Typeable, Generic)
92 deriving (Show, Eq, Data, Ord, Typeable, Generic)
92
93
93instance ToJSON LogMessage where
94instance ToJSON LogMessage where
@@ -207,6 +208,9 @@ instance ToJSON LogMessage where
207 "parent" .= Text.pack parent]
208 "parent" .= Text.pack parent]
208 UnknownOrgExportOption option ->
209 UnknownOrgExportOption option ->
209 ["option" .= Text.pack option]
210 ["option" .= Text.pack option]
211+ UnknownExtensions exts format ->
212+ ["extensions" .= map Text.pack exts
213+ ,"format" .= Text.pack format]
210
214
211
215
212showPos :: SourcePos -> String
216showPos :: SourcePos -> String
@@ -310,6 +314,10 @@ showLogMessage msg =
310 "Unexpected XML element " ++ element ++ " in " ++ parent
314 "Unexpected XML element " ++ element ++ " in " ++ parent
311 UnknownOrgExportOption option ->
315 UnknownOrgExportOption option ->
312 "Ignoring unknown Org export option: " ++ option
316 "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
313
321
314messageVerbosity:: LogMessage -> Verbosity
322messageVerbosity:: LogMessage -> Verbosity
315messageVerbosity msg =
323messageVerbosity msg =
@@ -351,3 +359,4 @@ messageVerbosity msg =
351 CouldNotLoadTranslations{} -> WARNING
359 CouldNotLoadTranslations{} -> WARNING
352 UnexpectedXmlElement {} -> WARNING
360 UnexpectedXmlElement {} -> WARNING
353 UnknownOrgExportOption {} -> WARNING
361 UnknownOrgExportOption {} -> WARNING
362+ UnknownExtensions{} -> WARNING