From da82107116a8af3cd4c54b2c8d486bbc15fd3a04 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 18 Jun 2026 23:27:17 +0300 Subject: [PATCH] Change adding of `.yaml` extension in `--defaults` option. Previously it was always added if the specified filename lacked an extension. Now it is only added if there is no file with the original name. So, these are tried in order: `FILE`, `FILE.yaml`, `DATADIR/defaults/FILE`, `DATADIR/defaults/FILE.yaml`. This facilitates using `--defaults` with bash/zsh `<(..)` or `=(..)`. Closes #11717. --- MANUAL.txt | 11 ++++++----- src/Text/Pandoc/App/Opt.hs | 25 ++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/MANUAL.txt b/MANUAL.txt index 8a73b867a..5e954fef5 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -422,11 +422,12 @@ header when requesting a document from a URL: and output files, can be set using a defaults file. The file will be searched for first in the working directory, and then in the `defaults` subdirectory of the user data directory - (see `--data-dir`). The `.yaml` extension will be added if - *FILE* lacks an extension. See the section [Defaults files] - for more information on the file format. Settings from the - defaults file may be overridden or extended by subsequent - options on the command line. + (see `--data-dir`). If *FILE* lacks an extension, pandoc will + also try adding the `.yaml` extension if *FILE* is not found. + See the section [Defaults files] for more information on the + file format. Settings from the defaults file may be + overridden or extended by subsequent options on the command + line. `--bash-completion` diff --git a/src/Text/Pandoc/App/Opt.hs b/src/Text/Pandoc/App/Opt.hs index f95ba2a44..8b0f805f7 100644 --- a/src/Text/Pandoc/App/Opt.hs +++ b/src/Text/Pandoc/App/Opt.hs @@ -31,7 +31,7 @@ import Control.Monad.Except (throwError) import Control.Monad.Trans (MonadIO, liftIO, lift) import Control.Monad ((>=>), foldM) import Control.Monad.State.Strict (StateT, modify, gets) -import System.FilePath ( addExtension, (), takeExtension, takeDirectory ) +import System.FilePath ( (<.>), (), takeDirectory ) import System.Directory ( canonicalizePath ) import Data.Char (toLower, isSpace) import Data.Maybe (fromMaybe) @@ -891,19 +891,18 @@ fullDefaultsPath :: (PandocMonad m, MonadIO m) -> FilePath -> m FilePath fullDefaultsPath dataDir file = do - let fp = if null (takeExtension file) - then addExtension file "yaml" - else file defaultDataDir <- liftIO defaultUserDataDir - let defaultFp = fromMaybe defaultDataDir dataDir "defaults" fp - fpExists <- fileExists fp - if fpExists - then return fp - else do - defaultFpExists <- fileExists defaultFp - if defaultFpExists - then return defaultFp - else return fp + let ddir = fromMaybe defaultDataDir dataDir "defaults" + let findFile [] = return file + findFile (fp:fps) = do + fpExists <- fileExists fp + if fpExists + then return fp + else findFile fps + findFile [file, + file <.> "yaml", + ddir file, + ddir file <.> "yaml"] -- | In a list of lists, append another list in front of every list which -- starts with specific element.