aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <[email protected]>2024-12-19 10:17:58 -0800
committerJohn MacFarlane <[email protected]>2024-12-19 10:20:22 -0800
commitdf0d3fb78cd5d0457c791bbd3b132dd284bfe49c (patch)
treea97b288cc265b9601181ae61479534522dc79f6a /src
parentd0b652491a10a7b64e1939108005429cfbeb0e3b (diff)
Change `--template` to allow use of extensionless templates.
The intent is to allow bash process substitution: e.g., `--template <(echo "foo")`. Previously pandoc *always* added an extension based on the output format, which caused problems with the absolute filenames used by bash process substitution (e.g. `/dev/fd/11`). Now, if the template has no extension, pandoc will first try to find it without the extension, and then add the extension if it can't be found. So, in general, extensionless templates can now be used. But this has been implemented in a way that should not cause problems for existing uses, unless you are using a template `NAME.FORMAT` but happen to have an extensionless file `NAME` in the template search path. Closes #5270.
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/App/OutputSettings.hs18
1 files changed, 10 insertions, 8 deletions
diff --git a/src/Text/Pandoc/App/OutputSettings.hs b/src/Text/Pandoc/App/OutputSettings.hs
index f8c27bee4..38cd35455 100644
--- a/src/Text/Pandoc/App/OutputSettings.hs
+++ b/src/Text/Pandoc/App/OutputSettings.hs
@@ -25,7 +25,7 @@ import qualified Data.Text as T
import Text.DocTemplates (toVal, Context(..), Val(..))
import qualified Control.Exception as E
import Control.Monad
-import Control.Monad.Except (throwError)
+import Control.Monad.Except (throwError, catchError)
import Control.Monad.Trans
import Data.Char (toLower)
import Data.List (find)
@@ -111,13 +111,15 @@ optToOutputSettings scriptingEngine opts = do
_ | not standalone -> return Nothing
Nothing -> Just <$> getDefault
Just tp -> do
- -- strip off extensions
- let tp' = case takeExtension tp of
- "" -> tp <.> T.unpack format
- _ -> tp
- getTemplate tp'
- >>= runWithPartials . compileTemplate tp'
- >>= fmap Just . templateOrThrow
+ let getAndCompile fp =
+ getTemplate fp >>= runWithPartials . compileTemplate fp >>=
+ fmap Just . templateOrThrow
+ catchError
+ (getAndCompile tp)
+ (\e ->
+ if null (takeExtension tp)
+ then getAndCompile (tp <.> T.unpack format)
+ else throwError e)
(writer, writerExts, mtemplate) <-
if "lua" `T.isSuffixOf` format