2017-03-04 13:03:41 +01:00
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2017-10-27 23:13:55 -07:00
|
|
|
|
2017-03-04 13:03:41 +01:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
2009-12-31 01:08:38 +00:00
|
|
|
{-
|
2017-05-13 23:30:13 +02:00
|
|
|
Copyright (C) 2009-2017 John MacFarlane <jgm@berkeley.edu>
|
2009-12-31 01:08:38 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
-}
|
|
|
|
|
|
|
|
{- |
|
|
|
|
Module : Text.Pandoc.Templates
|
2017-05-13 23:30:13 +02:00
|
|
|
Copyright : Copyright (C) 2009-2017 John MacFarlane
|
2009-12-31 01:08:38 +00:00
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
Stability : alpha
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
A simple templating system with variable substitution and conditionals.
|
2009-12-31 01:15:33 +00:00
|
|
|
|
2009-12-31 01:08:38 +00:00
|
|
|
-}
|
|
|
|
|
2017-06-20 22:41:56 +02:00
|
|
|
module Text.Pandoc.Templates ( module Text.DocTemplates
|
2013-05-10 22:53:35 -07:00
|
|
|
, renderTemplate'
|
2017-06-20 22:41:56 +02:00
|
|
|
, getDefaultTemplate
|
|
|
|
) where
|
2009-12-31 01:08:38 +00:00
|
|
|
|
2017-06-20 22:41:56 +02:00
|
|
|
import Control.Monad.Except (throwError)
|
2017-03-04 13:03:41 +01:00
|
|
|
import Data.Aeson (ToJSON (..))
|
2013-05-16 23:00:58 -07:00
|
|
|
import qualified Data.Text as T
|
2017-03-04 13:03:41 +01:00
|
|
|
import System.FilePath ((<.>), (</>))
|
|
|
|
import Text.DocTemplates (Template, TemplateTarget, applyTemplate,
|
|
|
|
compileTemplate, renderTemplate, varListToJSON)
|
2017-10-27 20:28:29 -07:00
|
|
|
import Text.Pandoc.Class (PandocMonad, readDataFile)
|
2017-06-20 22:41:56 +02:00
|
|
|
import Text.Pandoc.Error
|
2017-07-19 21:46:28 +02:00
|
|
|
import qualified Text.Pandoc.UTF8 as UTF8
|
2010-01-11 03:43:49 +00:00
|
|
|
|
2010-01-18 05:06:08 +00:00
|
|
|
-- | Get default template for the specified writer.
|
2017-07-19 21:46:28 +02:00
|
|
|
getDefaultTemplate :: PandocMonad m
|
2017-08-10 23:29:25 -07:00
|
|
|
=> String -- ^ Name of writer
|
2017-07-19 21:46:28 +02:00
|
|
|
-> m String
|
2017-08-10 23:29:25 -07:00
|
|
|
getDefaultTemplate writer = do
|
2015-01-05 14:41:35 +11:00
|
|
|
let format = takeWhile (`notElem` ("+-" :: String)) writer -- strip off extensions
|
2012-08-09 20:19:06 -07:00
|
|
|
case format of
|
2017-07-19 21:46:28 +02:00
|
|
|
"native" -> return ""
|
|
|
|
"json" -> return ""
|
|
|
|
"docx" -> return ""
|
|
|
|
"fb2" -> return ""
|
2017-08-10 23:29:25 -07:00
|
|
|
"odt" -> getDefaultTemplate "opendocument"
|
|
|
|
"html" -> getDefaultTemplate "html5"
|
|
|
|
"docbook" -> getDefaultTemplate "docbook5"
|
|
|
|
"epub" -> getDefaultTemplate "epub3"
|
2017-09-08 10:48:02 -07:00
|
|
|
"beamer" -> getDefaultTemplate "latex"
|
2017-08-10 23:29:25 -07:00
|
|
|
"markdown_strict" -> getDefaultTemplate "markdown"
|
|
|
|
"multimarkdown" -> getDefaultTemplate "markdown"
|
|
|
|
"markdown_github" -> getDefaultTemplate "markdown"
|
|
|
|
"markdown_mmd" -> getDefaultTemplate "markdown"
|
|
|
|
"markdown_phpextra" -> getDefaultTemplate "markdown"
|
|
|
|
"gfm" -> getDefaultTemplate "commonmark"
|
2012-08-09 20:19:06 -07:00
|
|
|
_ -> let fname = "templates" </> "default" <.> format
|
2017-08-10 23:29:25 -07:00
|
|
|
in UTF8.toString <$> readDataFile fname
|
2009-12-31 01:14:04 +00:00
|
|
|
|
2017-06-20 22:41:56 +02:00
|
|
|
-- | Like 'applyTemplate', but runs in PandocMonad and
|
|
|
|
-- raises an error if compilation fails.
|
|
|
|
renderTemplate' :: (PandocMonad m, ToJSON a, TemplateTarget b)
|
|
|
|
=> String -> a -> m b
|
2017-10-27 23:13:55 -07:00
|
|
|
renderTemplate' template context =
|
2017-06-20 22:41:56 +02:00
|
|
|
case applyTemplate (T.pack template) context of
|
|
|
|
Left e -> throwError (PandocTemplateError e)
|
|
|
|
Right r -> return r
|