Skip to content

Commit

Permalink
Merge pull request #7531 from quarto-dev/bugfix/7528
Browse files Browse the repository at this point in the history
html, latex: fix fig-align regressions
  • Loading branch information
cscheid authored Nov 9, 2023
2 parents 2b7c834 + 863ac12 commit 0202743
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/resources/filters/layout/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,9 @@ function latexImageFigure(image)

-- get align
local align = figAlignAttribute(image)

if align ~= nil then
image.attributes[kFigAlign] = nil
end
-- insert the figure without the caption
local figureContent = { pandoc.Para({
pandoc.RawInline("latex", latexBeginAlign(align)),
Expand Down
46 changes: 37 additions & 9 deletions src/resources/filters/quarto-post/html.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ function render_html_fixups()
if not _quarto.format.isHtmlOutput() then
return {}
end
local function needs_forward_align(source)
return attribute(source, kFigAlign, nil) or source.classes:find_if(function(c) return c:match("quarto%-figure.*") end)
end
local function forward_align(source, target)
local align = attribute(source, kFigAlign, nil)
if align ~= nil then
target.classes:insert("quarto-figure")
target.classes:insert("quarto-figure-" .. align)
end
for i, c in ipairs(source.classes) do
if c:match("quarto%-figure.*") then
target.classes:insert(c)
end
end
end

return {
Image = function(el)
Expand All @@ -14,6 +29,7 @@ function render_html_fixups()
local align = attribute(el, kFigAlign, nil)
if align ~= nil then
el.attributes[kFigAlign] = nil
el.classes:insert("quarto-figure")
el.classes:insert("quarto-figure-" .. align)
end
local alt_text = attribute(el, kFigAlt, nil)
Expand All @@ -23,6 +39,26 @@ function render_html_fixups()
end
return el
end,
Para = function(para)
if #para.content ~= 1 then
return nil
end
local img = quarto.utils.match("Para/[1]/Image")(para)
if img == false then
return nil
end
if not needs_forward_align(img) then
return nil
end
local el = pandoc.Div({
pandoc.RawBlock("html", "<figure>"),
para,
pandoc.RawBlock("html", "</figure>")
})

forward_align(img, el)
return el
end,
Div = function(div)
-- this narrow fix prevents a 1.3 regression with knitr:
-- https://github.com/quarto-dev/quarto-cli/issues/7516
Expand Down Expand Up @@ -50,15 +86,7 @@ function render_html_fixups()
-- but I don't trust this order to be consistent, so here we check
-- for both attribute and class

local align = attribute(img, kFigAlign, nil)
if align ~= nil then
el.classes:insert("quarto-figure-" .. align)
end
for i, c in ipairs(img.classes) do
if c:match("quarto%-figure%-.*") then
el.classes:insert(c)
end
end
forward_align(img, el)
return div
end
end
Expand Down
11 changes: 11 additions & 0 deletions src/resources/filters/quarto-post/latex.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ function render_latex()
if img.classes:includes("column-margin") then
return column_margin(pandoc.Span(img, img.attr))
end
local align = attribute(img, kFigAlign, nil) or attribute(img, kLayoutAlign, nil)
if align == nil then
return nil
end
img.attributes[kFigAlign] = nil
-- \\centering doesn't work consistently here...
return pandoc.Inlines({
pandoc.RawInline('latex', '\\begin{center}\n'),
img,
pandoc.RawInline('latex', '\n\\end{center}\n')
})
end,
Callout = function(node)
-- read and clear attributes
Expand Down

0 comments on commit 0202743

Please sign in to comment.