git.iko.soy/typeable/pandoc
on master help sign in
Added getDefaultTemplate to Templates.
2b1c010fiddlosopher
pandoc.cabal+2−2
@@ -195,10 +195,10 @@ Library
195 Text.Pandoc.Writers.RST,
195 Text.Pandoc.Writers.RST,
196 Text.Pandoc.Writers.MediaWiki,
196 Text.Pandoc.Writers.MediaWiki,
197 Text.Pandoc.Writers.RTF,
197 Text.Pandoc.Writers.RTF,
198− Text.Pandoc.Writers.S5
198+ Text.Pandoc.Writers.S5,
199+ Text.Pandoc.Templates
199 Other-Modules: Text.Pandoc.XML,
200 Other-Modules: Text.Pandoc.XML,
200 Text.Pandoc.TH,
201 Text.Pandoc.TH,
201− Text.Pandoc.Templates
202 Paths_pandoc
202 Paths_pandoc
203 Extensions: CPP, TemplateHaskell, FlexibleInstances
203 Extensions: CPP, TemplateHaskell, FlexibleInstances
204 Ghc-Options: -O2 -Wall
204 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
43is used.
43is used.
44-}
44-}
45
45
46−module Text.Pandoc.Templates (renderTemplate) where
46+module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
47
47
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)
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
50
76
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