@@ -43,10 +43,36 @@ used if @variable_name@ has a non-null value, otherwise the else section
4343is used.
4444-}
4545
46−module Text.Pandoc.Templates (renderTemplate) where
46+module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
4747
4848import Text.ParserCombinators.Parsec
4949import 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)
55+import Paths_pandoc
56+
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
62+ case ut of
63+ Right t -> return $ Right t
64+ Left _ -> getTemplateFromCabalDataDirectory format
65+
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
71+
72+getTemplateFromCabalDataDirectory :: String -> IO (Either E.IOException String)
73+getTemplateFromCabalDataDirectory format = E.try $ do
74+ templatePath <- getDataFileName $ "templates" </> format <.> "template"
75+ readFile templatePath
5076
5177-- | Renders a template
5278renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables