git.iko.soy/typeable/pandoc
on master help sign in
--defaults: add .yaml extension if absent, look in user data dir...

under "defaults" subdirectory.

commitfcefcfec39eeff5eff02bdd07dca7839f6b08ca2 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 9 Oct 2019, 23:29 (7 years ago) parentff1df24
1 file changed+30−13
src/Text/Pandoc/App/CommandLineOptions.hs+30−13
src/Text/Pandoc/App/CommandLineOptions.hs+30−13
@@ -42,12 +42,13 @@ import System.FilePath
4242import System.IO (stdout)
4343import Text.Pandoc
4444import Text.Pandoc.Builder (setMeta)
45−import Text.Pandoc.App.Opt (Opt (..), LineEnding (..), defaultOpts)
45+import Text.Pandoc.App.Opt (Opt (..), LineEnding (..))
4646import Text.Pandoc.Filter (Filter (..))
4747import Text.Pandoc.Highlighting (highlightingStyles)
4848import Text.Pandoc.Writers.Math (defaultMathJaxURL, defaultKaTeXURL)
4949import Text.Pandoc.Shared (ordNub, safeRead, defaultUserDataDirs)
5050import Text.Printf
51+import Text.Pandoc.Class (runIOorExplode, PandocMonad(..))
5152
5253#ifdef EMBED_DATA_FILES
5354import Text.Pandoc.Data (dataFiles)
@@ -106,25 +107,41 @@ engines = map ("html",) htmlEngines ++
106107pdfEngines :: [String]
107108pdfEngines = ordNub $ map snd engines
108109
110+findFile :: PandocMonad m => [FilePath] -> m (Maybe FilePath)
111+findFile [] = return Nothing
112+findFile (f:fs) = do
113+ exists <- fileExists f
114+ if exists
115+ then return $ Just f
116+ else findFile fs
117+
109118-- | A list of functions, each transforming the options data structure
110119-- in response to a command-line option.
111120options :: [OptDescr (Opt -> IO Opt)]
112121options =
113122 [ Option "" ["defaults"]
114123 (ReqArg
115− (\arg _opt -> do
116− let defaults = YA.encode1 defaultOpts
117− inp <- E.catch (B.readFile arg)
118− (\e -> E.throwIO $ PandocIOError
119− "Error reading defaults file" e)
124+ (\arg opt -> runIOorExplode $ do
125+ setVerbosity $ optVerbosity opt
126+ let fp = if null (takeExtension arg)
127+ then addExtension arg "yaml"
128+ else arg
129+ dataDirs <- liftIO defaultUserDataDirs
130+ let fps = case optDataDir opt of
131+ Nothing -> (fp : map (</> ("defaults" </> fp))
132+ dataDirs)
133+ Just dd -> [fp, dd </> "defaults" </> fp]
134+ fp' <- fromMaybe fp <$> findFile fps
135+ inp <- readFileLazy fp'
136+ let defaults = YA.encode1 opt
120137 case YA.decode1 (defaults <> inp) of
121− Right (newopts :: Opt) -> do
122− return newopts
123− Left (errpos, errmsg) -> E.throwIO $
124− PandocParseError $ "Error parsing " ++ arg ++
125− " (line " ++ show (Y.posLine errpos) ++
126− " column " ++ show (Y.posColumn errpos) ++ ")\n"
127− ++ errmsg
138+ Right (newopts :: Opt) -> return newopts
139+ Left (errpos, errmsg) -> throwError $
140+ PandocParseError $
141+ "Error parsing " ++ fp' ++
142+ " (line " ++ show (Y.posLine errpos) ++
143+ " column " ++ show (Y.posColumn errpos) ++
144+ ")\n" ++ errmsg
128145 )
129146 "FILE")
130147 ""