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

Lua - add quarto.format.typst module #9527

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions news/changelog-1.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ All changes included in 1.5:
- ([#9394](https://github.com/quarto-dev/quarto-cli/issues/9394)): Make `template` a required field in the `about` schema.
- ([#9426](https://github.com/quarto-dev/quarto-cli/issues/9426)): Update `crossref.lua` filter to avoid crashes and hangs in documents with custom AST nodes.
- ([#9492](https://github.com/quarto-dev/quarto-cli/issues/9492)): Improve performance of `quarto preview` when serving resource files of the following formats: HTML, PDF, DOCX, and plaintext.
- ([#9527](https://github.com/quarto-dev/quarto-cli/pull/9527)): Add `quarto.format` and `quarto.format.typst` to Quarto's public Lua filter API.
- Add support for `{{< lipsum >}}` shortcode, which is useful for emitting placeholder text. Provide a specific number of paragraphs (`{{< lipsum 3 >}}`).
- Resolve data URIs in Pandoc's mediabag when rendering documents.
- Increase v8's max heap size by default, to avoid out-of-memory errors when rendering large documents (also cf. https://github.com/denoland/deno/issues/18935).
Expand Down
6 changes: 3 additions & 3 deletions src/resources/filters/customnodes/callout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,9 @@ end, function(callout)
title = pandoc.Plain(displayName(callout.type))
end

local typst_callout = typst_function_call("callout", {
{ "body", as_typst_content(callout.content) },
{ "title", as_typst_content(title) },
local typst_callout = _quarto.format.typst.function_call("callout", {
{ "body", _quarto.format.typst.as_typst_content(callout.content) },
{ "title", _quarto.format.typst.as_typst_content(title) },
{ "background_color", pandoc.RawInline("typst", background_color) },
{ "icon_color", pandoc.RawInline("typst", icon_color) },
{ "icon", pandoc.RawInline("typst", "" .. icon .. "()")}
Expand Down
60 changes: 60 additions & 0 deletions src/resources/filters/modules/typst.lua
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()
42 changes: 7 additions & 35 deletions src/resources/filters/quarto-post/typst.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,11 @@
--
-- renders AST nodes to Typst

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
for i, pair in ipairs(params) do
local k = pair[1]
local v = pair[2]
if v ~= nil then
result:insert(pandoc.RawInline("typst", k .. ": "))
add(v)
result:insert(pandoc.RawInline("typst", ", "))
else
add(k)
end
end
result:insert(pandoc.RawInline("typst", ")"))
return pandoc.Div(result)
end

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
-- FIXME Ideally this would go directly on init.lua, but
-- the module path set up doesn't appear to be working there.

local typst = require("modules/typst")
_quarto.format.typst = typst

function render_typst()
if not _quarto.format.isTypstOutput() then
Expand Down Expand Up @@ -94,8 +66,8 @@ function render_typst()
if el.classes:includes("unlisted") then
params:insert({"outlined", false})
end
params:insert({as_typst_content(el.content)})
return typst_function_call("heading", params)
params:insert({_quarto.format.typst.as_typst_content(el.content)})
return _quarto.format.typst.function_call("heading", params)
end,
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/resources/lua-types/quarto/format/typst.lua
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