Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/extensions/quarto/placeholder/placeholder.lua
12923 views
1
return {
2
-- returns a placeholder data URI image of the specified size
3
['placeholder'] = function(args, kwargs, _meta, _rawargs, context)
4
local width = args[1] or "100"
5
local height = args[2] or width
6
local size_str = tostring(width) .. " x " .. tostring(height)
7
local svg_open = "<svg width = \"" .. width .. "\" height = \"" .. height .. "\" xmlns = \"http://www.w3.org/2000/svg\" viewBox = \"0 0 " .. width .. " " .. height .. "\">"
8
local svg_close = "</svg>"
9
local rect = "<rect width = \"" .. width .. "\" height = \"" .. height .. "\" fill = \"#ddd\" />"
10
local font_size = math.floor(0.1 * math.min(tonumber(width) or 100, tonumber(height) or 100))
11
local text = "<text x = \"50%\" y = \"50%\" font-family = \"sans-serif\" font-size = \"" .. tostring(font_size) .. "\" fill = \"#000\" text-anchor = \"middle\">" .. size_str .. "</text>"
12
local svg = svg_open .. rect .. text .. svg_close
13
local svg64 = "data:image/svg+xml;base64," .. quarto.base64.encode(svg)
14
local result
15
16
local output_format = pandoc.utils.stringify(kwargs["format"])
17
if output_format == "" then
18
if quarto.format.is_typst_output() then
19
output_format = "svg"
20
else
21
output_format = "png"
22
end
23
end
24
25
if output_format == "svg" then
26
result = svg64
27
else
28
local pcallresult, mt, contents = pcall(function()
29
local mt, contents = pandoc.mediabag.fetch("https://svg2png.deno.dev/" .. svg64)
30
return mt, contents
31
end)
32
if not pcallresult then
33
error("Error rendering placeholder")
34
error(contents)
35
return pandoc.Str("Error rendering placeholder")
36
end
37
if mt ~= "image/png" then
38
error("Expected image/png but got " .. mt)
39
error(contents)
40
return pandoc.Str("Error rendering placeholder")
41
end
42
result = "data:" .. mt .. ";base64," .. quarto.base64.encode(contents)
43
end
44
45
if context == "text" then
46
return result
47
else
48
return pandoc.Image({}, result)
49
end
50
end
51
}
52
53