Skip to content

Commit

Permalink
add required variables template error to Text.Pandoc.Writers.HTML
Browse files Browse the repository at this point in the history
Allows an error to be thrown if a required variable is missing from the context.
This has only been applied to the HTML writer but could be added to all the
writers. I do not think this is a breaking change since existing templates do
not have required variables. It requires the new features in doctemplates pull
request jgm/doctemplates#19
  • Loading branch information
BebeSparkelSparkel committed Dec 18, 2022
1 parent 0d6c2e3 commit c7d6ec3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Text/Pandoc/Templates.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module Text.Pandoc.Templates ( Template
, WithPartials(..)
, compileTemplate
, renderTemplate
, renderTemplateM
, getTemplate
, getDefaultTemplate
, compileDefaultTemplate
) where

import System.FilePath ((<.>), (</>), takeFileName)
import Text.DocTemplates (Template, TemplateMonad(..), compileTemplate, renderTemplate)
import Text.DocTemplates (Template, TemplateMonad(..), compileTemplate, renderTemplate,
renderTemplateM)
import Text.Pandoc.Class.CommonState (CommonState(..))
import Text.Pandoc.Class.PandocMonad (PandocMonad, fetchItem,
getCommonState, modifyCommonState)
Expand Down
7 changes: 4 additions & 3 deletions src/Text/Pandoc/Writers/HTML.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Text.Pandoc.ImageSize
import Text.Pandoc.Options
import Text.Pandoc.Shared
import Text.Pandoc.Slides
import Text.Pandoc.Templates (renderTemplate)
import Text.Pandoc.Templates (renderTemplateM)
import Text.Pandoc.Walk
import Text.Pandoc.Writers.Math
import Text.Pandoc.Writers.Shared
Expand Down Expand Up @@ -249,8 +249,9 @@ writeHtmlString' st opts d = do
Just (x:_) -> takeBaseName $ T.unpack x
report $ NoTitleElement fallback
return $ resetField "pagetitle" (literal fallback) context
return $ render colwidth $ renderTemplate tpl
(defField "body" (layoutMarkup body) context')
document <- either (throwError . PandocTemplateError) pure $
renderTemplateM tpl (defField "body" (layoutMarkup body) context')
return $ render colwidth document

writeHtml' :: PandocMonad m => WriterState -> WriterOptions -> Pandoc -> m Html
writeHtml' st opts d =
Expand Down

0 comments on commit c7d6ec3

Please sign in to comment.