Skip to content

Commit

Permalink
Lua: add function pandoc.template.get.
Browse files Browse the repository at this point in the history
The function allows to specify a template with the same argument value
that would be used with the `--template` command line parameter.

Closes: jgm#9854

Co-authored-by: Carsten Gips <cagix@hsbi.de>
  • Loading branch information
tarleb and cagix committed Jun 7, 2024
1 parent a0ba712 commit 82cf979
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
22 changes: 22 additions & 0 deletions doc/lua-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6207,6 +6207,28 @@ Returns:

*Since: 2.17*

### get {#pandoc.template.get}

`get (filename)`

Retrieve text for a template.

This function first checks the resource paths for a file of this
name; if none is found, the `templates` directory in the user data
directory is checked. Returns the content of the file, or throws
an error if no file is found.

Parameters:

`filename`
: name of the template (string)

Returns:

- content of template file (string)

*Since: 3.2.1*

### meta_to_context {#pandoc.template.meta_to_context}

`meta_to_context (meta, blocks_writer, inlines_writer)`
Expand Down
19 changes: 16 additions & 3 deletions pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Text.Pandoc.Lua.Marshal.Template (typeTemplate, peekTemplate, pushTemplat
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua)
import Text.Pandoc.Writers.Shared (metaToContext')
import Text.Pandoc.Templates
( compileTemplate, getDefaultTemplate, renderTemplate
( compileTemplate, getDefaultTemplate, getTemplate, renderTemplate
, runWithPartials, runWithDefaultPartials )

import qualified Data.Text as T
Expand Down Expand Up @@ -91,14 +91,27 @@ functions =
<#> opt (textParam "writer"
("name of the writer for which the template should be " <>
"retrieved; defaults to the global `FORMAT`."))
=#> functionResult pushText "string"
"raw template"
=#> functionResult pushText "string" "raw template"
#? T.unlines
[ "Returns the default template for a given writer as a string. An"
, "error is thrown if no such template can be found."
]
`since` makeVersion [2,17]

, defun "get"
### (unPandocLua . getTemplate)
<#> stringParam "filename" "name of the template"
=#> textResult "content of template file"
#? T.unlines
[ "Retrieve text for a template."
, ""
, "This function first checks the resource paths for a file of this"
, "name; if none is found, the `templates` directory in the user data"
, "directory is checked. Returns the content of the file, or throws"
, "an error if no file is found."
]
`since` makeVersion [3,2,1]

, defun "meta_to_context"
### (\meta blockWriterIdx inlineWriterIdx -> unPandocLua $ do
let blockWriter blks = liftPandocLua $ do
Expand Down
20 changes: 19 additions & 1 deletion pandoc-lua-engine/test/lua/module/pandoc-template.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local tasty = require 'tasty'
local pandoc = require 'pandoc'
local template = require 'pandoc.template'

local assert = tasty.assert
Expand All @@ -24,8 +25,25 @@ return {
)
end),
test('fails on unknown format', function ()
local success, msg = pcall(function ()
return pandoc.utils.type(template.default 'nosuchformat')
end)
assert.is_falsy(success)
end),
},
group 'get' {
test('is function', function ()
assert.are_equal(type(template.get), 'function')
end),
test('searches the template data directory', function ()
assert.are_equal(
template.default 'html5',
template.get 'default.html5'
)
end),
test('fails on non-existent file', function ()
local success, msg = pcall(function ()
return pandoc.utils.type(template.default 'nosuchformat')
return pandoc.utils.type(template.get 'nosuchfile.nope')
end)
assert.is_falsy(success)
end),
Expand Down

0 comments on commit 82cf979

Please sign in to comment.