git.iko.soy/typeable/pandoc
on master help sign in
Added getDefaultTemplate to Templates.

Exposed Templates module in API. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1688 788f1e2b-df1e-0410-8736-df70ead52e1b

commit2b1c01059974bbfdfc50f26c4e3c815742385de1 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:10 (17 years ago) parent8c74ceb
2 files changed+29−3
pandoc.cabal+2−2 src/Text/Pandoc/Templates.hs+27−1
pandoc.cabal+2−2
@@ -195,10 +195,10 @@ Library
195195 Text.Pandoc.Writers.RST,
196196 Text.Pandoc.Writers.MediaWiki,
197197 Text.Pandoc.Writers.RTF,
198− Text.Pandoc.Writers.S5
198+ Text.Pandoc.Writers.S5,
199+ Text.Pandoc.Templates
199200 Other-Modules: Text.Pandoc.XML,
200201 Text.Pandoc.TH,
201− Text.Pandoc.Templates
202202 Paths_pandoc
203203 Extensions: CPP, TemplateHaskell, FlexibleInstances
204204 Ghc-Options: -O2 -Wall
src/Text/Pandoc/Templates.hs+27−1
@@ -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