git.iko.soy/typeable/pandoc
on master help sign in
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.

commit1aeb2beba00b67fe1ec419483e8c62a9daee09f7 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 5 Mar 2021, 11:56 (6 years ago) parenta34e0e2
2 files changed+24−2
MANUAL.txt+13 src/Text/Pandoc/App/Opt.hs+11−2
MANUAL.txt+13
@@ -1684,6 +1684,19 @@ user data directory that is current when the defaults file is
16841684parsed, regardless of the setting of the environment
16851685variable `USERDATA`.
16861686
1687+`${.}` will resolve to the directory containing the default
1688+file itself. This allows you to refer to resources contained
1689+in that directory:
1690+
1691+``` yaml
1692+epub-cover-image: ${.}/cover.jpg
1693+epub-metadata: ${.}/meta.xml
1694+resource-path:
1695+- . # the working directory from which pandoc is run
1696+- ${.}/images # the images subdirectory of the directory
1697+ # containing this defaults file
1698+```
1699+
16871700This environment variable interpolation syntax *only* works in
16881701fields that expect file paths.
16891702
src/Text/Pandoc/App/Opt.hs+11−2
@@ -27,7 +27,8 @@ module Text.Pandoc.App.Opt (
2727 ) where
2828import Control.Monad.Except (MonadIO, liftIO, throwError, (>=>), foldM)
2929import Control.Monad.State.Strict (StateT, modify, gets)
30−import System.FilePath ( addExtension, (</>), takeExtension )
30+import System.FilePath ( addExtension, (</>), takeExtension, takeDirectory )
31+import System.Directory ( canonicalizePath )
3132import Data.Char (isLower, toLower)
3233import Data.Maybe (fromMaybe)
3334import GHC.Generics hiding (Meta)
@@ -189,7 +190,8 @@ instance (PandocMonad m, MonadIO m)
189190 toText _ = ""
190191 parseYAML n = failAtNode n "Expected a mapping"
191192
192−resolveVarsInOpt :: (PandocMonad m, MonadIO m) => Opt -> m Opt
193+resolveVarsInOpt :: forall m. (PandocMonad m, MonadIO m)
194+ => Opt -> StateT DefaultsState m Opt
193195resolveVarsInOpt
194196 opt@Opt
195197 { optTemplate = oTemplate
@@ -263,6 +265,7 @@ resolveVarsInOpt
263265 }
264266
265267 where
268+ resolveVars :: FilePath -> StateT DefaultsState m FilePath
266269 resolveVars [] = return []
267270 resolveVars ('$':'{':xs) =
268271 let (ys, zs) = break (=='}') xs
@@ -272,6 +275,12 @@ resolveVarsInOpt
272275 val <- lookupEnv' ys
273276 (val ++) <$> resolveVars (drop 1 zs)
274277 resolveVars (c:cs) = (c:) <$> resolveVars cs
278+ lookupEnv' :: String -> StateT DefaultsState m String
279+ lookupEnv' "." = do
280+ mbCurDefaults <- gets curDefaults
281+ maybe (return "")
282+ (fmap takeDirectory . liftIO . canonicalizePath)
283+ mbCurDefaults
275284 lookupEnv' "USERDATA" = do
276285 mbodatadir <- mapM resolveVars oDataDir
277286 mbdatadir <- getUserDataDir