summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2009-12-31 01:10:49 +0000
committerfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>2009-12-31 01:10:49 +0000
commit2b1c01059974bbfdfc50f26c4e3c815742385de1 (patch)
tree22605ffcf03db369235d080f00b7c0481fb3a099 /src
parent8c74cebbfc52e0441d2632470e367e3edb99f20b (diff)
Added getDefaultTemplate to Templates.
Exposed Templates module in API. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1688 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src')
-rw-r--r--src/Text/Pandoc/Templates.hs28
1 files changed, 27 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Templates.hs b/src/Text/Pandoc/Templates.hs
index 7cfb697cf..887696159 100644
--- a/src/Text/Pandoc/Templates.hs
+++ b/src/Text/Pandoc/Templates.hs
@@ -43,10 +43,36 @@ used if @variable_name@ has a non-null value, otherwise the else section
is used.
-}
-module Text.Pandoc.Templates (renderTemplate) where
+module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
import Text.ParserCombinators.Parsec
import Control.Monad (liftM)
+import qualified Control.Exception as E (try, IOException)
+import System.FilePath
+import System.Directory
+import Prelude hiding (readFile)
+import System.IO.UTF8 (readFile)
+import Paths_pandoc
+
+-- | Get the default template, either from the application's user data
+-- directory (~/.pandoc on unix) or from the cabal data directory.
+getDefaultTemplate :: String -> IO (Either E.IOException String)
+getDefaultTemplate format = do
+ ut <- getTemplateFromUserDataDirectory format
+ case ut of
+ Right t -> return $ Right t
+ Left _ -> getTemplateFromCabalDataDirectory format
+
+getTemplateFromUserDataDirectory :: String -> IO (Either E.IOException String)
+getTemplateFromUserDataDirectory format = E.try $ do
+ userDir <- getAppUserDataDirectory "pandoc"
+ let templatePath = userDir </> "templates" </> format <.> "template"
+ readFile templatePath
+
+getTemplateFromCabalDataDirectory :: String -> IO (Either E.IOException String)
+getTemplateFromCabalDataDirectory format = E.try $ do
+ templatePath <- getDataFileName $ "templates" </> format <.> "template"
+ readFile templatePath
-- | Renders a template
renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables