Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add layout = auto #9423

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/resources/filters/layout/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function layout_cells(float_or_div, cells)
-- check for layout
elseif layout ~= nil then
-- parse the layout
layout = parseLayoutWidths(layout, #cells)
layout = parseLayoutWidths(layout, cells)

-- manage/perform next insertion into the layout
local cellIndex = 1
Expand Down
43 changes: 36 additions & 7 deletions src/resources/filters/layout/width.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,43 @@
-- Copyright (C) 2020-2022 Posit Software, PBC

-- parse a layout specification
function parseLayoutWidths(figLayout, figureCount)
function parseLayoutWidths(figLayout, cells)
local figureCount = #cells

-- parse json
figLayout = pandoc.List(quarto.json.decode(figLayout))

-- if there are no tables then make a table and stick the items in it
if not figLayout:find_if(function(item) return type(item) == "table" end) then
figLayout = pandoc.List({figLayout})
if figLayout == "auto" then
local sizes = pandoc.List({})
_quarto.ast.walk(pandoc.Div(cells), {
Image = function(image)
print("Found image")
local mt, contents = pandoc.mediabag.lookup(image.src)
if mt == nil then
mt, contents = pandoc.mediabag.fetch(image.src)
end
if mt == nil then
error("Could not find image " .. image.src)
end
sizes:insert(pandoc.image.size(contents))
end
})
if #cells ~= #sizes then
error("Number of found images does not match number of cells; cannot use 'auto' layout")
end
if #cells == 0 then
error("No images found; cannot use 'auto' layout")
end
-- create widths such that the first image is 100% and the rest are relative to it
figLayout = pandoc.List({sizes:map(function(size)
return size.width * (sizes[1].height / size.height)
end)})
quarto.utils.dump(figLayout)
else
-- parse json
figLayout = pandoc.List(quarto.json.decode(figLayout))

-- if there are no tables then make a table and stick the items in it
if not figLayout:find_if(function(item) return type(item) == "table" end) then
figLayout = pandoc.List({figLayout})
end
end

-- validate that layout is now all rows
Expand Down
Loading