-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathprerelease.lua
More file actions
152 lines (135 loc) · 4.75 KB
/
prerelease.lua
File metadata and controls
152 lines (135 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-- Version-aware shortcodes for prerelease content.
--
-- prerelease-docs-url:
-- {{< prerelease-docs-url 1.9 >}}
-- Returns "prerelease." when the referenced version's docs live on
-- prerelease.quarto.org, or "" when they're on quarto.org.
--
-- prerelease-callout:
-- {{< prerelease-callout 1.9 >}}
-- Shows a "Pre-release Feature" callout when the referenced version is
-- unreleased. Shows nothing once the version is released.
--
-- {{< prerelease-callout 1.9 type="blog" >}}
-- Blog mode: shows "Pre-release Feature" callout when unreleased,
-- switches to "Quarto X.Y Feature" callout once released.
--
-- Both shortcodes use the `version` key from _quarto.yml metadata and
-- the `prerelease-docs` profile to determine whether a version is released.
--- Strip surrounding quotes from a shortcode argument.
local function strip_quotes(s)
return s:gsub('^"(.*)"$', '%1'):gsub("^'(.*)'$", '%1')
end
--- Parse the version argument and site version metadata.
-- Returns ref_version, branch_version (as pandoc.types.Version), or
-- nil plus an error Blocks/Inlines on failure.
local function parse_versions(shortcode_name, args, meta, context)
local ref_str = quarto.shortcode.read_arg(args, 1)
if ref_str == nil then
return nil, nil, quarto.shortcode.error_output(
shortcode_name,
"requires a version argument, e.g. {{< " .. shortcode_name .. " 1.9 >}}",
context
)
end
ref_str = strip_quotes(ref_str)
local version_str = meta["version"] and pandoc.utils.stringify(meta["version"]) or nil
if not version_str or version_str == "" then
return nil, nil, quarto.shortcode.error_output(
shortcode_name,
"missing 'version' in document metadata",
context
)
end
local ok_branch, branch_version = pcall(pandoc.types.Version, version_str)
if not ok_branch then
return nil, nil, quarto.shortcode.error_output(
shortcode_name,
"invalid metadata version '" .. version_str .. "'",
context
)
end
local ok_ref, ref_version = pcall(pandoc.types.Version, ref_str)
if not ok_ref then
return nil, nil, quarto.shortcode.error_output(
shortcode_name,
"invalid version argument '" .. ref_str .. "'",
context
)
end
return ref_version, branch_version, nil
end
--- Check whether a referenced version is unreleased.
-- On prerelease site (profile prerelease-docs): ref >= site version
-- On main site: ref > site version
local function is_unreleased(ref_version, branch_version)
if quarto.project.profile:includes("prerelease-docs") then
return ref_version >= branch_version
else
return ref_version > branch_version
end
end
--- Parse a markdown string into pandoc Blocks.
local function md_to_blocks(md)
return pandoc.read(md, "markdown").blocks
end
-- Shortcode: prerelease-docs-url
local function docs_url_handler(args, kwargs, meta, raw_args, context)
local ref_version, branch_version, err = parse_versions(
"prerelease-docs-url", args, meta, context
)
if err then return err end
-- On the prerelease site, always link to prerelease
if quarto.project.profile:includes("prerelease-docs") then
return pandoc.Str("prerelease.")
end
if ref_version <= branch_version then
return pandoc.Str("")
else
return pandoc.Str("prerelease.")
end
end
-- Shortcode: prerelease-callout
local function callout_handler(args, kwargs, meta, raw_args, context)
local ref_version, branch_version, err = parse_versions(
"prerelease-callout", args, meta, context
)
if err then return err end
local ref_str = strip_quotes(quarto.shortcode.read_arg(args, 1))
local callout_type = kwargs["type"] or ""
local is_blog = callout_type == "blog"
local unreleased = is_unreleased(ref_version, branch_version)
if unreleased then
-- Pre-release callout (both feature docs and blog)
local content = md_to_blocks(
"This feature is new in the upcoming Quarto " .. ref_str .. " release. " ..
"To use the feature now, you'll need to " ..
"[download and install](/docs/download/prerelease.qmd) " ..
"the Quarto pre-release."
)
return quarto.Callout({
type = "note",
title = "Pre-release Feature",
content = content,
})
end
if is_blog then
-- Released blog callout
local content = md_to_blocks(
"This post is part of a series highlighting new features in the " ..
ref_str .. " release of Quarto. Get the latest release on the " ..
"[download page](/docs/download/index.qmd)."
)
return quarto.Callout({
type = "note",
title = "Quarto " .. ref_str .. " Feature",
content = content,
})
end
-- Feature docs, already released: show nothing
return pandoc.Blocks({})
end
return {
["prerelease-docs-url"] = docs_url_handler,
["prerelease-callout"] = callout_handler,
}