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 default file format when Quarto format is typst #7

Merged
merged 17 commits into from
Mar 20, 2024
Merged
Changes from 3 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
30 changes: 21 additions & 9 deletions _extensions/d2/d2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local D2Theme = {
EvergladeGreen = 104,
ButteredToast = 105,
DarkMauve = 200,
DarkFlagshipTerrastruct = 201,
Terminal = 300,
TerminalGrayscale = 301,
Origami = 302
Expand Down Expand Up @@ -67,6 +68,10 @@ function dump(o)
end
end

-- Helper for non empty string
function is_nonempty_string(x)
return x ~= nil and type(x) == "string"
end

-- Counter for the diagram files
local counter = 0
Expand All @@ -75,7 +80,9 @@ local function render_graph(globalOptions)
local filter = {
CodeBlock = function(cb)
-- Check if the CodeBlock has the 'd2' class
if not cb.classes:includes('d2') or cb.text == nil then
is_d2_cb = cb.classes:includes('d2') or cb.attributes.classes:includes("d2")
--
if not is_d2_cb then
return nil
end

Expand All @@ -90,27 +97,28 @@ local function render_graph(globalOptions)
end

-- Transform options
if options.theme ~= nil and type(options.theme) == "string" then
if is_nonempty_string(options.theme) then
assert(D2Theme[options.theme] ~= nil, "Invalid theme: " .. options.theme .. ". Options are: " .. dump(D2Theme))
options.theme = D2Theme[options.theme]
end
if options.layout ~= nil and type(options.layout) == "string" then
if is_nonempty_string(options.layout) then
options.layout = string.lower(options.layout)
assert(D2Layout[options.layout] ~= nil, "Invalid layout: " .. options.layout .. ". Options are: " .. dump(D2Layout))
options.layout = D2Layout[options.layout]
end
if options.format ~= nil and type(options.format) == "string" then
if is_nonempty_string(options.format) then
assert(D2Format[options.format] ~= nil, "Invalid format: " .. options.format .. ". Options are: " .. dump(D2Format))
options.format = D2Format[options.format]
end
if options.embed_mode ~= nil and type(options.embed_mode) == "string" then
if is_nonempty_string(options.embed_mode) then
assert(EmbedMode[options.embed_mode] ~= nil, "Invalid embed_mode: " .. options.embed_mode .. ". Options are: " .. dump(EmbedMode))
options.embed_mode = EmbedMode[options.embed_mode]
end
if options.sketch ~= nil and type(options.sketch) == "string" then
if is_nonempty_string(options.sketch) then
assert(options.sketch == "true" or options.sketch == "false", "Invalid sketch: " .. options.sketch .. ". Options are: true, false")
options.sketch = options.sketch == "true"
end
if options.pad ~= nil and type(options.pad) == "string" then
if is_nonempty_string(options.pad) then
assert(tonumber(options.pad) ~= nil, "Invalid pad: " .. options.pad .. ". Must be a number")
options.pad = tonumber(options.pad)
end
Expand All @@ -121,13 +129,17 @@ local function render_graph(globalOptions)

-- Set default filename
if options.filename == nil then
options.filename = "diagram-" .. counter
options.filename = "diagram-" .. counter .. math.random (1, 1000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a random number to this filename?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had an issue with the extension not overwriting existing files that this seemed to resolve. I’ll try to put together a reprex (it may be specifically related to using knit_child?) to see if this random number assignment is a real fix or if there may be a better way to fix the issue.

end

-- Set the default format to pdf since svg is not supported in PDF output
if options.format == D2Format.svg and quarto.doc.is_format("latex") then
options.format = D2Format.pdf
end
-- Set the default format to svg since pdf is not supported in Typst output
if options.format == D2Format.pdf and quarto.doc.is_format("typst") then
options.format = D2Format.svg
end
-- Set the default embed_mode to link if the quarto format is not html or the figure format is pdf
if not quarto.doc.is_format("html") or options.format == D2Format.pdf then
options.embed_mode = EmbedMode.link
Expand All @@ -141,7 +153,7 @@ local function render_graph(globalOptions)
-- Generate diagram using `d2` CLI utility
local result = pandoc.system.with_temporary_directory('svg-convert', function (tmpdir)
-- determine path name of input file
local inputPath = pandoc.path.join({tmpdir, "temp_" .. counter .. ".txt"})
local inputPath = pandoc.path.join({tmpdir, "temp_" .. counter .. ".d2"})

-- determine path name of output file
local outputPath
Expand Down