aboutsummaryrefslogtreecommitdiff
path: root/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
diff options
context:
space:
mode:
authorAlbert Krewinkel <[email protected]>2022-10-11 21:40:21 +0200
committerJohn MacFarlane <[email protected]>2022-10-11 14:03:08 -0700
commit78d7fc46febe3f2d5ebc1bea4761d115bb26f117 (patch)
treeffb4bccbdc58ee339cfdd44879b8fb153f4dc3a1 /pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
parent3cac33bdd2b2b69ac585cc37498deea49763c665 (diff)
Lua: add function `pandoc.template.apply`
The new function applies a context, containing variable assignments, to a template.
Diffstat (limited to 'pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs')
-rw-r--r--pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs22
1 files changed, 19 insertions, 3 deletions
diff --git a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
index 967fe31a8..be769e988 100644
--- a/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
+++ b/pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
@@ -12,11 +12,14 @@ module Text.Pandoc.Lua.Module.Template
) where
import HsLua
+import HsLua.Module.DocLayout (pushDoc)
import Text.Pandoc.Error (PandocError)
-import Text.Pandoc.Lua.Marshal.Template (pushTemplate)
+import Text.Pandoc.Lua.Marshal.Context (peekContext)
+import Text.Pandoc.Lua.Marshal.Template (peekTemplate, pushTemplate)
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua)
import Text.Pandoc.Templates
- (compileTemplate, getDefaultTemplate, runWithPartials, runWithDefaultPartials)
+ ( compileTemplate, getDefaultTemplate, renderTemplate
+ , runWithPartials, runWithDefaultPartials )
import qualified Data.Text as T
@@ -35,7 +38,20 @@ documentedModule = Module
-- | Template module functions.
functions :: [DocumentedFunction PandocError]
functions =
- [ defun "compile"
+ [ defun "apply"
+ ### liftPure2 renderTemplate
+ <#> parameter peekTemplate "pandoc Template" "template" "template to apply"
+ <#> parameter peekContext "table" "context" "variable values"
+ =#> functionResult pushDoc "Doc" "rendered template"
+ #? T.unlines
+ [ "Applies a context with variable assignments to a template,"
+ , "returning the rendered template. The `context` parameter must be a"
+ , "table with variable names as keys and [Doc], string, boolean, or"
+ , "table as values, where the table can be either be a list of the"
+ , "aforementioned types, or a nested context."
+ ]
+
+ , defun "compile"
### (\template mfilepath -> unPandocLua $
case mfilepath of
Just fp -> runWithPartials (compileTemplate fp template)