Skip to content

Commit c821c68

Browse files
committed
lua - slightly revert overzealous conversion to subfigure
1 parent 36ccd88 commit c821c68

File tree

2 files changed

+157
-77
lines changed

2 files changed

+157
-77
lines changed

src/resources/filters/layout/pandoc3_figure.lua

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

8+
local scope_utils = require("modules/scope")
9+
810
function render_pandoc3_figure()
911
local function html_handle_linked_image(figure)
1012
local div = pandoc.Div({})
@@ -70,90 +72,167 @@ function render_pandoc3_figure()
7072
end
7173
}
7274
elseif _quarto.format.isLatexOutput() then
73-
-- split local declaration because Lua's local is not
74-
-- a letrec
75-
local filter
76-
filter = function(state)
77-
state = state or {}
78-
local function figure_renderer(figure, is_subfig)
79-
-- this is a figure that is not cross-referenceable
80-
-- if this ends up in a layout without fig-pos = H, it'll fail
81-
-- 'H' forces it to not float
82-
if figure.identifier == "" then
83-
figure = _quarto.ast.walk(figure, {
84-
Image = function(image)
85-
image.attributes['fig-pos'] = 'H'
86-
return image
87-
end
88-
})
89-
end
90-
local image
91-
_quarto.ast.walk(figure, {
92-
Image = function(img)
93-
image = img
75+
local function is_inside_float(scope)
76+
for i = #scope, 1, -1 do
77+
local s = scope[i]
78+
local data = _quarto.ast.resolve_custom_data(s)
79+
if data then
80+
if (data.t == "PanelLayout" and data.is_float_reftarget == true) then
81+
return true
82+
elseif (data.t == "FloatRefTarget") then
83+
return true
9484
end
95-
})
96-
if image == nil then
97-
return figure
9885
end
99-
if figure.caption.long ~= nil then
100-
image.caption = quarto.utils.as_inlines(figure.caption.long)
101-
end
102-
for k, v in pairs(figure.attributes) do
103-
image.attributes[k] = v
104-
end
105-
if is_subfig then
106-
image.attributes['quarto-caption-env'] = 'subcaption'
107-
end
108-
image.classes:extend(figure.classes)
109-
if state.in_column_margin then
110-
image.classes:insert("column-margin")
86+
end
87+
end
88+
local function is_subfig(scope)
89+
for i = #scope, 1, -1 do
90+
local s = scope[i]
91+
local data = _quarto.ast.resolve_custom_data(s)
92+
if data and (data.t == "PanelLayout") then
93+
return true
11194
end
112-
return latexImageFigure(image)
113-
end
114-
local function float_renderer(float)
115-
local count = 0
116-
local new_content = _quarto.ast.walk(float.content, {
117-
Figure = function(fig)
118-
count = count + 1
119-
return figure_renderer(fig, true), false
95+
end
96+
97+
return false
98+
end
99+
100+
local function figure_renderer(figure, scope)
101+
if is_inside_float(scope) then
102+
return nil, false
103+
end
104+
local subfig = is_subfig(scope)
105+
-- this is a figure that is not cross-referenceable
106+
-- if this ends up in a layout without fig-pos = H, it'll fail
107+
-- 'H' forces it to not float
108+
if figure.identifier == "" then
109+
figure = _quarto.ast.walk(figure, {
110+
Image = function(image)
111+
image.attributes['fig-pos'] = 'H'
112+
return image
120113
end
121114
})
122-
if count > 0 then
123-
float.content = new_content
124-
return float, false
125-
end
126115
end
127-
return {
128-
traverse = "topdown",
129-
PanelLayout = function(panel)
130-
panel.rows = _quarto.ast.walk(panel.rows, {
131-
Figure = function(fig)
132-
return figure_renderer(fig, true), false
133-
end
134-
})
135-
return panel, false
136-
end,
137-
FloatRefTarget = float_renderer,
138-
Div = function(div)
139-
if div.classes:includes("column-margin") then
140-
local new_state = {}
141-
for k, v in pairs(state) do
142-
new_state[k] = v
143-
end
144-
new_state.in_column_margin = true
145-
146-
div.content = _quarto.ast.walk(div.content, filter(new_state))
147-
div.classes = div.classes:filter(function(x) return x ~= "column-margin" end)
148-
return div
149-
end
150-
end,
151-
Figure = function(figure)
152-
return figure_renderer(figure, false)
116+
local image
117+
_quarto.ast.walk(figure, {
118+
Image = function(img)
119+
image = img
153120
end
154-
}
121+
})
122+
if image == nil then
123+
return figure
124+
end
125+
if figure.caption.long ~= nil then
126+
image.caption = quarto.utils.as_inlines(figure.caption.long)
127+
end
128+
for k, v in pairs(figure.attributes) do
129+
image.attributes[k] = v
130+
end
131+
if subfig then
132+
image.attributes['quarto-caption-env'] = 'subcaption'
133+
end
134+
image.classes:extend(figure.classes)
135+
if scope_utils.lookup_class(scope, "column-margin") then
136+
image.classes:insert("column-margin")
137+
end
138+
return latexImageFigure(image)
155139
end
156-
return filter()
140+
141+
local filter = {
142+
Figure = function(figure, scope)
143+
return figure_renderer(figure, scope), false
144+
end
145+
}
146+
return {
147+
Pandoc = function(doc)
148+
_quarto.ast.scoped_walk(doc.blocks, filter)
149+
end
150+
}
151+
152+
-- split local declaration because Lua's local is not
153+
-- a letrec
154+
-- local filter
155+
-- filter = function(state)
156+
-- state = state or {}
157+
-- local function figure_renderer(figure, is_subfig)
158+
-- -- this is a figure that is not cross-referenceable
159+
-- -- if this ends up in a layout without fig-pos = H, it'll fail
160+
-- -- 'H' forces it to not float
161+
-- if figure.identifier == "" then
162+
-- figure = _quarto.ast.walk(figure, {
163+
-- Image = function(image)
164+
-- image.attributes['fig-pos'] = 'H'
165+
-- return image
166+
-- end
167+
-- })
168+
-- end
169+
-- local image
170+
-- _quarto.ast.walk(figure, {
171+
-- Image = function(img)
172+
-- image = img
173+
-- end
174+
-- })
175+
-- if image == nil then
176+
-- return figure
177+
-- end
178+
-- if figure.caption.long ~= nil then
179+
-- image.caption = quarto.utils.as_inlines(figure.caption.long)
180+
-- end
181+
-- for k, v in pairs(figure.attributes) do
182+
-- image.attributes[k] = v
183+
-- end
184+
-- if is_subfig then
185+
-- image.attributes['quarto-caption-env'] = 'subcaption'
186+
-- end
187+
-- image.classes:extend(figure.classes)
188+
-- if state.in_column_margin then
189+
-- image.classes:insert("column-margin")
190+
-- end
191+
-- return latexImageFigure(image)
192+
-- end
193+
-- local function float_renderer(float)
194+
-- local count = 0
195+
-- local new_content = _quarto.ast.walk(float.content, {
196+
-- Figure = function(fig)
197+
-- count = count + 1
198+
-- return figure_renderer(fig, true), false
199+
-- end
200+
-- })
201+
-- if count > 0 then
202+
-- float.content = new_content
203+
-- return float, false
204+
-- end
205+
-- end
206+
-- return {
207+
-- traverse = "topdown",
208+
-- PanelLayout = function(panel)
209+
-- panel.rows = _quarto.ast.walk(panel.rows, {
210+
-- Figure = function(fig)
211+
-- return figure_renderer(fig, true), false
212+
-- end
213+
-- })
214+
-- return panel, false
215+
-- end,
216+
-- FloatRefTarget = float_renderer,
217+
-- Div = function(div)
218+
-- if div.classes:includes("column-margin") then
219+
-- local new_state = {}
220+
-- for k, v in pairs(state) do
221+
-- new_state[k] = v
222+
-- end
223+
-- new_state.in_column_margin = true
224+
225+
-- div.content = _quarto.ast.walk(div.content, filter(new_state))
226+
-- div.classes = div.classes:filter(function(x) return x ~= "column-margin" end)
227+
-- return div
228+
-- end
229+
-- end,
230+
-- Figure = function(figure)
231+
-- return figure_renderer(figure, false)
232+
-- end
233+
-- }
234+
-- end
235+
-- return filter()
157236
elseif _quarto.format.isTypstOutput() then
158237
return {
159238
traverse = "topdown",

src/resources/filters/modules/scope.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ local function lookup_class(scope, class_name)
3333
else
3434
attr = scope[i].classes
3535
end
36-
if classes:includes(class_name) then
36+
if classes and classes:includes(class_name) then
3737
return true
3838
end
39+
i = i - 1
3940
end
4041
end
4142

0 commit comments

Comments
 (0)