Skip to content

Commit 6de2ca7

Browse files
authored
Merge pull request #11525 from tarleb/avoid-implicit-type-conversions
Reduce implicit type conversions in Lua
2 parents 047b5aa + 3a0dc9e commit 6de2ca7

File tree

22 files changed

+93
-72
lines changed

22 files changed

+93
-72
lines changed

src/resources/filters/ast/customnodes.lua

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,21 @@ _quarto.ast = {
336336
return
337337
end
338338
local node = node_accessor(table)
339-
local t = pandoc.utils.type(value)
340-
quarto_assert(t ~= 'Div' and t ~= 'Span', "")
339+
local valtype = pandoc.utils.type(value)
340+
quarto_assert(valtype ~= 'Div' and valtype ~= 'Span', "")
341341
if index > #node.content then
342342
_quarto.ast.grow_scaffold(node, index)
343343
end
344-
local pt = pandoc.utils.type(value)
345-
if pt == "Block" or pt == "Inline" then
346-
node.content[index].content = {value}
344+
local inner_node = node.content[index]
345+
local innertype = pandoc.utils.type(inner_node)
346+
if innertype == 'Block' then
347+
inner_node.content = quarto.utils.as_blocks(value)
348+
elseif innertype == 'Inline' then
349+
inner_node.content = quarto.utils.as_inlines(value)
347350
else
348-
node.content[index].content = value
351+
warn(debug.traceback(
352+
'Cannot find the right content type for value ' .. valtype))
353+
inner_node.content = value
349354
end
350355
end
351356
}
@@ -416,13 +421,15 @@ _quarto.ast = {
416421
-- luacov: enable
417422
end
418423

419-
local forwarder = { }
424+
local forwarder
420425
if tisarray(handler.slots) then
426+
forwarder = pandoc.List{}
421427
for i, slot in ipairs(handler.slots) do
422428
forwarder[slot] = i
423429
end
424-
else
425-
forwarder = handler.slots
430+
elseif handler.slots ~= nil then
431+
warn('Expected `slots` to be either an array or nil, got ' ..
432+
tostring(handler.slots))
426433
end
427434

428435
quarto[handler.ast_name] = function(params)

src/resources/filters/crossref/equations.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function process_equations(blockEl)
2020
end
2121

2222
local mathInlines = nil
23-
local targetInlines = pandoc.List()
23+
local targetInlines = pandoc.Inlines{}
2424

2525
for i, el in ipairs(inlines) do
2626

src/resources/filters/crossref/index.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ end
6565
-- add an entry to the index
6666
function indexAddEntry(label, parent, order, caption, appendix)
6767
if caption ~= nil then
68-
caption = pandoc.List(caption)
68+
caption = quarto.utils.as_blocks(caption)
6969
else
70-
caption = pandoc.List({})
70+
caption = pandoc.Blocks({})
7171
end
7272
crossref.index.entries[label] = {
7373
parent = parent,

src/resources/filters/crossref/preprocess.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function crossref_mark_subfloats()
77
return {
88
traverse = "topdown",
99
FloatRefTarget = function(float)
10-
float.content = _quarto.ast.walk(float.content, {
10+
float.content = _quarto.ast.walk(float.content or pandoc.Blocks{}, {
1111
FloatRefTarget = function(subfloat)
1212
float.has_subfloats = true
1313
crossref.subfloats[subfloat.identifier] = {

src/resources/filters/customnodes/callout.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function _callout_main()
264264
return _quarto.format.typst.function_call("callout", {
265265
{ "body", _quarto.format.typst.as_typst_content(callout.content) },
266266
{ "title", _quarto.format.typst.as_typst_content(
267-
callout.title or pandoc.Plain(_quarto.modules.callouts.displayName(callout.type))
267+
(not quarto.utils.is_empty_node(callout.title) and callout.title) or
268+
pandoc.Plain(_quarto.modules.callouts.displayName(callout.type))
268269
)},
269270
{ "background_color", pandoc.RawInline("typst", background_color) },
270271
{ "icon_color", pandoc.RawInline("typst", icon_color) },
@@ -406,4 +407,4 @@ function crossref_callouts()
406407
return callout
407408
end
408409
}
409-
end
410+
end

src/resources/filters/customnodes/content-hidden.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ local _content_hidden_meta = nil
105105
function content_hidden_meta(meta)
106106
-- return {
107107
-- Meta = function(meta)
108-
_content_hidden_meta = meta
108+
-- The call to `pandoc.Meta` ensures that we hold a copy.
109+
_content_hidden_meta = pandoc.Meta(meta)
109110
-- end
110111
-- }
111112
end

src/resources/filters/customnodes/floatreftarget.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ function float_reftarget_render_html_figure(float)
767767
local float_content = pandoc.Div(_quarto.ast.walk(float.content, {
768768
-- strip image captions
769769
Image = function(image)
770-
image.caption = {}
770+
image.caption = pandoc.Inlines{}
771771
return image
772772
end
773773
}) or pandoc.Div({})) -- this should never happen but the lua analyzer doesn't know it
@@ -1098,4 +1098,4 @@ end, function(float)
10981098
return pandoc.Para({im})
10991099
end)
11001100

1101-
global_table_guid_id = 0
1101+
global_table_guid_id = 0

src/resources/filters/customnodes/shortcodes.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _quarto.ast.add_handler({
1111
kind = "Inline",
1212

1313
parse = function(span)
14-
local inner_content = pandoc.List({})
14+
local inner_content = pandoc.Inlines({})
1515

1616
span.content = span.content:filter(function(el)
1717
return el.t == "Span"
@@ -78,9 +78,9 @@ _quarto.ast.add_handler({
7878
end
7979

8080
local node = _quarto.ast.create_custom_node_scaffold("Shortcode", "Inline")
81-
node.content = inner_content:map(function(el)
82-
return pandoc.Span({el})
83-
end)
81+
node.content = pandoc.Inlines(inner_content:map(function(el)
82+
return pandoc.Span({el})
83+
end))
8484
local tbl = {
8585
__quarto_custom_node = node,
8686
name = name,

src/resources/filters/layout/html.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function renderHtmlFigure(el, render)
190190
end)
191191

192192
-- remove identifier (it is now on the div)
193-
el.identifier = ""
193+
el.attr.identifier = ""
194194

195195
if not figureDiv.classes:find_if(function(str) return str:match("quarto%-figure%-.+") end) then
196196
-- apply standalone figure css if not already set

src/resources/filters/layout/lightbox.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,11 @@ function lightbox()
176176
return {{
177177
traverse = "topdown",
178178

179-
Meta = function(meta)
179+
Meta = function(meta)
180180
-- Set auto lightbox mode, if need be
181181
auto = lightbox_module.automatic(meta) == true
182-
end,
182+
imgCount = 0
183+
end,
183184
-- Find images that are already within links
184185
-- we'll use this to filter out these images if
185186
-- the most is auto

0 commit comments

Comments
 (0)