Skip to content

Commit 1b50a46

Browse files
committed
Text.Pandoc.Templates: Fix bug in getTemplate.
When calling `fetchItem` in `getTemplate`, we temporarily reset the `stSourceURL` in CommonState so that the template is sought locally. Previously, an exception in `fetchItem` would prevent `stSourceURL` from being set back to its original value. This could result in a local file being fetched instead of a remote one. Note that an exception is triggered when one uses e.g. `--template default.html5`; in that case `getTemplate` handles the exception by looking for the file in the user data directory. This patch fixes the bug so that the `stSourceURL` is reset regardless of whether `fetchItem` raises an exception. Thanks to Yingjie Su for identifying the problem.
1 parent 9260d17 commit 1b50a46

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

pandoc.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ library
544544
ipynb >= 0.2 && < 0.3,
545545
jira-wiki-markup >= 1.5.1 && < 1.6,
546546
mime-types >= 0.1.1 && < 0.2,
547-
mtl >= 2.2 && < 2.4,
547+
mtl >= 2.3 && < 2.4,
548548
network-uri >= 2.6 && < 2.8,
549549
pandoc-types >= 1.23.1.2 && < 1.24,
550550
parsec >= 3.1 && < 3.2,

src/Text/Pandoc/Templates.hs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import Text.Pandoc.Class.PandocMonad (PandocMonad, fetchItem,
4747
getCommonState, modifyCommonState,
4848
toTextM)
4949
import Text.Pandoc.Data (readDataFile)
50-
import Control.Monad.Except (catchError, throwError)
50+
import Control.Monad.Except (throwError, tryError)
5151
import Data.Text (Text)
5252
import qualified Data.Text as T
5353
import Text.Pandoc.Error
@@ -74,24 +74,22 @@ instance PandocMonad m => TemplateMonad (WithPartials m) where
7474
-- | Retrieve text for a template.
7575
getTemplate :: PandocMonad m => FilePath -> m Text
7676
getTemplate tp =
77-
((do surl <- stSourceURL <$> getCommonState
77+
(do surl <- stSourceURL <$> getCommonState
7878
-- we don't want to look for templates remotely
7979
-- unless the full URL is specified:
80-
modifyCommonState $ \st -> st{
81-
stSourceURL = Nothing }
82-
(bs, _) <- fetchItem $ T.pack tp
83-
modifyCommonState $ \st -> st{
84-
stSourceURL = surl }
85-
return bs)
86-
`catchError`
87-
(\e -> case e of
80+
modifyCommonState $ \st -> st{ stSourceURL = Nothing }
81+
res <- tryError $ fetchItem $ T.pack tp
82+
modifyCommonState $ \st -> st{ stSourceURL = surl }
83+
case res of
84+
Right (bs, _) -> return bs
85+
Left e -> case e of
8886
PandocResourceNotFound _ ->
8987
-- see #5987 on reason for takeFileName
9088
readDataFile ("templates" </> takeFileName tp)
9189
PandocIOError _ ioe | isDoesNotExistError ioe ->
9290
-- see #5987 on reason for takeFileName
9391
readDataFile ("templates" </> takeFileName tp)
94-
_ -> throwError e)) >>= toTextM tp
92+
_ -> throwError e) >>= toTextM tp
9593

9694
-- | Get default template for the specified writer.
9795
getDefaultTemplate :: PandocMonad m

0 commit comments

Comments
 (0)