Skip to content

Commit ebe36ab

Browse files
authored
Merge pull request #13246 from quarto-dev/feature/lua-api-shortcodes
new Lua APIs for programmatic access to var and meta shortcode results
2 parents 2a2eefc + 5c3f163 commit ebe36ab

File tree

12 files changed

+123
-1
lines changed

12 files changed

+123
-1
lines changed

news/changelog-1.8.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,13 @@ All changes included in 1.8:
7676

7777
- ([#12615](https://github.com/quarto-dev/quarto-cli/pull/12615)): Adds `algorithm` to theorem environments. (author: @jeremy9959)
7878

79-
## Lua Filters
79+
## Lua Filters and API
8080

8181
- ([#12727](https://github.com/quarto-dev/quarto-cli/issues/12727)): Do not crash in the presence of malformed tabset contents.
8282
- ([#12806](https://github.com/quarto-dev/quarto-cli/pull/12806)): Use pandoc APIs to handle codepage conversion on Windows.
8383
- ([#12811](https://github.com/quarto-dev/quarto-cli/pull/12811)): Add support for YouTube Shorts in `video` shortcode.
8484
- ([#13128](https://github.com/quarto-dev/quarto-cli/issues/13128)): Meta shortcode could crash on bad input.
85+
- ([#13246](https://github.com/quarto-dev/quarto-cli/pull/13246)): Add `quarto.variables.get()` and `quarto.metadata.get()` APIs.
8586

8687
## Commands
8788

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}

0 commit comments

Comments
 (0)