Path: blob/main/src/resources/extensions/quarto/placeholder/placeholder.lua
12923 views
return {1-- returns a placeholder data URI image of the specified size2['placeholder'] = function(args, kwargs, _meta, _rawargs, context)3local width = args[1] or "100"4local height = args[2] or width5local size_str = tostring(width) .. " x " .. tostring(height)6local svg_open = "<svg width = \"" .. width .. "\" height = \"" .. height .. "\" xmlns = \"http://www.w3.org/2000/svg\" viewBox = \"0 0 " .. width .. " " .. height .. "\">"7local svg_close = "</svg>"8local rect = "<rect width = \"" .. width .. "\" height = \"" .. height .. "\" fill = \"#ddd\" />"9local font_size = math.floor(0.1 * math.min(tonumber(width) or 100, tonumber(height) or 100))10local text = "<text x = \"50%\" y = \"50%\" font-family = \"sans-serif\" font-size = \"" .. tostring(font_size) .. "\" fill = \"#000\" text-anchor = \"middle\">" .. size_str .. "</text>"11local svg = svg_open .. rect .. text .. svg_close12local svg64 = "data:image/svg+xml;base64," .. quarto.base64.encode(svg)13local result1415local output_format = pandoc.utils.stringify(kwargs["format"])16if output_format == "" then17if quarto.format.is_typst_output() then18output_format = "svg"19else20output_format = "png"21end22end2324if output_format == "svg" then25result = svg6426else27local pcallresult, mt, contents = pcall(function()28local mt, contents = pandoc.mediabag.fetch("https://svg2png.deno.dev/" .. svg64)29return mt, contents30end)31if not pcallresult then32error("Error rendering placeholder")33error(contents)34return pandoc.Str("Error rendering placeholder")35end36if mt ~= "image/png" then37error("Expected image/png but got " .. mt)38error(contents)39return pandoc.Str("Error rendering placeholder")40end41result = "data:" .. mt .. ";base64," .. quarto.base64.encode(contents)42end4344if context == "text" then45return result46else47return pandoc.Image({}, result)48end49end50}515253