-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9527 from quarto-dev/feature/typst-function-call-api
Lua - add `_quarto.format.typst` module
- Loading branch information
Showing
11 changed files
with
430 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
-- typst.lua | ||
-- Copyright (C) 2024 Posit Software, PBC | ||
|
||
-- helpers for working with typst in filters | ||
-- this module is exposed as quarto.format.typst | ||
|
||
local function _main() | ||
local function typst_function_call(name, params) | ||
local result = pandoc.Blocks({}) | ||
result:insert(pandoc.RawInline("typst", "#" .. name .. "(")) | ||
local function add(v) | ||
if type(v) == "userdata" or type(v) == "table" then | ||
result:extend(quarto.utils.as_blocks(v) or {}) | ||
else | ||
result:extend(quarto.utils.as_blocks({pandoc.utils.stringify(v)}) or {}) | ||
end | ||
end | ||
-- needs to be array of pairs because order matters for typst | ||
local n = #params | ||
for i, pair in ipairs(params) do | ||
if type(pair) == "table" then | ||
local k = pair[1] | ||
local v = pair[2] | ||
if v ~= nil then | ||
result:insert(pandoc.RawInline("typst", k .. ": ")) | ||
add(v) | ||
else | ||
add(k) | ||
end | ||
else | ||
add(pair) | ||
end | ||
if i < n then | ||
result:insert(pandoc.RawInline("typst", ", ")) | ||
end | ||
end | ||
result:insert(pandoc.RawInline("typst", ")")) | ||
|
||
-- We emit slightly inefficient output here by not using `quarto-scaffold`. | ||
-- The result from this div cannot be a quarto-scaffold because some typst template | ||
-- functions assume they can get the element's children. pandoc.Div is converted to | ||
-- a #block, and those are guaranteed to have children. | ||
return pandoc.Div(result) | ||
end | ||
|
||
local function as_typst_content(content) | ||
local result = pandoc.Blocks({}) | ||
result:insert(pandoc.RawInline("typst", "[\n")) | ||
result:extend(quarto.utils.as_blocks(content) or {}) | ||
result:insert(pandoc.RawInline("typst", "]\n")) | ||
return result | ||
end | ||
|
||
return { | ||
function_call = typst_function_call, | ||
as_typst_content = as_typst_content | ||
} | ||
end | ||
|
||
return _main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
---@meta | ||
|
||
quarto.format.typst = {} | ||
|
||
--[[ | ||
Create a node that represents a typst function call. | ||
]] | ||
---@param name string Name of function | ||
---@param params table<string, any> Array of {param, value} pairs | ||
---@return pandoc.Div | ||
function quarto.format.typst.function_call(name, params) end | ||
|
||
--[[ | ||
Create a node that represents typst content (`[Hello, world]`). Use | ||
this to ensure your output isn't interpreted as typst computations. | ||
]] | ||
---@param content any content | ||
---@return pandoc.Blocks | ||
function quarto.format.typst.as_typst_content(content) end |
Oops, something went wrong.