From 2914c7ae85b407c27c17b367b914c686794915ea Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Fri, 19 Apr 2024 09:50:10 -0700 Subject: [PATCH] partial work --- src/resources/filters/layout/layout.lua | 2 +- src/resources/filters/layout/width.lua | 43 +++++++++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/resources/filters/layout/layout.lua b/src/resources/filters/layout/layout.lua index 49ab16f0fc..9b4f9cf1e7 100644 --- a/src/resources/filters/layout/layout.lua +++ b/src/resources/filters/layout/layout.lua @@ -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 diff --git a/src/resources/filters/layout/width.lua b/src/resources/filters/layout/width.lua index f83bc9d9d5..07a27a9537 100644 --- a/src/resources/filters/layout/width.lua +++ b/src/resources/filters/layout/width.lua @@ -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