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

src/resources/filters/layout/typst.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function make_typst_figure(tbl)
1111
local identifier = tbl.identifier
1212
local separator = tbl.separator
1313

14-
if (not caption or #caption.content == 0) and tbl.separator == nil then
14+
if quarto.utils.is_empty_node(caption) and tbl.separator == nil then
1515
separator = ""
1616
end
1717

src/resources/filters/normalize/draft.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ function normalize_draft()
2323
end
2424
is_draft = meta[kDraft] == true or tcontains(drafts, quarto.doc.input_file);
2525
end,
26-
Pandoc = function(pandoc)
26+
Pandoc = function(doc)
2727
if _quarto.format.isHtmlOutput() and not _quarto.format.isHtmlSlideOutput() then
2828
if is_draft and draft_mode == kDraftModeGone then
29-
pandoc.blocks = {}
29+
doc.blocks = pandoc.Blocks{}
3030
quarto.doc.includeText("in-header", '<meta name="quarto:status" content="' .. kDraftStatusRemove .. '">')
31-
return pandoc
31+
return doc
3232
elseif is_draft and draft_mode ~= kDraftModeGone then
3333
quarto.doc.includeText("in-header", '<meta name="quarto:status" content="' .. kDraftStatusDraft .. '">')
34-
return pandoc
34+
return doc
3535
end
3636
end
3737
end
3838
}
39-
end
39+
end

src/resources/filters/normalize/flags.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ function compute_flags()
107107
-- FIXME: are we actually triggering this with FloatRefTargets?
108108
-- table captions
109109
local kTblCap = "tbl-cap"
110-
local tblCap = extractTblCapAttrib(node,kTblCap)
111-
if hasTableRef(node) or tblCap then
110+
if hasTableRef(node) or node.attr.attributes[kTblCap] then
112111
flags.has_table_captions = true
113112
end
114113

src/resources/filters/quarto-init/metainit.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function quarto_meta_init()
88
read_includes(meta)
99
init_crossref_options(meta)
1010
initialize_custom_crossref_categories(meta)
11+
return meta
1112
end
1213
}
13-
end
14+
end

src/resources/filters/quarto-post/foldcode.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function fold_code_and_lift_codeblocks()
6565
local prev_annotated_code_block_scaffold = nil
6666
local prev_annotated_code_block = nil
6767
-- ok to lift codeblocks
68-
float.content = _quarto.ast.walk(float.content, {
68+
float.content = _quarto.ast.walk(float.content or pandoc.Blocks{}, {
6969
traverse = "topdown",
7070
DecoratedCodeBlock = function(block)
7171
-- defer the folding of code blocks to the DecoratedCodeBlock renderer

src/resources/filters/quarto-post/typst.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ function render_typst()
1919
return {
2020
{
2121
Meta = function(m)
22-
m["toc-depth"] = PANDOC_WRITER_OPTIONS["toc_depth"]
22+
-- This should be a number, but we must represent it as a string,
23+
-- as numbers are disallowed as metadata values.
24+
m["toc-depth"] = tostring(PANDOC_WRITER_OPTIONS["toc_depth"])
2325
m["toc-indent"] = option("toc-indent")
2426
if m["number-depth"] then
2527
number_depth = tonumber(pandoc.utils.stringify(m["number-depth"]))
@@ -138,7 +140,7 @@ function render_typst_fixups()
138140
end
139141

140142
img.attributes["fig-align"] = nil
141-
return pandoc.Inlines({
143+
return pandoc.Plain({
142144
pandoc.RawInline("typst", "#align(" .. align .. ")["),
143145
img,
144146
pandoc.RawInline("typst", "]"),

src/resources/filters/quarto-pre/code-annotation.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function code_annotations()
310310
-- if code annotations is false, then shut it down
311311
if codeAnnotations ~= false then
312312

313-
local outputs = pandoc.List()
313+
local outputs = pandoc.Blocks{}
314314

315315
-- annotations[annotation-number] = {list of line numbers}
316316
local pendingAnnotations = nil

src/resources/filters/quarto-pre/output-location.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function output_location()
7171
if _quarto.format.isRevealJsOutput() then
7272
return {
7373
Blocks = function(blocks)
74-
local newBlocks = pandoc.List()
74+
local newBlocks = pandoc.Blocks{}
7575
for _,block in pairs(blocks) do
7676
local outputLoc = collectCellOutputLocation(block)
7777
if outputLoc then

src/resources/filters/quarto-pre/parsefiguredivs.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function parse_floatreftargets()
236236
end
237237
local caption = refCaptionFromDiv(div)
238238
if caption ~= nil then
239-
div.content:remove(#div.content)
239+
div.content:remove() -- drop the last element
240240
elseif div.attributes[caption_attr_key] ~= nil then
241241
caption = pandoc.Plain(string_to_quarto_ast_inlines(div.attributes[caption_attr_key]))
242242
div.attributes[caption_attr_key] = nil
@@ -246,10 +246,10 @@ function parse_floatreftargets()
246246
local found_caption = false
247247
content = _quarto.ast.walk(content, {
248248
Table = function(table)
249-
if table.caption.long ~= nil then
249+
-- check if caption is non-empty
250+
if table.caption.long and next(table.caption.long) then
250251
found_caption = true
251252
caption = table.caption.long[1] -- what if there's more than one entry here?
252-
table.caption.long = nil
253253
return table
254254
end
255255
end
@@ -458,7 +458,7 @@ function parse_floatreftargets()
458458
fig_attr.classes:insert(v)
459459
end
460460
end
461-
image.caption = {}
461+
image.caption = pandoc.Inlines{}
462462
return image
463463
end
464464
}) or fig.content[1] -- this shouldn't be needed but the lua analyzer doesn't know it
@@ -494,7 +494,7 @@ function parse_floatreftargets()
494494
end
495495

496496
-- we've parsed the caption, so we can remove it from the table
497-
el.caption.long = pandoc.List({})
497+
el.caption.long = pandoc.Blocks({})
498498

499499
if label == "" then
500500
return nil
@@ -602,7 +602,7 @@ function parse_floatreftargets()
602602
if img.identifier == "" then
603603
local caption = img.caption
604604
if #caption > 0 then
605-
img.caption = nil
605+
img.caption = pandoc.Inlines{}
606606
return pandoc.Figure(link, { long = { caption } })
607607
else
608608
return nil
@@ -819,4 +819,4 @@ function forward_cell_subcaps()
819819
return div
820820
end
821821
}
822-
end
822+
end

src/resources/filters/quarto-pre/table-captions.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function applyTableCaptions(el, tblCaptions, tblLabels)
140140
cap:insert(pandoc.Str("{#" .. tblLabels[idx] .. "}"))
141141
end
142142
idx = idx + 1
143-
el.caption.long = pandoc.Plain(cap)
143+
el.caption.long = pandoc.Blocks{pandoc.Plain(cap)}
144144
return el
145145
end
146146
end,
@@ -231,7 +231,7 @@ function extractTblCapAttrib(el, name, subcap)
231231
else
232232
value = pandoc.List({ value })
233233
end
234-
el.attr.attributes[name] = nil
234+
-- el.attr.attributes[name] = nil
235235
return value
236236
end
237237
return nil

src/resources/filters/quarto-pre/table-rawhtml.lua

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,32 @@ function table_merge_raw_html()
1414

1515
return {
1616
Blocks = function(blocks)
17-
local pendingRaw = pandoc.List()
18-
local merged = pandoc.List()
19-
for i,el in ipairs(blocks) do
20-
if _quarto.format.isRawHtml(el) and el.text:find(patterns.html_table_tag_name) then
21-
pendingRaw:insert(el.text)
17+
local pending_raw = pandoc.List()
18+
local next_element_idx = 1
19+
for _, el in ipairs(blocks) do
20+
if _quarto.format.isRawHtml(el) and
21+
el.text:find(patterns.html_table_tag_name) then
22+
pending_raw:insert(el.text)
2223
else
23-
if #pendingRaw > 0 then
24-
merged:insert(pandoc.RawBlock("html", table.concat(pendingRaw, "\n")))
25-
pendingRaw = pandoc.List()
24+
if next(pending_raw) then
25+
blocks[next_element_idx] =
26+
pandoc.RawBlock("html", table.concat(pending_raw, "\n"))
27+
pending_raw = pandoc.List()
28+
next_element_idx = next_element_idx + 1
2629
end
27-
merged:insert(el)
30+
blocks[next_element_idx] = el
31+
next_element_idx = next_element_idx + 1
2832
end
2933
end
30-
if #pendingRaw > 0 then
31-
merged:insert(pandoc.RawBlock("html", table.concat(pendingRaw, "\n")))
34+
if #pending_raw > 0 then
35+
blocks[next_element_idx] =
36+
pandoc.RawBlock("html", table.concat(pending_raw, "\n"))
37+
next_element_idx = next_element_idx + 1
3238
end
33-
return merged
39+
for i = next_element_idx, #blocks do
40+
blocks[i] = nil
41+
end
42+
return blocks
3443
end
3544
}
3645
end
@@ -54,4 +63,4 @@ function table_respecify_gt_css()
5463
return el
5564
end
5665
}
57-
end
66+
end

0 commit comments

Comments
 (0)