diff options
author | John MacFarlane <jgm@berkeley.edu> | 2021-03-05 11:56:41 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2021-03-05 11:56:41 -0800 |
commit | df00cf05cbf817b7d8e7de0a4a220dd70f8a9608 (patch) | |
tree | 98ac261dccf4c7b13055b4571f189e15095b590a /src/Text/Pandoc/App | |
parent | 6dd7520cc4b3816ae13ec486ce0909b9b881d240 (diff) | |
download | pandoc-df00cf05cbf817b7d8e7de0a4a220dd70f8a9608.tar.gz |
Allow `${.}` in defaults files paths...
to refer to the directory where the default file is.
This will make it possible to create moveable
"packages" of resources in a directory.
Closes #5871.
Diffstat (limited to 'src/Text/Pandoc/App')
-rw-r--r-- | src/Text/Pandoc/App/Opt.hs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/Text/Pandoc/App/Opt.hs b/src/Text/Pandoc/App/Opt.hs index b56b2c377..d09a6afc0 100644 --- a/src/Text/Pandoc/App/Opt.hs +++ b/src/Text/Pandoc/App/Opt.hs @@ -27,7 +27,8 @@ module Text.Pandoc.App.Opt ( ) where import Control.Monad.Except (MonadIO, liftIO, throwError, (>=>), foldM) import Control.Monad.State.Strict (StateT, modify, gets) -import System.FilePath ( addExtension, (</>), takeExtension ) +import System.FilePath ( addExtension, (</>), takeExtension, takeDirectory ) +import System.Directory ( canonicalizePath ) import Data.Char (isLower, toLower) import Data.Maybe (fromMaybe) import GHC.Generics hiding (Meta) @@ -189,7 +190,8 @@ instance (PandocMonad m, MonadIO m) toText _ = "" parseYAML n = failAtNode n "Expected a mapping" -resolveVarsInOpt :: (PandocMonad m, MonadIO m) => Opt -> m Opt +resolveVarsInOpt :: forall m. (PandocMonad m, MonadIO m) + => Opt -> StateT DefaultsState m Opt resolveVarsInOpt opt@Opt { optTemplate = oTemplate @@ -263,6 +265,7 @@ resolveVarsInOpt } where + resolveVars :: FilePath -> StateT DefaultsState m FilePath resolveVars [] = return [] resolveVars ('$':'{':xs) = let (ys, zs) = break (=='}') xs @@ -272,6 +275,12 @@ resolveVarsInOpt val <- lookupEnv' ys (val ++) <$> resolveVars (drop 1 zs) resolveVars (c:cs) = (c:) <$> resolveVars cs + lookupEnv' :: String -> StateT DefaultsState m String + lookupEnv' "." = do + mbCurDefaults <- gets curDefaults + maybe (return "") + (fmap takeDirectory . liftIO . canonicalizePath) + mbCurDefaults lookupEnv' "USERDATA" = do mbodatadir <- mapM resolveVars oDataDir mbdatadir <- getUserDataDir |