Skip to content

Commit 6a3d543

Browse files
committed
new _quarto.ast.walk_meta helper function, walk meta in ast normalization
1 parent c5e85c0 commit 6a3d543

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/resources/filters/ast/customnodes.lua

+39
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function run_emulated_filter(doc, filter)
7979
return node
8080
else
8181
-- luacov: disable
82+
quarto.utils.dump(node)
8283
internal_error()
8384
-- luacov: enable
8485
end
@@ -259,6 +260,42 @@ function create_emulated_node(t, tbl, context, forwarder)
259260
return result, custom_node_data[id]
260261
end
261262

263+
-- walk_meta walks a Pandoc Meta object, applying the filter to each node
264+
-- and recursing on lists and objects. It mutates the meta object in place
265+
-- and returns it.
266+
--
267+
-- It performs slightly more work than a regular walk filter because of the
268+
-- ambiguity around single-element lists and objects.
269+
function walk_meta(meta, filter)
270+
local skip = {
271+
["nil"] = true,
272+
number = true,
273+
boolean = true,
274+
string = true,
275+
["function"] = true,
276+
}
277+
local iterate = {
278+
Meta = true,
279+
List = true,
280+
}
281+
local function walk(obj)
282+
local t = type(obj)
283+
if skip[t] then
284+
return obj
285+
end
286+
local pt = quarto.utils.type(obj)
287+
if iterate[pt] then
288+
for k, v in pairs(obj) do
289+
obj[k] = walk(v)
290+
end
291+
else
292+
return _quarto.ast.walk(obj, filter)
293+
end
294+
return obj
295+
end
296+
return walk(meta)
297+
end
298+
262299
_quarto.ast = {
263300
vault = {
264301
_uuid = "3ade8a4a-fb1d-4a6c-8409-ac45482d5fc9",
@@ -495,6 +532,8 @@ _quarto.ast = {
495532

496533
walk = run_emulated_filter,
497534

535+
walk_meta = walk_meta,
536+
498537
writer_walk = function(doc, filter)
499538
local old_custom_walk = filter.Custom
500539
local function custom_walk(node)

src/resources/filters/normalize/extractquartodom.lua

+18-5
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,33 @@ function parse_md_in_html_rawblocks()
2727
end
2828
end,
2929
RawBlock = function(raw)
30+
local result
3031
if raw.format == "pandoc-native" then
31-
return pandoc.read(raw.text, "native").blocks
32+
result = pandoc.read(raw.text, "native").blocks
3233
elseif raw.format == "pandoc-json" then
33-
return pandoc.read(raw.text, "json").blocks
34+
result = pandoc.read(raw.text, "json").blocks
35+
else
36+
return raw
3437
end
38+
return result
3539
end,
3640
RawInline = function(raw)
41+
local result
3742
if raw.format == "pandoc-native" then
38-
return quarto.utils.as_inlines(pandoc.read(raw.text, "native").blocks)
43+
result = quarto.utils.as_inlines(pandoc.read(raw.text, "native").blocks)
3944
elseif raw.format == "pandoc-json" then
4045
-- let's try to be minimally smart here, and handle lists differently from a single top-level element
41-
return quarto.utils.as_inlines(pandoc.read(raw.text, "json").blocks)
46+
result = quarto.utils.as_inlines(pandoc.read(raw.text, "json").blocks)
47+
else
48+
return raw
4249
end
43-
end
50+
return result
51+
end,
52+
-- Meta = function(meta)
53+
-- local filter = parse_md_in_html_rawblocks()
54+
-- local result = _quarto.ast.walk_meta(meta, filter)
55+
-- return result
56+
-- end,
4457
}
4558
end
4659

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
format: html
3+
key-good: "`Str \"path-to-directory-with--two-dashes/file\"`{=pandoc-native}"
4+
key-bad: path-to-directory-with--two-dashes/file
5+
_quarto:
6+
tests:
7+
html:
8+
ensureFileRegexMatches:
9+
- ["--"]
10+
- ["–"]
11+
---
12+
13+
Hello {{< meta key-good >}}. (This would fail with `{{{< meta key-bad >}}}`)

0 commit comments

Comments
 (0)