@@ -43,10 +43,36 @@ used if @variable_name@ has a non-null value, otherwise the else section
46−module Text.Pandoc.Templates (renderTemplate) where
46+module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
48import Text.ParserCombinators.Parsec
48import Text.ParserCombinators.Parsec
49import Control.Monad (liftM)
49import Control.Monad (liftM)
50+import qualified Control.Exception as E (try, IOException)
51+import System.FilePath
52+import System.Directory
53+import Prelude hiding (readFile)
54+import System.IO.UTF8 (readFile)
57+-- | Get the default template, either from the application's user data
58+-- directory (~/.pandoc on unix) or from the cabal data directory.
59+getDefaultTemplate :: String -> IO (Either E.IOException String)
60+getDefaultTemplate format = do
61+ ut <- getTemplateFromUserDataDirectory format
63+ Right t -> return $ Right t
64+ Left _ -> getTemplateFromCabalDataDirectory format
66+getTemplateFromUserDataDirectory :: String -> IO (Either E.IOException String)
67+getTemplateFromUserDataDirectory format = E.try $ do
68+ userDir <- getAppUserDataDirectory "pandoc"
69+ let templatePath = userDir </> "templates" </> format <.> "template"
70+ readFile templatePath
72+getTemplateFromCabalDataDirectory :: String -> IO (Either E.IOException String)
73+getTemplateFromCabalDataDirectory format = E.try $ do
74+ templatePath <- getDataFileName $ "templates" </> format <.> "template"
75+ readFile templatePath
51-- | Renders a template
77-- | Renders a template
52renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables
78renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables