diff options
author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2010-01-11 03:43:49 +0000 |
---|---|---|
committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2010-01-11 03:43:49 +0000 |
commit | fd7fe7d0007ce092cc99070c4be2a500545cc75a (patch) | |
tree | d8fe9d88a1253bc12b87244f1cabb73b63d980f7 /src | |
parent | 58f32c1928e21abd4d36e75db9e2fba1e7a14287 (diff) | |
download | pandoc-fd7fe7d0007ce092cc99070c4be2a500545cc75a.tar.gz |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/Text/Pandoc/Templates.hs | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/Text/Pandoc/Templates.hs b/src/Text/Pandoc/Templates.hs index 03055735a..850d6a08c 100644 --- a/src/Text/Pandoc/Templates.hs +++ b/src/Text/Pandoc/Templates.hs @@ -66,27 +66,47 @@ You may optionally specify separators using @$sep$@: module Text.Pandoc.Templates ( renderTemplate , TemplateTarget + , getTemplate , getDefaultTemplate) where import Text.ParserCombinators.Parsec import Control.Monad (liftM, when, forM) import qualified Control.Exception as E (try, IOException) import System.FilePath -import Text.Pandoc.Shared (readDataFile) import Data.List (intercalate, intersperse) import Text.PrettyPrint (text, Doc) import Text.XHtml (primHtml, Html) import Data.ByteString.Lazy.UTF8 (ByteString, fromString) +import System.Directory +-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv +-- So we use System.IO.UTF8 only if we have an earlier version +#if MIN_VERSION_base(4,2,0) +#else +import Prelude hiding ( readFile ) +import System.IO.UTF8 ( readFile ) +#endif +import Paths_pandoc (getDataFileName) + +-- | Get a template for the specified writer. +getTemplate :: Bool -- ^ Allow override from user's application data directory? + -> String -- ^ Name of writer + -> IO (Either E.IOException String) +getTemplate _ "native" = return $ Right "" +getTemplate user "s5" = getTemplate user "html" +getTemplate user "odt" = getTemplate user "opendocument" +getTemplate user writer = do + let format = takeWhile (/='+') writer -- strip off "+lhs" if present + userDir <- getAppUserDataDirectory "pandoc" + let fname = "templates" </> format <.> "template" + hasUserTemplate <- doesFileExist (userDir </> fname) + E.try $ if user && hasUserTemplate + then readFile $ userDir </> fname + else getDataFileName fname >>= readFile -- | 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 "native" = return $ Right "" -getDefaultTemplate "s5" = getDefaultTemplate "html" -getDefaultTemplate "odt" = getDefaultTemplate "opendocument" -getDefaultTemplate format = do - let format' = takeWhile (/='+') format -- strip off "+lhs" if present - E.try $ readDataFile $ "templates" </> format' <.> "template" +getDefaultTemplate = getTemplate True data TemplateState = TemplateState Int [(String,String)] |