module Text.Pandoc.Templates ( renderTemplate
, renderTemplate'
, TemplateTarget
, varListToJSON
, compileTemplate
, Template
, getDefaultTemplate ) where
import Text.DocTemplates (Template, TemplateTarget, compileTemplate,
renderTemplate, applyTemplate,
varListToJSON)
import Data.Aeson (ToJSON(..))
import qualified Data.Text as T
import System.FilePath ((</>), (<.>))
import qualified Control.Exception.Extensible as E (try, IOException)
import Text.Pandoc.Shared (readDataFileUTF8)
getDefaultTemplate :: (Maybe FilePath)
-> String
-> IO (Either E.IOException String)
getDefaultTemplate user writer = do
let format = takeWhile (`notElem` ("+-" :: String)) writer
case format of
"native" -> return $ Right ""
"json" -> return $ Right ""
"docx" -> return $ Right ""
"fb2" -> return $ Right ""
"odt" -> getDefaultTemplate user "opendocument"
"markdown_strict" -> getDefaultTemplate user "markdown"
"multimarkdown" -> getDefaultTemplate user "markdown"
"markdown_github" -> getDefaultTemplate user "markdown"
"markdown_mmd" -> getDefaultTemplate user "markdown"
"markdown_phpextra" -> getDefaultTemplate user "markdown"
_ -> let fname = "templates" </> "default" <.> format
in E.try $ readDataFileUTF8 user fname
renderTemplate' :: (ToJSON a, TemplateTarget b) => String -> a -> b
renderTemplate' template = either error id . applyTemplate (T.pack template)