Skip to content

Commit 9af5b1c

Browse files
tarlebcagix
authored andcommitted
Lua: add function pandoc.template.get.
The function allows to specify a template with the same argument value that would be used with the `--template` command line parameter. Closes: #9854 Co-authored-by: Carsten Gips <[email protected]>
1 parent 11a2f78 commit 9af5b1c

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

doc/lua-filters.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6207,6 +6207,28 @@ Returns:
62076207

62086208
*Since: 2.17*
62096209

6210+
### get {#pandoc.template.get}
6211+
6212+
`get (filename)`
6213+
6214+
Retrieve text for a template.
6215+
6216+
This function first checks the resource paths for a file of this
6217+
name; if none is found, the `templates` directory in the user data
6218+
directory is checked. Returns the content of the file, or throws
6219+
an error if no file is found.
6220+
6221+
Parameters:
6222+
6223+
`filename`
6224+
: name of the template (string)
6225+
6226+
Returns:
6227+
6228+
- content of template file (string)
6229+
6230+
*Since: 3.2.1*
6231+
62106232
### meta_to_context {#pandoc.template.meta_to_context}
62116233

62126234
`meta_to_context (meta, blocks_writer, inlines_writer)`

pandoc-lua-engine/src/Text/Pandoc/Lua/Module/Template.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Text.Pandoc.Lua.Marshal.Template (typeTemplate, peekTemplate, pushTemplat
2121
import Text.Pandoc.Lua.PandocLua (PandocLua (unPandocLua), liftPandocLua)
2222
import Text.Pandoc.Writers.Shared (metaToContext')
2323
import Text.Pandoc.Templates
24-
( compileTemplate, getDefaultTemplate, renderTemplate
24+
( compileTemplate, getDefaultTemplate, getTemplate, renderTemplate
2525
, runWithPartials, runWithDefaultPartials )
2626

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

101+
, defun "get"
102+
### (unPandocLua . getTemplate)
103+
<#> stringParam "filename" "name of the template"
104+
=#> textResult "content of template file"
105+
#? T.unlines
106+
[ "Retrieve text for a template."
107+
, ""
108+
, "This function first checks the resource paths for a file of this"
109+
, "name; if none is found, the `templates` directory in the user data"
110+
, "directory is checked. Returns the content of the file, or throws"
111+
, "an error if no file is found."
112+
]
113+
`since` makeVersion [3,2,1]
114+
102115
, defun "meta_to_context"
103116
### (\meta blockWriterIdx inlineWriterIdx -> unPandocLua $ do
104117
let blockWriter blks = liftPandocLua $ do

pandoc-lua-engine/test/lua/module/pandoc-template.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local tasty = require 'tasty'
2+
local pandoc = require 'pandoc'
23
local template = require 'pandoc.template'
34

45
local assert = tasty.assert
@@ -24,8 +25,25 @@ return {
2425
)
2526
end),
2627
test('fails on unknown format', function ()
28+
local success, msg = pcall(function ()
29+
return pandoc.utils.type(template.default 'nosuchformat')
30+
end)
31+
assert.is_falsy(success)
32+
end),
33+
},
34+
group 'get' {
35+
test('is function', function ()
36+
assert.are_equal(type(template.get), 'function')
37+
end),
38+
test('searches the template data directory', function ()
39+
assert.are_equal(
40+
template.default 'html5',
41+
template.get 'default.html5'
42+
)
43+
end),
44+
test('fails on non-existent file', function ()
2745
local success, msg = pcall(function ()
28-
return pandoc.utils.type(template.default 'nosuchformat')
46+
return pandoc.utils.type(template.get 'nosuchfile.nope')
2947
end)
3048
assert.is_falsy(success)
3149
end),

0 commit comments

Comments
 (0)