git.iko.soy/typeable/pandoc
on master help sign in
Added getTemplate to Text.Pandoc.Templates.

This allows the caller to select whether to allow user overrides from the user data directory (~/.pandoc). git-svn-id: https://pandoc.googlecode.com/svn/trunk@1803 788f1e2b-df1e-0410-8736-df70ead52e1b

commitfd7fe7d0007ce092cc99070c4be2a500545cc75a authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateMon 11 Jan 2010, 03:43 (17 years ago) parent58f32c1
2 files changed+31−7
changelog+4 src/Text/Pandoc/Templates.hs+27−7
changelog+4
@@ -2,6 +2,10 @@ pandoc (1.4.1)
22
33 [ John MacFarlane ]
44
5+ * Text.Pandoc.Templates now exports getTemplate, which differs from
6+ getDefaultTemplate in allowing the caller to select
7+ whether to allow user overrides from the ~/.pandoc directory.
8+
59 * Changed default of writerXeTeX to False.
610
711 * HTML writer: don't include empty UL if --toc but no sections.
src/Text/Pandoc/Templates.hs+27−7
@@ -66,27 +66,47 @@ You may optionally specify separators using @$sep$@:
6666
6767module Text.Pandoc.Templates ( renderTemplate
6868 , TemplateTarget
69+ , getTemplate
6970 , getDefaultTemplate) where
7071
7172import Text.ParserCombinators.Parsec
7273import Control.Monad (liftM, when, forM)
7374import qualified Control.Exception as E (try, IOException)
7475import System.FilePath
75−import Text.Pandoc.Shared (readDataFile)
7676import Data.List (intercalate, intersperse)
7777import Text.PrettyPrint (text, Doc)
7878import Text.XHtml (primHtml, Html)
7979import Data.ByteString.Lazy.UTF8 (ByteString, fromString)
80+import System.Directory
81+-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
82+-- So we use System.IO.UTF8 only if we have an earlier version
83+#if MIN_VERSION_base(4,2,0)
84+#else
85+import Prelude hiding ( readFile )
86+import System.IO.UTF8 ( readFile )
87+#endif
88+import Paths_pandoc (getDataFileName)
89+
90+-- | Get a template for the specified writer.
91+getTemplate :: Bool -- ^ Allow override from user's application data directory?
92+ -> String -- ^ Name of writer
93+ -> IO (Either E.IOException String)
94+getTemplate _ "native" = return $ Right ""
95+getTemplate user "s5" = getTemplate user "html"
96+getTemplate user "odt" = getTemplate user "opendocument"
97+getTemplate user writer = do
98+ let format = takeWhile (/='+') writer -- strip off "+lhs" if present
99+ userDir <- getAppUserDataDirectory "pandoc"
100+ let fname = "templates" </> format <.> "template"
101+ hasUserTemplate <- doesFileExist (userDir </> fname)
102+ E.try $ if user && hasUserTemplate
103+ then readFile $ userDir </> fname
104+ else getDataFileName fname >>= readFile
80105
81106-- | Get the default template, either from the application's user data
82107-- directory (~/.pandoc on unix) or from the cabal data directory.
83108getDefaultTemplate :: String -> IO (Either E.IOException String)
84−getDefaultTemplate "native" = return $ Right ""
85−getDefaultTemplate "s5" = getDefaultTemplate "html"
86−getDefaultTemplate "odt" = getDefaultTemplate "opendocument"
87−getDefaultTemplate format = do
88− let format' = takeWhile (/='+') format -- strip off "+lhs" if present
89− E.try $ readDataFile $ "templates" </> format' <.> "template"
109+getDefaultTemplate = getTemplate True
90110
91111data TemplateState = TemplateState Int [(String,String)]
92112