@@ -1,3 +1,4 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
12{-# LANGUAGE MultiParamTypeClasses #-}
23{-# LANGUAGE ScopedTypeVariables #-}
34{-# LANGUAGE OverloadedStrings #-}
@@ -14,18 +15,61 @@ Utility functions for working with pandoc templates.
1415-}
1516
1617module Text.Pandoc.Templates ( Template
18+ , WithDefaultPartials(..)
19+ , WithPartials(..)
1720 , compileTemplate
1821 , renderTemplate
22+ , getTemplate
1923 , getDefaultTemplate
24+ , compileDefaultTemplate
2025 ) where
2126
2227import Prelude
2328import System.FilePath ((<.>), (</>))
24−import Text.DocTemplates (Template, compileTemplate, renderTemplate)
25−import Text.Pandoc.Class (PandocMonad, readDataFile)
29+import Text.DocTemplates (Template, TemplateMonad(..), compileTemplate, renderTemplate)
30+import Text.Pandoc.Class (PandocMonad, readDataFile, fetchItem,
31+ CommonState(..), getCommonState, modifyCommonState)
2632import qualified Text.Pandoc.UTF8 as UTF8
33+import Control.Monad.Except (catchError, throwError)
2734import Data.Text (Text)
2835import qualified Data.Text as T
36+import Text.Pandoc.Error
37+
38+-- | Wrap a Monad in this if you want partials to
39+-- be taken only from the default data files.
40+newtype WithDefaultPartials m a = WithDefaultPartials { runWithDefaultPartials :: m a }
41+ deriving (Functor, Applicative, Monad)
42+
43+-- | Wrap a Monad in this if you want partials to
44+-- be looked for locally (or, when the main template
45+-- is at a URL, via HTTP), falling back to default data files.
46+newtype WithPartials m a = WithPartials { runWithPartials :: m a }
47+ deriving (Functor, Applicative, Monad)
48+
49+instance PandocMonad m => TemplateMonad (WithDefaultPartials m) where
50+ getPartial fp = WithDefaultPartials $
51+ UTF8.toText <$> readDataFile fp
52+
53+instance PandocMonad m => TemplateMonad (WithPartials m) where
54+ getPartial fp = WithPartials $ getTemplate fp
55+
56+-- | Retrieve text for a template.
57+getTemplate :: PandocMonad m => FilePath -> m Text
58+getTemplate tp = UTF8.toText <$>
59+ ((do surl <- stSourceURL <$> getCommonState
60+ -- we don't want to look for templates remotely
61+ -- unless the full URL is specified:
62+ modifyCommonState $ \st -> st{
63+ stSourceURL = Nothing }
64+ (bs, _) <- fetchItem $ T.pack tp
65+ modifyCommonState $ \st -> st{
66+ stSourceURL = surl }
67+ return bs)
68+ `catchError`
69+ (\e -> case e of
70+ PandocResourceNotFound _ ->
71+ readDataFile ("templates" </> tp)
72+ _ -> throwError e))
2973
3074-- | Get default template for the specified writer.
3175getDefaultTemplate :: PandocMonad m
@@ -55,4 +99,15 @@ getDefaultTemplate writer = do
5599 let fname = "templates" </> "default" <.> T.unpack format
56100 UTF8.toText <$> readDataFile fname
57101
58−
102+-- | Get and compile default template for the specified writer.
103+-- Raise an error on compilation failure.
104+compileDefaultTemplate :: PandocMonad m
105+ => Text
106+ -> m (Template Text)
107+compileDefaultTemplate writer = do
108+ res <- getDefaultTemplate writer >>=
109+ runWithDefaultPartials .
110+ compileTemplate ("templates/default." <> T.unpack writer)
111+ case res of
112+ Left e -> throwError $ PandocTemplateError (T.pack e)
113+ Right t -> return t