Added getDefaultTemplate to Templates.

Exposed Templates module in API.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1688 788f1e2b-df1e-0410-8736-df70ead52e1b
This commit is contained in:
fiddlosopher 2009-12-31 01:10:49 +00:00
parent 8c74cebbfc
commit 2b1c010599
2 changed files with 29 additions and 3 deletions

View file

@ -195,10 +195,10 @@ Library
Text.Pandoc.Writers.RST,
Text.Pandoc.Writers.MediaWiki,
Text.Pandoc.Writers.RTF,
Text.Pandoc.Writers.S5
Text.Pandoc.Writers.S5,
Text.Pandoc.Templates
Other-Modules: Text.Pandoc.XML,
Text.Pandoc.TH,
Text.Pandoc.Templates
Paths_pandoc
Extensions: CPP, TemplateHaskell, FlexibleInstances
Ghc-Options: -O2 -Wall

View file

@ -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