Skip to content

Commit 45caede

Browse files
authored
Use _quarto.modules registry instead of redundant module requires in filters (#14702)
Pandoc's Lua interpreter caps top-level local variables at 200 (MAXVARS) across the whole bundled filter chain, and main.lua was already at that ceiling with zero headroom - blocking evaluation of a bump to Pandoc 3.10. `modules/import_all.lua` already centralizes most `modules/*` requires into a `_quarto.modules` registry table at no local-slot cost, but ~24 inlined filter files still redundantly re-required the same modules (constants, patterns, lightbox, scope, typst) as fresh top-level locals. - Replace `local X = require("modules/Y")` aliases with direct `_quarto.modules.Y` access at every call site across common, customnodes, init, layout, crossref, quarto-pre, quarto-post, and normalize/flags.lua filters - Pure access-path change to an already-loaded singleton table - no logic or API change - Drops packaged bundle's top-level local count from ~200 to 173 Verified packaged `quarto check` passes with no "too many local variables" error, packaged lightbox (HTML) and typst (PDF) renders succeed, and smoke-all coverage for the edited filters passes. Follow-up to #14664.
1 parent 9d3e107 commit 45caede

24 files changed

Lines changed: 113 additions & 159 deletions

‎src/resources/filters/common/citations.lua‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
-- restructured into the standard has
77
-- format
88

9-
local constants = require("modules/constants")
10-
119
local function processTypedId(el)
1210
if pandoc.utils.type(el) == "Inlines" then
1311
return { value = el }
@@ -36,23 +34,23 @@ end
3634

3735
function processCitationMeta(meta)
3836
if meta then
39-
local citationMeta = meta[constants.kCitation]
37+
local citationMeta = meta[_quarto.modules.constants.kCitation]
4038
if citationMeta and type(citationMeta) == "object" then
41-
local containerIds = citationMeta[constants.kContainerId]
39+
local containerIds = citationMeta[_quarto.modules.constants.kContainerId]
4240
if containerIds ~= nil then
43-
meta[constants.kCitation][constants.kContainerId] = normalizeTypedId(containerIds)
41+
meta[_quarto.modules.constants.kCitation][_quarto.modules.constants.kContainerId] = normalizeTypedId(containerIds)
4442
end
4543

46-
local articleIds = citationMeta[constants.kArticleId]
44+
local articleIds = citationMeta[_quarto.modules.constants.kArticleId]
4745
if articleIds ~= nil then
48-
meta[constants.kCitation][constants.kArticleId] = normalizeTypedId(articleIds)
46+
meta[_quarto.modules.constants.kCitation][_quarto.modules.constants.kArticleId] = normalizeTypedId(articleIds)
4947
end
5048

51-
if citationMeta[constants.kPage] and citationMeta[constants.kPageFirst] == nil and citationMeta[constants.kPageLast] == nil then
52-
local pagerange = split(pandoc.utils.stringify(citationMeta[constants.kPage]), '-')
53-
meta[constants.kCitation][constants.kPageFirst] = pandoc.Inlines(pagerange[1])
49+
if citationMeta[_quarto.modules.constants.kPage] and citationMeta[_quarto.modules.constants.kPageFirst] == nil and citationMeta[_quarto.modules.constants.kPageLast] == nil then
50+
local pagerange = split(pandoc.utils.stringify(citationMeta[_quarto.modules.constants.kPage]), '-')
51+
meta[_quarto.modules.constants.kCitation][_quarto.modules.constants.kPageFirst] = pandoc.Inlines(pagerange[1])
5452
if pagerange[2] then
55-
meta[constants.kCitation][constants.kPageLast] = pandoc.Inlines(pagerange[2])
53+
meta[_quarto.modules.constants.kCitation][_quarto.modules.constants.kPageLast] = pandoc.Inlines(pagerange[2])
5654
end
5755
end
5856
end

‎src/resources/filters/common/tables.lua‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- tables.lua
22
-- Copyright (C) 2021-2022 Posit Software, PBC
33

4-
local patterns = require("modules/patterns")
5-
64
function anonymousTblId()
75
return "tbl-anonymous-" .. tostring(math.random(10000000))
86
end
@@ -106,23 +104,23 @@ end
106104

107105
function hasGtHtmlTable(raw)
108106
if _quarto.format.isRawHtml(raw) and _quarto.format.isHtmlOutput() then
109-
return raw.text:match(patterns.html_gt_table)
107+
return raw.text:match(_quarto.modules.patterns.html_gt_table)
110108
else
111109
return false
112110
end
113111
end
114112

115113
function hasPagedHtmlTable(raw)
116114
if _quarto.format.isRawHtml(raw) and _quarto.format.isHtmlOutput() then
117-
return raw.text:match(patterns.html_paged_table)
115+
return raw.text:match(_quarto.modules.patterns.html_paged_table)
118116
else
119117
return false
120118
end
121119
end
122120

123121
function hasRawHtmlTable(raw)
124122
if _quarto.format.isRawHtml(raw) and _quarto.format.isHtmlOutput() then
125-
return raw.text:match(patterns.html_table)
123+
return raw.text:match(_quarto.modules.patterns.html_table)
126124
else
127125
return false
128126
end

‎src/resources/filters/crossref/tables.lua‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
-- wrapped in a div so they can carry parent information and so that
66
-- we can create a hyperef target for latex)
77

8-
local patterns = require("modules/patterns")
9-
108
function preprocessRawTableBlock(rawEl, parentId)
119

1210
local function divWrap(el, label, caption)
@@ -21,7 +19,7 @@ function preprocessRawTableBlock(rawEl, parentId)
2119
end
2220

2321
if _quarto.format.isRawHtml(rawEl) and _quarto.format.isHtmlOutput() then
24-
local captionPattern = patterns.html_table_caption
22+
local captionPattern = _quarto.modules.patterns.html_table_caption
2523
local _, caption, _ = string.match(rawEl.text, captionPattern)
2624
if caption then
2725
-- extract id if there is one
@@ -165,7 +163,7 @@ function processRawTable(divEl)
165163
local label = divEl.attr.identifier
166164
-- html table
167165
if _quarto.format.isRawHtml(rawEl) then
168-
local captionPattern = patterns.html_table_caption
166+
local captionPattern = _quarto.modules.patterns.html_table_caption
169167
local _, caption, _ = string.match(rawEl.text, captionPattern)
170168
if caption then
171169

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
-- Copyright (C) 2022 Posit Software, PBC
33

44

5-
local constants = require("modules/constants")
6-
75
local kConditions = pandoc.List({
8-
constants.kWhenMeta, constants.kUnlessMeta,
9-
constants.kWhenFormat, constants.kUnlessFormat,
10-
constants.kWhenProfile, constants.kUnlessProfile
6+
_quarto.modules.constants.kWhenMeta, _quarto.modules.constants.kUnlessMeta,
7+
_quarto.modules.constants.kWhenFormat, _quarto.modules.constants.kUnlessFormat,
8+
_quarto.modules.constants.kWhenProfile, _quarto.modules.constants.kUnlessProfile
119
})
1210

1311
function is_visible(node)
1412
local profiles = pandoc.List(param("quarto_profile", {}))
1513
local match = propertiesMatch(node.condition, profiles)
16-
if node.behavior == constants.kContentVisible then
14+
if node.behavior == _quarto.modules.constants.kContentVisible then
1715
return match
18-
elseif node.behavior == constants.kContentHidden then
16+
elseif node.behavior == _quarto.modules.constants.kContentHidden then
1917
return not match
2018
else
2119
-- luacov: disable
@@ -26,14 +24,14 @@ function is_visible(node)
2624
end
2725

2826
_quarto.ast.add_handler({
29-
class_name = { constants.kContentVisible, constants.kContentHidden },
27+
class_name = { _quarto.modules.constants.kContentVisible, _quarto.modules.constants.kContentHidden },
3028

3129
ast_name = "ConditionalBlock",
3230

3331
kind = "Block",
3432

3533
parse = function(div)
36-
local behavior = div.classes:find(constants.kContentVisible) or div.classes:find(constants.kContentHidden)
34+
local behavior = div.classes:find(_quarto.modules.constants.kContentVisible) or div.classes:find(_quarto.modules.constants.kContentHidden)
3735
local condition = pandoc.List({})
3836
local remaining_attributes = pandoc.List({})
3937
for i, v in ipairs(div.attributes) do
@@ -44,7 +42,7 @@ _quarto.ast.add_handler({
4442
end
4543
end
4644
div.attributes = remaining_attributes
47-
div.classes = div.classes:filter(function(k) return k ~= constants.kContentVisible and k ~= constants.kContentHidden end)
45+
div.classes = div.classes:filter(function(k) return k ~= _quarto.modules.constants.kContentVisible and k ~= _quarto.modules.constants.kContentHidden end)
4846

4947
return quarto.ConditionalBlock({
5048
node = div,
@@ -144,10 +142,10 @@ end
144142
function handleHiddenVisible(profiles)
145143
return function(el)
146144
local visible
147-
if el.attr.classes:find(constants.kContentVisible) then
145+
if el.attr.classes:find(_quarto.modules.constants.kContentVisible) then
148146
visible = propertiesMatch(el.attributes, profiles)
149147
clearHiddenVisibleAttributes(el)
150-
elseif el.attr.classes:find(constants.kContentHidden) then
148+
elseif el.attr.classes:find(_quarto.modules.constants.kContentHidden) then
151149
visible = not propertiesMatch(el.attributes, profiles)
152150
clearHiddenVisibleAttributes(el)
153151
else
@@ -191,12 +189,12 @@ function propertiesMatch(properties, profiles)
191189
end
192190
end
193191
local tests = {
194-
{ constants.kWhenMeta, check_meta, false },
195-
{ constants.kUnlessMeta, check_meta, true },
196-
{ constants.kWhenFormat, quarto.format.is_format, false },
197-
{ constants.kUnlessFormat, quarto.format.is_format, true },
198-
{ constants.kWhenProfile, check_profile, false },
199-
{ constants.kUnlessProfile, check_profile, true }
192+
{ _quarto.modules.constants.kWhenMeta, check_meta, false },
193+
{ _quarto.modules.constants.kUnlessMeta, check_meta, true },
194+
{ _quarto.modules.constants.kWhenFormat, quarto.format.is_format, false },
195+
{ _quarto.modules.constants.kUnlessFormat, quarto.format.is_format, true },
196+
{ _quarto.modules.constants.kWhenProfile, check_profile, false },
197+
{ _quarto.modules.constants.kUnlessProfile, check_profile, true }
200198
}
201199
local match = true
202200
for _, test in ipairs(tests) do
@@ -211,10 +209,10 @@ function propertiesMatch(properties, profiles)
211209
end
212210

213211
function clearHiddenVisibleAttributes(el)
214-
el.attributes[constants.kUnlessFormat] = nil
215-
el.attributes[constants.kWhenFormat] = nil
216-
el.attributes[constants.kUnlessProfile] = nil
217-
el.attributes[constants.kWhenProfile] = nil
218-
el.attr.classes = removeClass(el.attr.classes, constants.kContentVisible)
219-
el.attr.classes = removeClass(el.attr.classes, constants.kContentHidden)
212+
el.attributes[_quarto.modules.constants.kUnlessFormat] = nil
213+
el.attributes[_quarto.modules.constants.kWhenFormat] = nil
214+
el.attributes[_quarto.modules.constants.kUnlessProfile] = nil
215+
el.attributes[_quarto.modules.constants.kWhenProfile] = nil
216+
el.attr.classes = removeClass(el.attr.classes, _quarto.modules.constants.kContentVisible)
217+
el.attr.classes = removeClass(el.attr.classes, _quarto.modules.constants.kContentHidden)
220218
end

‎src/resources/filters/customnodes/floatreftarget.lua‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
-- Copyright (C) 2023 Posit Software, PBC
33

44
local drop_class = require("modules/filters").drop_class
5-
local patterns = require("modules/patterns")
65

76
-- Track whether we've injected the Typst show rule for listing alignment
87
local injected_listing_align_rule = false
@@ -565,7 +564,7 @@ end, function(float)
565564
-- and recreating it below.
566565
-- See #7937
567566
if _quarto.format.isRawLatex(float.content) then
568-
local _b, _e, _beginenv, inner_content, _endenv = float.content.text:find(patterns.latex_table_star)
567+
local _b, _e, _beginenv, inner_content, _endenv = float.content.text:find(_quarto.modules.patterns.latex_table_star)
569568
if _b ~= nil then
570569
figEnv = "table*"
571570
float.content.text = inner_content

‎src/resources/filters/layout/figures.lua‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
-- figures.lua
22
-- Copyright (C) 2020-2022 Posit Software, PBC
33

4-
local constants = require("modules/constants")
5-
64
function preventExtendedFigure(el)
7-
el.attr.attributes[constants.kFigExtended] = "false"
5+
el.attr.attributes[_quarto.modules.constants.kFigExtended] = "false"
86
end
97

108
function forceExtendedFigure(el)
11-
el.attr.attributes[constants.kFigExtended] = "true"
9+
el.attr.attributes[_quarto.modules.constants.kFigExtended] = "true"
1210
end
1311

1412
function shouldHandleExtended(el)
15-
return el.attr.attributes[constants.kFigExtended] ~= "false"
13+
return el.attr.attributes[_quarto.modules.constants.kFigExtended] ~= "false"
1614
end
1715

1816
-- By default, images without captions should be
@@ -38,7 +36,7 @@ function shouldHandleExtendedImage(el)
3836
end
3937

4038
-- handle extended if it was explicitly enabled
41-
if el.attr.attributes[constants.kFigExtended] == "true" then
39+
if el.attr.attributes[_quarto.modules.constants.kFigExtended] == "true" then
4240
return true
4341
end
4442

‎src/resources/filters/layout/lightbox.lua‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
-- lightbox.lua
22
-- Copyright (C) 2020-2022 Posit Software, PBC
33

4-
local lightbox_module = require("modules/lightbox")
5-
6-
74
-- attributes to forward from the image to the newly created link
85
local kDescription = "description"
96
local kForwardedAttr = {
@@ -178,7 +175,7 @@ function lightbox()
178175

179176
Meta = function(meta)
180177
-- Set auto lightbox mode, if need be
181-
auto = lightbox_module.automatic(meta) == true
178+
auto = _quarto.modules.lightbox.automatic(meta) == true
182179
imgCount = 0
183180
end,
184181
-- Find images that are already within links

‎src/resources/filters/layout/manuscript.lua‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
-- manuscript.lua
22
-- Copyright (C) 2021-2022 Posit Software, PBC
33

4-
local constants = require("modules/constants")
54
local kUnrollMarkdownCells = "unroll-markdown-cells"
65

76
function manuscriptUnroll()
@@ -28,10 +27,10 @@ function manuscript()
2827
if _quarto.format.isWordProcessorOutput() or _quarto.format.isLatexOutput() then
2928

3029
local language = param("language", nil);
31-
local notebookPrefix = language[constants.kLangSourcePrefix]
30+
local notebookPrefix = language[_quarto.modules.constants.kLangSourcePrefix]
3231

33-
local manuscriptBaseUrl = param(constants.kManuscriptUrl)
34-
local notebookLinks = param(constants.kNotebookLinks)
32+
local manuscriptBaseUrl = param(_quarto.modules.constants.kManuscriptUrl)
33+
local notebookLinks = param(_quarto.modules.constants.kNotebookLinks)
3534

3635
return {
3736
traverse = 'topdown',
@@ -51,8 +50,8 @@ function manuscript()
5150
end
5251

5352
-- Read notebook parameters from the cell, if present
54-
local nbAbsPath = divEl.attributes[constants.kNotebook]
55-
local nbTitle = divEl.attributes[constants.kNotebookTitle]
53+
local nbAbsPath = divEl.attributes[_quarto.modules.constants.kNotebook]
54+
local nbTitle = divEl.attributes[_quarto.modules.constants.kNotebookTitle]
5655

5756
-- If this is a notebook embed cell, 'lift' the contents of any child divs
5857
-- up (unroll their contents), this will help us avoid
@@ -115,7 +114,7 @@ function manuscript()
115114
end
116115

117116
-- The Id
118-
local cellId = divEl.attributes[constants.kNotebookCellId];
117+
local cellId = divEl.attributes[_quarto.modules.constants.kNotebookCellId];
119118
if cellId ~= nil then
120119
cellId = '#' .. cellId
121120
else

‎src/resources/filters/layout/pandoc3_figure.lua‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
-- never cross-referenceable but they need to be rendered as
66
-- if they were.
77

8-
local scope_utils = require("modules/scope")
9-
108
function render_pandoc3_figure()
119
local function html_handle_linked_image(figure)
1210
local div = pandoc.Div({})
@@ -151,7 +149,7 @@ function render_pandoc3_figure()
151149
image.attributes['quarto-caption-env'] = 'subcaption'
152150
end
153151
image.classes:extend(figure.classes)
154-
if scope_utils.lookup_class(scope, "column-margin") then
152+
if _quarto.modules.scope.lookup_class(scope, "column-margin") then
155153
image.classes:insert("column-margin")
156154
end
157155
return latexImageFigure(image)

‎src/resources/filters/normalize/flags.lua‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@
55
-- so that we can skip as many filters as possible
66
-- when we don't need them
77

8-
local patterns = require("modules/patterns")
9-
local constants = require("modules/constants")
10-
local lightbox_module = require("modules/lightbox")
11-
128
flags = {}
139

1410
function compute_flags()
15-
local table_pattern = patterns.html_table
16-
local table_tag_pattern = patterns.html_table_tag_name
17-
local gt_table_pattern = patterns.html_gt_table
11+
local table_pattern = _quarto.modules.patterns.html_table
12+
local table_tag_pattern = _quarto.modules.patterns.html_table_tag_name
13+
local gt_table_pattern = _quarto.modules.patterns.html_gt_table
1814
local function find_shortcode_in_attributes(el)
1915
for k, v in pairs(el.attributes) do
2016
if type(v) == "string" and v:find("%{%{%<") then
@@ -79,7 +75,7 @@ function compute_flags()
7975
flags.has_theorem_refs = true
8076
end
8177

82-
local has_lightbox = lightbox_module.el_has_lightbox(node)
78+
local has_lightbox = _quarto.modules.lightbox.el_has_lightbox(node)
8379
if has_lightbox then
8480
flags.has_lightbox = true
8581
end
@@ -161,7 +157,7 @@ function compute_flags()
161157
flags.has_shortcodes = true
162158
end
163159

164-
local has_lightbox = lightbox_module.el_has_lightbox(node)
160+
local has_lightbox = _quarto.modules.lightbox.el_has_lightbox(node)
165161
if has_lightbox then
166162
flags.has_lightbox = true
167163
end
@@ -193,7 +189,7 @@ function compute_flags()
193189
end,
194190
}, {
195191
Meta = function(el)
196-
local lightbox_auto = lightbox_module.automatic(el)
192+
local lightbox_auto = _quarto.modules.lightbox.automatic(el)
197193
if lightbox_auto then
198194
flags.has_lightbox = true
199195
elseif lightbox_auto == false then

0 commit comments

Comments
 (0)