Skip to content

Commit f4a4723

Browse files
committed
new Lua APIs for programmatic access to var and meta shortcode results
1 parent 1ea5a78 commit f4a4723

File tree

11 files changed

+121
-0
lines changed

11 files changed

+121
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---@meta
2+
3+
quarto.metadata = {}
4+
5+
--[[
6+
Return the value of a metadata entry in Quarto metadata as a pandoc.MetaValue.
7+
Return nil if the key (or any of its subcomponents) are missing.
8+
9+
This is the Lua equivalent of the {{< meta key >}} shortcode in Quarto documents.
10+
]]
11+
---@param key string metadata key, possibly with nested keys separated by dots
12+
---@return pandoc.MetaValue | nil
13+
function quarto.metadata.get(key) end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---@meta
2+
3+
quarto.variables = {}
4+
5+
--[[
6+
Return the value of a variable in _variables.yml as a string, or nil if name is missing.
7+
8+
This is the Lua equivalent of the {{< var name >}} shortcode in Quarto documents.
9+
]]
10+
---@param name string name of variable
11+
---@return string | nil
12+
function quarto.variables.get(name) end

src/resources/pandoc/datadir/init.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,20 @@ quarto = {
10311031
end
10321032
end,
10331033
},
1034+
metadata = {
1035+
get = function(key)
1036+
return option(key, nil)
1037+
end
1038+
},
1039+
variables = {
1040+
get = function(name)
1041+
local value = var(name, nil)
1042+
if value then
1043+
value = pandoc.utils.stringify(value)
1044+
end
1045+
return value
1046+
end
1047+
}
10341048
}
10351049

10361050
-- alias old names for backwards compatibility
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.quarto/
2+
3+
/.luarc.json
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
meta_from_metadata: value_from_metadata
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
project:
2+
type: website
3+
4+
website:
5+
title: "test-lua-shortcode-apis"
6+
navbar:
7+
left:
8+
- href: index.qmd
9+
text: Home
10+
- about.qmd
11+
12+
format:
13+
html:
14+
theme:
15+
- cosmo
16+
- brand
17+
css: styles.css
18+
toc: true
19+
20+
21+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var1: var_value
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: "About"
3+
---
4+
5+
About this site
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: "test-lua-shortcode-apis"
3+
meta1: meta_value
4+
meta2: 2
5+
meta3:
6+
- foo
7+
- bar
8+
meta4:
9+
key1: value1
10+
key2: value2
11+
filters:
12+
- set_span_values.lua
13+
_quarto:
14+
tests:
15+
html:
16+
ensureFileRegexMatches:
17+
-
18+
- meta_value
19+
- var_value
20+
---
21+
22+
This is a Quarto website.
23+
24+
To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
25+
26+
[]{#span1}, []{#span2}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function Span(span)
2+
-- print(quarto.variables.get("var1"))
3+
-- print(quarto.metadata.get("meta1"))
4+
if span.identifier == "span1" then
5+
span.content = quarto.utils.string_to_inlines(
6+
pandoc.utils.stringify(quarto.variables.get("var1")))
7+
return span
8+
elseif span.identifier == "span2" then
9+
span.content = quarto.utils.string_to_inlines(
10+
pandoc.utils.stringify(quarto.metadata.get("meta1")))
11+
return span
12+
end
13+
end
14+
15+
function Pandoc(doc)
16+
local function get_meta(key)
17+
return pandoc.utils.stringify(quarto.metadata.get(key))
18+
end
19+
assert(type(quarto.metadata.get("meta3")) == "table")
20+
assert(get_meta("meta2") == "2")
21+
assert(get_meta("meta4.key1") == "value1")
22+
assert(get_meta("meta4.key2") == "value2")
23+
assert(get_meta("meta_from_metadata") == "value_from_metadata")
24+
end

0 commit comments

Comments
 (0)